将 BigQuery 数据同步到 AlloyDB

本页介绍了如何将 BigQuery 中的表同步到 AlloyDB for PostgreSQL 实例中。

通过将分析数据从 BigQuery 同步到 AlloyDB,您可以构建运营系统,该系统可受益于对数据湖的低延迟事务性访问。与就地查询数据的外部数据封装容器 (FDW) 不同,同步表会将数据移至 AlloyDB 存储空间,以实现最佳性能。

AlloyDB 提供了以下方法,可将 BigQuery 数据移至您的实例:

  • 一次性同步:创建 BigQuery 表的可写入独立副本。

  • 定期同步(镜像):创建只读本地表,该表会按预定时间自动刷新,例如每 6 小时或每天刷新一次。

性能和运营注意事项

使用 BigQuery 同步表时,请注意以下事项:

  • 资源用量:数据移动会消耗 CPU 和内存。对于非常大的表,请考虑在非高峰时段安排同步,以免影响主要事务性工作负载。
  • 数据可见性:在替换操作期间,系统会预先舍弃并重新创建现有目标表。在导入期间,查询最初会看到一个空表,随后随着批处理事务提交,新导入的数据会逐步显示。

准备工作

  1. 熟悉 bigquery_fdw 如何处理 BigQuery 数据类型和列映射,因为 alloydb_sync 扩展程序使用 bigquery_fdw 连接到 BigQuery。
  2. 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  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 role (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. 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 role (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. 启用创建和连接到 AlloyDB 所需的 Cloud API。

    启用 API

  8. 如需确认您要更改的项目的名称,请在确认项目步骤中点击下一步

  9. 启用 API 步骤中,点击启用以启用以下内容:

    • AlloyDB API
    • Compute Engine API
    • Cloud Resource Manager API
    • Service Networking API
    • BigQuery Storage API

    如果您计划使用与 AlloyDB 位于同一 Google Cloud 项目中的 VPC 网络配置与 AlloyDB 的网络连接,则需要使用 Service Networking API。

    如果您计划使用位于其他 Google Cloud 项目中的 VPC 网络配置与 AlloyDB 的网络连接,则需要使用 Compute Engine API 和 Cloud Resource Manager API。

  10. 确保您有一个现有的 BigQuery 表,可从中同步数据。如需了解详情,请参阅创建和使用 BigQuery 表

所需的角色

如需向 AlloyDB 集群服务账号授予对 BigQuery 数据集的访问权限,您需要以下权限:

  • BigQuery Data Viewer (roles/bigquery.dataViewer) 或具有 bigquery.tables.getbigquery.tables.getData 权限的任何自定义角色。针对服务账号授予此角色后,可提供从表或视图中读取数据和元数据的权限。
  • BigQuery Read Session User (roles/bigquery.readSessionUser) 或具有 bigquery.readsessions.createbigquery.readsessions.getData 权限的任何自定义角色。提供创建和使用读取会话的功能。
  • BigQuery Job User (roles/bigquery.jobUser) 或具有 bigquery.jobs.create 权限的任何自定义角色。提供创建和运行作业(包括查询作业)的功能。

配置扩展程序

在从 BigQuery 同步表之前,请启用所需的扩展程序并配置与 BigQuery 的连接。如果您使用 Google Cloud 控制台,AlloyDB 会自动执行这些步骤。

  1. 创建 扩展程序:

    1. 按照将 psql 客户端连接到实例中的说明,使用 psql 客户端连接到 AlloyDB 实例。
    2. 运行以下命令:

      CREATE EXTENSION IF NOT EXISTS alloydb_sync;
      
  2. 如需让 AlloyDB 通过 BigQuery 进行身份验证,请创建用户映射。

    CREATE EXTENSION IF NOT EXISTS bigquery_fdw;
    CREATE SERVER IF NOT EXISTS BIGQUERY_SERVER_NAME FOREIGN DATA WRAPPER bigquery_fdw;
    CREATE USER MAPPING IF NOT