查询公共数据集并直观呈现结果
了解如何在 BigQuery 中查询公共数据集并直观呈现结果。
如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示:
准备工作
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Make sure that you have the following role or roles on the project: BigQuery Job User, Service Usage Admin
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
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.
- 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
-
In the Google Cloud console, go to the IAM page.
前往 IAM - 选择项目。
- 点击 授予访问权限。
-
在新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
- 点击 Save(保存)。
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Make sure that you have the following role or roles on the project: BigQuery Job User, Service Usage Admin
Check for the roles
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
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.
- 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
-
In the Google Cloud console, go to the IAM page.
前往 IAM - 选择项目。
- 点击 授予访问权限。
-
在新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
- 点击 Save(保存)。
-
-
Verify that billing is enabled for your Google Cloud project.
确保已启用 BigQuery API。
如果您创建了一个新项目,则系统会自动启用 BigQuery API。
在 Google Cloud 控制台中,前往 BigQuery 页面。
在 BigQuery Studio 的探索器窗格中,点击
添加数据。在添加数据对话框的过滤依据窗格中,点击
公共数据集。
在 Marketplace 页面上的搜索 Marketplace 字段中,输入
NYC Citi Bike Trips
以缩小搜索范围。在搜索结果中,点击纽约市花旗单车行程。
在商品详情页面上,点击查看数据集。您可以在详细信息标签页中查看数据集的相关信息。
在 Google Cloud 控制台中,打开 BigQuery 页面。
点击
SQL 查询 。在
查询编辑器 中,输入以下查询: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;
如果查询有效,则会显示一个对勾标记以及查询处理的数据量。如果查询无效,则会显示一个感叹号,并会显示错误消息。
点击
运行 。最热门的车站会列在查询结果 部分中。可选:如需显示作业的时长和查询作业处理的数据量,请点击查询结果部分中的作业信息标签页。
切换到
可视化图表 标签页。此标签页会生成地图,以快速直观地呈现结果。在可视化图表配置面板中:
- 验证可视化图表类型是否设置为地图。
- 验证地理位置列是否设置为
geo_location
。 - 对于数据列,选择
num_trips
。 - 使用 放大选项显示曼哈顿地图。
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
- 了解 BigQuery 沙盒。
- 了解如何在 BigQuery 中创建数据集、加载数据和查询表。
如果您没有为本教程中使用的 Google Cloud 项目启用结算功能,则需要在 BigQuery 沙盒中查询公开数据。借助 BigQuery 沙盒,您可以免费使用限定的 BigQuery 功能,了解 BigQuery。
查看公共数据集
默认情况下,BigQuery 公共数据集可以通过 BigQuery Studio 在名为 bigquery-public-data
的项目中使用。在本教程中,您将查询纽约市花旗单车行程数据集。花旗单车 (Citi Bike) 是一项大型共享单车计划,在曼哈顿、布鲁克林、皇后区和泽西城拥有 10,000 辆单车和 600 个单车站点。此数据集包括花旗单车自 2013 年 9 月推出以来的花旗单车行程数据。
查询公共数据集
在以下步骤中,您将查询 citibike_trips
表,以确定纽约市花旗单车行程公共数据集中最受欢迎的 100 个花旗单车站点。该查询会检索车站的名称和位置,以及以该车站为起点的行程数量。
该查询使用 ST_GEOGPOINT 函数根据每个车站的经度和纬度参数创建一个点,并通过 GEOGRAPHY
列返回该点。GEOGRAPHY
列用于在集成地理位置数据查看器中生成热图。
清理
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。
删除项目
如果您使用 BigQuery 沙盒查询公共数据集,则您的项目不会启用结算功能,因此您无需删除该项目。
为了避免产生费用,最简单的方法是删除您为本教程创建的项目。
要删除项目,请执行以下操作: