可视化查询结果

在本快速入门中,您将学习如何在 BigQuery 笔记本中运行 SQL 并直观呈现结果:

  • 使用 bigquery-public-data.ml_datasets.penguins 公共数据集运行查询。
  • 使用 SQL 代码单元迭代查询结果。
  • 使用可视化图表单元格显示每种企鹅的雌性企鹅平均喙长和喙深。

准备工作

  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.

    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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  4. Verify that billing is enabled for your Google Cloud project.

  5. 验证已启用 BigQuery API。

    启用 API

    如果您创建了一个新项目,则系统会自动启用 BigQuery API。

  6. 所需权限

    如需创建和运行笔记本,您需要以下 Identity and Access Management (IAM) 角色:

    创建笔记本

    按照通过 BigQuery 编辑器创建笔记本中的说明创建新的笔记本。

    运行查询

    如需在笔记本中运行 SQL 查询,请按以下步骤操作:

    1. 如需在笔记本中创建新的 SQL 代码单元,请点击 SQL

    2. 输入以下查询:

      SELECT * FROM `bigquery-public-data.ml_datasets.penguins`;
      
    3. 点击 运行单元

      查询结果会自动保存到名为 df 的 DataFrame 中。

    4. 创建另一个 SQL 代码单元,并将标题更改为 female_penguins

    5. 输入以下查询,该查询会引用您刚刚创建的 DataFrame,并过滤结果以仅包含雌性企鹅:

      SELECT * FROM {df} WHERE sex = 'FEMALE';
      
    6. 点击 运行单元

      查询结果会自动保存在名为 female_penguins 的 DataFrame 中。

    直观呈现结果

    1. 如需在笔记本中创建新的可视化图表单元,请点击 可视化图表

    2. 点击选择 DataFrame,然后选择 female_penguins

      系统随即会显示图表界面。

    3. 点击散点图打开图表菜单,然后选择 竖条图

    4. 指标部分,检查是否显示 culmen_length_mmculmen_depth_mm。如果缺少某个指标,请点击 添加指标,然后选择该指标。如需移除某个指标,请将指针悬停在该指标名称上,然后点击 关闭

    5. 针对每个指标,点击 修改。 对于聚合,请选择平均值

    显示可视化的条形图

    清理

    为了避免产生费用,最简单的方法是删除您为本教程创建的项目。

    要删除项目,请执行以下操作:

    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.

    后续步骤