使用 BigQuery Studio 查询公共数据集并直观呈现结果

了解如何使用 BigQuery Studio 查询公共数据集并直观呈现结果。


如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示

操作演示


准备工作

  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. 新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。

    5. 选择角色列表中,选择一个角色。
    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. 新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。

    5. 选择角色列表中,选择一个角色。
    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 Studio 在名为 bigquery-public-data 的项目中使用。在本教程中,您将查询纽约市花旗单车行程数据集。花旗单车 (Citi Bike) 是一项大型共享单车计划,在曼哈顿、布鲁克林、皇后区和泽西城拥有 10,000 辆单车和 600 个单车站点。此数据集包括花旗单车自 2013 年 9 月推出以来的花旗单车行程数据。

    1. 在 Google Cloud 控制台中,前往 BigQuery Studio 页面。

      进入 BigQuery Studio

    2. 浏览器窗格中,点击 添加数据

    3. 添加数据对话框的过滤依据窗格中,点击 “过滤依据”页面上的公共数据集图标 公共数据集

    4. Marketplace 页面上的搜索 Marketplace 字段中,输入 NYC Citi Bike Trips 以缩小搜索范围。

    5. 在搜索结果中,点击纽约市花旗单车行程

    6. 商品详情页面上,点击查看数据集。您可以在详细信息标签页中查看数据集的相关信息。

    查询公共数据集

    在以下步骤中,您将查询 citibike_trips 表,以确定纽约市花旗单车行程公共数据集中最受欢迎的 100 个花旗单车站点。该查询会检索车站的名称和位置,以及以该车站为起点的行程数量。

    该查询使用 ST_GEOGPOINT 函数根据每个车站的经度和纬度参数创建一个点,并通过 GEOGRAPHY 列返回该点。GEOGRAPHY 列用于在集成地理位置数据查看器中生成热图。

    1. 在 Google Cloud 控制台中,打开 BigQuery Studio 页面。

      进入 BigQuery Studio

    2. 点击 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. 点击运行。最热门的车站会列在查询结果部分中。

       Google Cloud 控制台中的查询结果

    5. 可选:如需显示作业的时长和查询作业处理的数据量,请点击查询结果部分中的作业信息标签页。

    6. 切换到可视化图表标签页。此标签页会生成地图,以快速直观地呈现结果。

    7. 可视化图表配置面板中:

      1. 验证可视化图表类型是否设置为地图
      2. 验证地理位置列是否设置为 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.

    后续步骤