BigQuery Sharing 集成

借助 Google Cloud Cortex Framework,您可以将 BigQuery Sharing中订阅的关联数据集注册为外部 analytics_hub 目录模块。通过连接零复制共享数据集,您可以将外部商业、环境、合作伙伴或跨组织数据直接与 BigQuery 中预构建的 SAP ERP、SAP Business Data Cloud (SAP BDC) 和 Salesforce 数据产品混合,而无需构建或维护提取流水线。

支持的共享场景

您可以注册通过 data.modules.catalogs (type: analytics_hub) 下的 BigQuery Sharing 订阅的任何关联 BigQuery 数据集,包括:

  • 环境和地理空间预测:将天气集合预测(例如 Google WeatherNext)与工厂、仓库和入站交付时间表相结合,以对供应链中断风险进行建模。
  • 商业和物流情报:将第三方风险、信用和海事遥测数据(例如 Dun & Bradstreet Global Shipping Insights)与供应商和采购订单模型相结合。
  • 公共和合作伙伴数据交换:使用 Google Cloud Marketplace 和公共数据交换上发布的宏观经济指标、人口统计参考表或行业基准比较来丰富企业指标。
  • 跨组织和客户 360 共享:连接跨 Google Cloud 组织共享的内部数据网格列表或零复制 Salesforce Data 360 共享。

比较外部目录类型

Cortex Framework 支持两种外部零复制目录模块类型。请在下表中比较它们的基础设施要求:

维度 BigQuery 共享 (analytics_hub) BigLake Delta Sharing (lakehouse_delta_share)
主要数据源 已订阅的 BigQuery 关联数据集(商业列表、WeatherNext、Dun & Bradstreet、公共数据集、跨组织共享、Salesforce Data 360) 导入到 BigLake 的 Delta Sharing 共享(例如 SAP Business Data Cloud)
必需的 Google Cloud API bigquery.googleapis.com和analyticshub.googleapis.com之间 bigquery.googleapis.com、biglake.googleapis.com、bigqueryreservation.googleapis.com和cloudresourcemanager.googleapis.com
BigQuery 版本要求 标准按需或版本容量(无需企业 Plus 版预留) 需要有效的 BigQuery 企业 Plus 版预留分配
源表发现 在 inlineTableSettings.tables(或 inlineTableSettings.variants)中声明的确定性表和视图许可清单 从 BigLake 目录中导入的 connectionSettings.shares 发现(可选择使用 tableSettings 或 inlineTableSettings 进行过滤)

准备工作

在 Cortex Framework 中注册 analytics_hub 目录模块之前,请完成以下前提条件。

启用 Google Cloud API

在托管订阅的关联数据集的项目以及部署项目中启用所需的 Google Cloud API:

gcloud services enable bigquery.googleapis.com \
    analyticshub.googleapis.com \
    dataform.googleapis.com \
    --project=PROJECT_ID

替换以下内容:

  • PROJECT_ID:您在其中托管关联数据集或运行 Dataform 流水线的 Google Cloud 项目 ID,例如:my-cortex-project。

订阅清单

在 Google Cloud中,订阅 BigQuery Sharing 中的清单,以在您的使用方项目中创建只读关联数据集。验证关联的数据集(或配置的跨区域数据集副本)是否与 config/config.yaml 中的 data.bigQueryLocation 位于同一位置,并记录要包含在 inlineTableSettings.tables(或 inlineTableSettings.variants)中的共享表或视图名称。

获取所需的角色

如需获得在 BigQuery Sharing 共享目录中订阅清单和构建数据产品所需的权限,请让管理员向您授予以下 IAM 角色:

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

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

注册 analytics_hub 目录

在 config/config.yaml 中,使用 type: analytics_hub 注册 data.modules.catalogs 下的每个订阅的关联数据集。在 connectionSettings.datasetId 中仅指定简短的 BigQuery 数据集标识符(字母、数字和下划线),并在 inlineTableSettings.tables 中声明非空的共享表或视图名称列表(或在 inlineTableSettings.variants 下声明可选的变体特定列表,与目录条目上的 variants 配对)。在 uv run cortex-build 期间,AnalyticsHubCatalogBuilder 仅为列出的表生成 Dataform 来源声明 (definitions/sources/<project>_<dataset>_sources.js),而不会扫描不相关的对象或复制数据。

buildEnvironment:
  buildProjectId: BUILD_PROJECT_ID

data:
  bigQueryLocation: US
  namespaces:
    - name: cortex
    - name: cortex_samples
    - name: custom
      path: ../src/data_modules/custom
  datasets:
    - id: custom_products
      projectId: TARGET_PROJECT_ID
      datasetId: custom_supply_chain_insights

  modules:
    catalogs:
      # Example 1: Google WeatherNext linked dataset subscribed from BigQuery sharing.
      - id: ah_weathernext_catalog
        type: analytics_hub
        enabled: true
        bindsNamespaces:
          - cortex.external.weathernext
        connectionSettings:
          projectId: LINKED_DATASET_PROJECT_ID
          datasetId: WEATHERNEXT_DATASET_ID
          location: US
          # Optional source listing ID (auto-discovered when omitted):
          listingId: projects/PROVIDER_PROJECT_ID/locations/US/dataExchanges/EXCHANGE_ID/listings/LISTING_ID
        inlineTableSettings:
          tables:
            - weathernext_3_0_0
            - weathernext_daily_forecast

      # Example 2: Dun & Bradstreet shipping insights linked dataset.
      - id: ah_dnb_catalog
        type: analytics_hub
        enabled: true
        bindsNamespaces:
          - cortex.external.dnb
        connectionSettings:
          projectId: LINKED_DATASET_PROJECT_ID
          datasetId: DNB_DATASET_ID
          location: US
        inlineTableSettings:
          tables:
            - shipping_insights
            - company_profile

    products:
      # Composite data product blending SAP or Salesforce models with shared catalog tables.
      - moduleId: custom_delivery_weather_risk
        modulePath: custom.sap.products.delivery_weather_risk
        dataTargetId: custom_products
        dependencyBindings:
          sapDeliveries: sap_delivery_documents
          sapCustomers: sap_customers
          weatherNext: ah_weathernext_catalog.weathernext_daily_forecast
          dnbCatalog: ah_dnb_catalog

替换以下内容:

  • BUILD_PROJECT_ID:用于构建验证的 Google Cloud 项目 ID,例如:my-build-project。
  • TARGET_PROJECT_ID: Google Cloud 项目的 ID,其中可写入的下游数据产品已具体化,例如:my-target-project。
  • LINKED_DATASET_PROJECT_ID:托管订阅的关联数据集的 Google Cloud 项目 ID,例如:my-subscriber-project。
  • WEATHERNEXT_DATASET_ID:订阅的 WeatherNext 关联数据集的 BigQuery 数据集 ID,例如:weathernext_linked。
  • DNB_DATASET_ID:订阅的 Dun & Bradstreet 关联数据集的 BigQuery 数据集 ID,例如:dnb_shipping_insights_linked。
  • PROVIDER_PROJECT_ID:connectionSettings.listingId 的可选发布商 Google Cloud 项目 ID,例如:weathernext-provider。
  • EXCHANGE_ID:connectionSettings.listingId 的可选 BigQuery 共享数据交换 ID,例如:weather_data_exchange。
  • LISTING_ID:connectionSettings.listingId 的可选 BigQuery Sharing 列表 ID,例如:weathernext_daily。您可以将 listingId 指定为规范资源路径、完整商品详情网址或从 Google Cloud 控制台复制的由英文句点分隔的四部分 ID(其中 LOCATION 是交换位置,例如 US):

    projects/PROVIDER_PROJECT_ID/locations/LOCATION/dataExchanges/EXCHANGE_ID/listings/LISTING_ID
    PROVIDER_PROJECT_ID.LOCATION.EXCHANGE_ID.LISTING_ID
    

    如果您省略 listingId,Cortex Framework 会自动从关联数据集的订阅元数据或数据集说明中发现来源清单。

如需查看完整的字段规范,请参阅数据:模块:目录。

在复合数据产品中绑定共享表

data.modules.products 下的下游数据产品可以使用以下两种格式之一绑定到 dependencyBindings 中的 analytics_hub 目录:

  • 两部分绑定(<catalog_id>.<table_name> 或 <catalog_id>.<dataset_id>):将逻辑依赖项键绑定到 inlineTableSettings.tables 中列出的特定表或视图(例如 weatherNext: ah_weathernext_catalog.weathernext_daily_forecast),或绑定到目录配置的 datasetId。在 includes/config.js 中,moduleConfig.sources.<dependencyKey> 会解析 { projectId, datasetId, tables: [...] }(不含 tableName 属性),因此请将 projectId、datasetId 和声明的表名称字符串传递给 ctx.ref(...)。
  • 单部分目录绑定 (<catalog_id>):当模块的 manifest.yaml 在与 bindsNamespaces 匹配的命名空间下声明所需的表名称时,将逻辑依赖项键绑定到整个目录实例(例如 dnbCatalog: ah_dnb_catalog)。

以下 Dataform .js 示例使用 includes/ 中的 publish_config.getPublishConfig(...) 和 publisher.publish(...)(因此表设置和上游 blocking: true 断言会自动附加),将预构建的 sap_delivery_documents (cortex.sap.products.delivery_documents) 和 sap_customers (cortex.sap.products.customers) 数据产品与使用两部分格式绑定的共享表联接:

// ___MODULE_CONTEXT___
// ___TABLE_CONFIG___

const publish_config = require("includes/publish_config.js");
const publisher = require("includes/publisher.js");

const moduleConfig = config.product[moduleContext.moduleId];
const sapDeliveriesDataset = moduleConfig.sources.sapDeliveries.datasetId;
const sapCustomersDataset = moduleConfig.sources.sapCustomers.datasetId;
const weatherSource = moduleConfig.sources.weatherNext;

const materializationType = tableConfig.materializationType || "table";
const publishConfig = publish_config.getPublishConfig(
  materializationType,
  tableConfig,
  moduleConfig
);

publisher.publish(moduleContext.moduleId + "_" + tableConfig.tableName, publishConfig).query(
  (ctx) => `
    SELECT
      d.document_number_vbeln,
      d.ship_to_party_kunnr,
      c.postal_code_pstlz,
      w.init_time,
      w.precipitation_total_mm
    FROM ${ctx.ref(sapDeliveriesDataset, "delivery_document_headers")} AS d
    LEFT JOIN ${ctx.ref(sapCustomersDataset, "customers")} AS c
      ON d.client_mandt = c.client_mandt
      AND d.ship_to_party_kunnr = c.customer_number_kunnr
    LEFT JOIN ${ctx.ref(
      weatherSource.projectId,
      weatherSource.datasetId,
      "weathernext_daily_forecast"
    )} AS w
      ON c.postal_code_pstlz = w.postal_code
  `
);

如需了解将 SAP BDC 与 Google WeatherNext 3 和 Dun & Bradstreet Global Shipping Insights 相结合的可部署多源参考实现,请参阅供应商和工厂天气风险数据洞见以及供应商入站交付天气风险数据洞见。

提取并缓存 Agentic Workflows 的元数据

当您使用智能体技能构建自定义复合数据产品时,Cortex Framework 可以自动在本地检查和缓存 BigQuery 物理表架构和 BigQuery Sharing 列表元数据,以便 AI 编码助理生成准确的 SQL 联接,而无需猜测列名称。

针对您配置的目录 ID 运行 cortex-metadata fetch:

export CORTEX_CONFIG_FILE=config/config.yaml
uv run cortex-metadata fetch \
    --config $CORTEX_CONFIG_FILE \
    --module-id CATALOG_ID

替换以下内容:

  • CATALOG_ID:config/config.yaml 中 analytics_hub 目录条目的 id,例如:ah_weathernext_catalog。

提取器会检索并缓存:

  • 物理 BigQuery 元数据:inlineTableSettings.tables 中每个表的列名称、数据类型、说明、分区和聚簇定义、估计行数和字节大小。
  • Listing 治理元数据:发布商、数据提供商、显示名称、类别、文档网址、来源 Listing URI 以及数据集关联状态(当 listingId 或订阅权限可用时)。

预检验证和问题排查

在 uv run cortex-build 和 uv run cortex-build-and-deploy 期间,GcpEnvironmentChecker 会根据 BigQuery 和 BigQuery Sharing 验证每个已启用的 analytics_hub 目录,然后再暂存 Dataform 代码(独立 uv run cortex-deploy 仅运行部署目标检查)。下表总结了预检验证行为和解决方法:

检查 条件 结果 解决方法
数据集是否存在 在connectionSettings.projectId中找不到connectionSettings.datasetId 错误 (CortexGcpError) 请验证项目和数据集 ID,确认您已成功订阅 BigQuery Sharing,并向构建主体授予 roles/bigquery.dataViewer 角色。
数据集关联状态 linkedDatasetMetadata.linkState 为 UNLINKED 错误 (CortexGcpError) 发布商撤消或删除了底层列表链接。重新订阅 BigQuery Sharing 中的有效清单,并在创建了新数据集时更新 datasetId。
复制的数据集或标准数据集 BigQuery 中存在数据集,但没有关联的数据集元数据 警告(继续构建) 系统会跳过链接和订阅检查。请注意,复制的快照数据集不会自动接收来自原始提供商的更新。
订阅运行状况 订阅状态为 STATE_STALE 或 STATE_INACTIVE 警告(继续构建) 在 Google Cloud 控制台中的 BigQuery > 共享下检查订阅,以恢复或续订订阅。如果调用方缺少 analyticshub.subscriptions.list,Cortex Framework 会记录警告并继续。
配置的位置不匹配 connectionSettings.location 与实际 BigQuery 数据集位置不同 警告(使用实际数据集位置) 更新 config/config.yaml 中的 connectionSettings.location,使其与物理数据集区域一致。
跨区域兼容性(支持副本) 关联的数据集位置与 data.bigQueryLocation 不同,且该清单支持 data.bigQueryLocation(或无法读取清单区域) 警告(副本指南) 在 data.bigQueryLocation 中添加跨区域数据集副本(需要 bigquery.datasets.update),以便 BigQuery 能够执行同位置联接。
跨区域兼容性(不受支持的区域) 关联的数据集位置与 data.bigQueryLocation 不同,并且发现或配置的 listingId 确认该清单未在 data.bigQueryLocation 中提供 错误 (CortexGcpError) 将 data.bigQueryLocation 与该商品详情支持的区域对齐,或者如果您的提供商许可允许,将共享表复制到 data.bigQueryLocation 中的标准数据集。

后续步骤