使用 SQL 单元格

本指南介绍了如何使用 SQL 代码单元在 Colab Enterprise 笔记本中查询数据。

概览

SQL 代码单元是一种代码单元,用于在 Colab Enterprise 笔记本中编写、修改和运行 SQL 查询。SQL 代码单元提供了一种替代 IPython Magics for BigQuery 的工作流。

功能

SQL 代码单元提供以下功能:

  • 支持试运行:SQL 语句验证和查询处理的字节数近似值
  • 格式设置:关键字 lint 检查和语法突出显示
  • BigQuery DataFrame 输出变量命名:从其他笔记本单元格中引用输出变量
  • 变量替换:引用 Python 变量和 SQL 代码单元,以支持参数化和查询先前查询结果的功能
  • 结果集查看器:轻量级表格结果集查看器,可针对大型结果集进行分页

支持的 SQL 方言和数据源

  • Colab Enterprise SQL 代码单元支持 GoogleSQL。

  • 您可以对 BigQuery 数据运行 SQL 查询。

限制

在规划项目时,请考虑以下限制:

  • 您可以在单个 SQL 单元中运行多条 SQL 语句,但只有最后一条 SQL 语句的结果会保存到 DataFrame 中。

准备工作

  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. Verify that billing is enabled for your Google Cloud project.

  4. Enable the BigQuery, Compute Engine, Dataform, and Vertex AI APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. 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

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

  7. Enable the BigQuery, Compute Engine, Dataform, and Vertex AI APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  8. 所需的角色

    如需获得创建 Colab Enterprise 笔记本、在运行时运行笔记本代码以及在笔记本中使用 BigQuery 数据所需的权限,请让管理员向您授予项目的以下 IAM 角色:

    如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

    您也可以通过自定义角色或其他预定义角色来获取所需的权限。

    创建 SQL 代码单元

    如需在 Colab Enterprise 中创建 SQL 代码单元,请执行以下操作:

    1. 在 Google Cloud 控制台中,前往 Colab Enterprise 我的笔记本页面。

      前往我的笔记本

    2. 区域菜单中,选择包含笔记本的区域。

    3. 点击您要打开的笔记本。如果您尚未创建笔记本,请创建一个笔记本

    4. 在工具栏中,如需添加 SQL 单元格,请点击  插入代码单元格选项菜单,然后选择添加 SQL 单元格

      您的 SQL 单元格已添加到笔记本中。

    输入并运行查询

    1. 在 SQL 单元中,输入 SQL 查询。如需简要了解支持的语句和 SQL 方言,请参阅 BigQuery 中的 SQL 简介

      您可以在表达式中引用 Python 变量,方法是将变量名称放在大括号 ({ }) 中。例如,如果您在名为 my_threshold 的 Python 变量中指定了一个值,则可以使用类似于以下内容的查询来限制结果集:

      SELECT * FROM my_dataset.my_table WHERE x > {my_threshold};
          
    2. 将指针悬停在要运行的 SQL 单元上,然后点击  运行单元按钮。

    查询的输出会自动保存为 BigQuery DataFrame,其名称与 SQL 代码单元的标题相同。

    与结果集互动

    您可以将结果集作为 BigQuery DataFrame 或 pandas DataFrame 进行交互。

    您可以使用相同的 SQL 代码单元变量名称链接 SQL 语句。例如,您可以将结果集生成的 BigQuery DataFrames 用作后续查询中的表,只需将 DataFrame 名称放在大括号 ({ }) 中即可。请参阅以下示例,该示例引用了之前查询的输出,该输出已保存为名为 df 的 DataFrame:

    SELECT * FROM {df};

    后续步骤