查詢公開資料集並將結果視覺化

瞭解如何在 BigQuery 中查詢公開資料集,並將結果視覺化。


如要直接在 Google Cloud 控制台按照逐步指南操作,請按一下「Guide me」(逐步引導)

逐步引導


事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that you have the following role or roles on the project: BigQuery Job User, Service Usage Admin

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往 IAM
    2. 選取專案。
    3. 按一下 「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that you have the following role or roles on the project: BigQuery Job User, Service Usage Admin

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      前往 IAM
    2. 選取專案。
    3. 按一下 「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  6. Verify that billing is enabled for your Google Cloud project.

  7. 如果在本教學課程中使用的 Google Cloud 專案未啟用計費功能,您需要在 BigQuery 沙箱中查詢公開資料。BigQuery 沙箱可讓您學習 BigQuery,但可免費使用的 BigQuery 功能有限。

  8. 確認已啟用 BigQuery API。

    啟用 API

    如果您建立新專案,系統會自動啟用 BigQuery API。

  9. 查看公開資料集

    根據預設,BigQuery 公開資料集會直接顯示在名為 bigquery-public-data 的專案中。在本教學課程中,您將查詢紐約市 Citi Bike 行程資料集。Citi Bike 是大型自行車共享計畫,在曼哈頓、布魯克林、皇后區和澤西市設有 10,000 輛自行車及 600 個站點。這個資料集包含 Citi Bike 自 2013 年 9 月推出以來的行程。

    1. 前往 Google Cloud 控制台的「BigQuery」頁面。

      前往「BigQuery」

    2. 在 BigQuery Studio 的「Explorer」窗格中,按一下「新增資料」

    3. 在「新增資料」對話方塊的「依條件篩選」窗格中,按一下 「依頁面篩選」頁面上的公開資料集圖示「公開資料集」

    4. 在「Marketplace」頁面的「Search Marketplace」欄位中輸入 NYC Citi Bike Trips,縮小搜尋範圍。

    5. 在搜尋結果中,按一下「NYC Citi Bike Trips」

    6. 在「產品詳細資料」頁面中,按一下「查看資料集」。您可以在「詳細資料」分頁中查看資料集相關資訊。

    查詢公開資料集

    在下列步驟中,您會查詢 citibike_trips 資料表,找出 NYC Citi Bike 行程公開資料集中最熱門的 100 個 Citi Bike 租借站。這項查詢會擷取車站名稱和位置,以及從該車站出發的行程數量。

    這項查詢會使用 ST_GEOGPOINT 函式,根據每個車站的經緯度參數建立點,並在 GEOGRAPHY 欄中傳回該點。GEOGRAPHY 欄用於在整合式地理資料檢視器中產生熱視圖。

    1. 在 Google Cloud 控制台開啟「BigQuery」BigQuery頁面。

      前往「BigQuery」

    2. 按一下 「SQL query」(SQL 查詢)

    3. 查詢編輯器中輸入以下查詢:

      SELECT
        start_station_name,
        start_station_latitude,
        start_station_longitude,
        ST_GEOGPOINT(start_station_longitude, start_station_latitude) AS geo_location,
        COUNT(*) AS num_trips
      FROM
        `bigquery-public-data.new_york.citibike_trips`
      GROUP BY
        1,
        2,
        3
      ORDER BY
        num_trips DESC
      LIMIT
        100;
      

      如果查詢有效,系統就會顯示勾號和查詢處理的資料量。如果查詢無效,則會顯示驚嘆號和錯誤訊息。

      查詢驗證工具

    4. 點選「Run」(執行),「Query results」(查詢結果) 部分會列出最熱門的電台。

       Google Cloud 控制台中的查詢結果

    5. 選用步驟:如要查看工作持續時間和查詢工作處理的資料量,請點選「Query results」(查詢結果) 專區中的「Job information」(工作資訊) 分頁標籤。

    6. 切換至「Visualization」分頁。這個分頁會生成地圖,方便您快速查看結果。

    7. 在「視覺化設定」面板中:

      1. 確認「圖表類型」已設為「地圖」
      2. 確認「Geography column」(地理位置資料欄) 已設為 geo_location
      3. 在「資料欄」部分,選擇 num_trips
      4. 使用「 放大」選項,顯示曼哈頓地圖。

      「視覺化」分頁中產生的熱視圖

    清除所用資源

    如要避免系統向您的 Google Cloud 帳戶收取本頁所用資源的費用,請按照下列步驟操作。

    刪除專案

    如果您使用 BigQuery 沙箱查詢公開資料集,則專案不會啟用帳單功能,因此您不需要刪除專案。

    如要避免付費,最簡單的方法就是刪除您為了本教學課程所建立的專案。

    如要刪除專案:

    1. In the Google Cloud console, go to the Manage resources page.

      Go to Manage resources

    2. In the project list, select the project that you want to delete, and then click Delete.
    3. In the dialog, type the project ID, and then click Shut down to delete the project.

    後續步驟