

# 创建 BigQuery 连接
<a name="creating-bigquery-connection"></a>

要从 AWS Glue 中连接到 Google BigQuery，您需要创建 Google Cloud Platform 凭证并将其存储在 AWS Secrets Manager 密钥中，然后将该密钥与 Google BigQuery AWS Glue 连接关联。

**要配置与 BigQuery 的连接：**

1. 在 Google Cloud Platform，创建并识别相关资源：
   + 创建或标识包含您想要连接的 BigQuery 表的 GCP 项目。
   + 启用 BigQuery API。有关更多信息，请参阅[使用 BigQuery 存储读取 API 读取表数据](https://cloud.google.com/bigquery/docs/reference/storage/#enabling_the_api)。

1. 在 Google Cloud Platform 中，创建和导出服务账户凭证：

   您可以使用 BigQuery 凭证向导来加快此步骤：[创建凭证](https://console.cloud.google.com/apis/credentials/wizard?api=bigquery.googleapis.com)。

   要在 GCP 中创建服务账户，请按照[创建服务账户](https://cloud.google.com/iam/docs/service-accounts-create)中的教程进行操作。
   + 选择**项目**时，请选择包含您的 BigQuery 表的项目。
   + 为您的服务账户选择 GCP IAM 角色时，请添加或创建一个角色，该角色将授予运行 BigQuery 作业的相应权限，以读取、写入或创建 BigQuery 表。

   要为您的服务账户创建凭证，请按照[创建服务账户密钥](https://cloud.google.com/iam/docs/keys-create-delete#creating)中的教程进行操作。
   + 在选择密钥类型时，请选择 **JSON**。

   现在，您应该已经下载了包含服务账户凭证的 JSON 文件。如下所示：

   ```
   {
     "type": "service_account",
     "project_id": "*****",
     "private_key_id": "*****",
     "private_key": "*****",
     "client_email": "*****",
     "client_id": "*****",
     "auth_uri": "https://accounts.google.com/o/oauth2/auth",
     "token_uri": "https://oauth2.googleapis.com/token",
     "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
     "client_x509_cert_url": "*****",
     "universe_domain": "googleapis.com"
   }
   ```

1. 在 AWS Secrets Manager 中，使用您下载的凭证文件创建密钥。您可以选择“**纯文本**”选项卡并粘贴 JSON 格式的文件内容。要在 Secrets Manager 中创建密钥，请按照 AWS Secrets Manager 文档中[创建 AWS Secrets Manager 密钥](https://docs.aws.amazon.com//secretsmanager/latest/userguide/create_secret.html)中的教程进行操作。创建密钥后，保留密钥名称 *secretName*，以供下一步使用。

1. 在 AWS Glue Data Catalog 中，按照 [https://docs.aws.amazon.com/glue/latest/dg/console-connections.html](https://docs.aws.amazon.com/glue/latest/dg/console-connections.html) 中的步骤创建连接。创建连接后，保留连接名称 *connectionName*，以供下一步使用。
   + 在选择**连接类型**时，请选择 Google BigQuery。
   + 选择 **AWS 密钥**时，请提供 *secretName*。

1. 向与您的 AWS Glue 作业关联的 IAM 角色授予读取 *secretName* 的权限。

1. 在 AWS Glue 作业配置中，提供 *connectionName* 作为**附加网络连接**。