Hive to BigQuery 模板

使用 Managed Service for Apache Spark Hive to BigQuery 模板将数据从 Hive 提取到 BigQuery。

使用模板

使用 gcloud CLI 或 Managed Service for Apache Spark API 运行模板。

gcloud

在使用下面的命令数据之前, 请先进行以下替换:

  • PROJECT_ID:必填。您的 Google Cloud 项目 ID,列在 IAM 设置中。
  • REGION:必填。Compute Engine 区域。
  • TEMPLATE_VERSION:必填。指定 latest 以使用最新 模板版本,或指定特定版本的日期,例如 2023-03-17_v0.1.0-beta (访问 gs://dataproc-templates-binaries 或运行 gcloud storage ls gs://dataproc-templates-binaries 以列出可用的模板版本)。
  • SUBNET:可选。如果未指定子网,系统会选择 default 网络中指定 REGION 内的子网。

    示例: projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET_NAME

  • HOST 和 PORT:必填。 源 Hive 数据库主机的主机名或 IP 地址和端口。

    示例: 10.0.0.33:9083

  • DATASET:必填。BigQuery 输出数据集名称。
  • TABLE:必填。BigQuery 输出表名称。
  • QUERY:必填。用于从 Hive 提取数据的查询。
  • TEMP_BUCKET:必填。Cloud Storage 存储桶 名称(仅指定存储桶的名称)。此存储桶用于 BigQuery 加载。
  • MODE:必填。BigQuery 输出的写入模式。 选项:Append、Overwrite、Ignore、 或 ErrorIfExists。
  • TEMPVIEW 和 TEMPVIEW_SQL_QUERY:可选。 您可以使用这两个可选参数在将数据加载到 BigQuery 时应用 Spark SQL 转换。 TEMPVIEW 是临时视图名称,TEMPVIEW_SQL_QUERY 是查询语句。TEMPVIEW 和 TEMPVIEW_SQL_QUERY 中的表名必须匹配。
  • SERVICE_ACCOUNT:可选。如果未提供,系统会使用 默认的 Compute Engine 服务账号 。
  • PROPERTY 和 PROPERTY_VALUE: 可选。以逗号分隔的 Spark 属性=value 对列表。
  • LABEL 和 LABEL_VALUE: 可选。以英文逗号分隔的 label=value 对列表。
  • LOG_LEVEL:可选。日志记录级别。可以是 ALL、DEBUG、ERROR、FATAL、INFO、 OFF、TRACE 或 WARN 之一。默认值:INFO。
  • KMS_KEY:可选。用于加密的 Cloud Key Management Service。如果未指定密钥,系统会使用对静态数据进行加密 Google-owned and Google-managed encryption key。

    示例:projects/PROJECT_ID/regions/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME

执行以下命令:

Linux、macOS 或 Cloud Shell

gcloud dataproc batches submit spark \
    --class=com.google.cloud.dataproc.templates.main.DataProcTemplate \
    --version="2.3" \
    --project="PROJECT_ID" \
    --region="REGION" \
    --jars="gs://dataproc-templates-binaries/TEMPLATE_VERSION/java/dataproc-templates.jar" \
    --subnet="SUBNET" \
    --service-account="SERVICE_ACCOUNT" \
    --properties="spark.hadoop.hive.metastore.uris=thrift://HOST:PORT,PROPERTY=PROPERTY_VALUE" \
    --kms-key="KMS_KEY" \
    --labels="LABEL=LABEL_VALUE" \
    -- --template HIVETOBIGQUERY \
    --templateProperty log.level="LOG_LEVEL" \
    --templateProperty hivetobq.bigquery.location="PROJECT_ID.DATASET.TABLE" \
    --templateProperty hivetobq.sql="QUERY" \
    --templateProperty hivetobq.temp.gcs.bucket="TEMP_BUCKET" \
    --templateProperty hivetobq.write.mode="MODE" \
    --templateProperty hivetobq.temp.table="TEMPVIEW" \
    --templateProperty hivetobq.temp.query="TEMPVIEW_SQL_QUERY"

Windows (PowerShell)

gcloud dataproc batches submit spark `
    --class=com.google.cloud.dataproc.templates.main.DataProcTemplate `
    --version="2.3" `
    --project="PROJECT_ID" `
    --region="REGION" `
    --jars="gs://dataproc-templates-binaries/TEMPLATE_VERSION/java/dataproc-templates.jar" `
    --subnet="SUBNET" `
    --service-account="SERVICE_ACCOUNT" `
    --properties="spark.hadoop.hive.metastore.uris=thrift://HOST:PORT,PROPERTY=PROPERTY_VALUE" `
    --kms-key="KMS_KEY" `
    --labels="LABEL=LABEL_VALUE" `
    -- --template HIVETOBIGQUERY `
    --templateProperty log.level="LOG_LEVEL" `
    --templateProperty hivetobq.bigquery.location="PROJECT_ID.DATASET.TABLE" `
    --templateProperty hivetobq.sql="QUERY" `
    --templateProperty hivetobq.temp.gcs.bucket="TEMP_BUCKET" `
    --templateProperty hivetobq.write.mode="MODE" `
    --templateProperty hivetobq.temp.table="TEMPVIEW" `
    --templateProperty hivetobq.temp.query="TEMPVIEW_SQL_QUERY"

Windows (cmd.exe)

gcloud dataproc batches submit spark ^
    --class=com.google.cloud.dataproc.templates.main.DataProcTemplate ^
    --version="2.3" ^
    --project="PROJECT_ID" ^
    --region="REGION" ^
    --jars="gs://dataproc-templates-binaries/TEMPLATE_VERSION/java/dataproc-templates.jar" ^
    --subnet="SUBNET" ^
    --service-account="SERVICE_ACCOUNT" ^
    --properties="spark.hadoop.hive.metastore.uris=thrift://HOST:PORT,PROPERTY=PROPERTY_VALUE" ^
    --kms-key="KMS_KEY" ^
    --labels="LABEL=LABEL_VALUE" ^
    -- --template HIVETOBIGQUERY ^
    --templateProperty log.level="LOG_LEVEL" ^
    --templateProperty hivetobq.bigquery.location="PROJECT_ID.DATASET.TABLE" ^
    --templateProperty hivetobq.sql="QUERY" ^
    --templateProperty hivetobq.temp.gcs.bucket="TEMP_BUCKET" ^
    --templateProperty hivetobq.write.mode="MODE" ^
    --templateProperty hivetobq.temp.table="TEMPVIEW" ^
    --templateProperty hivetobq.temp.query="TEMPVIEW_SQL_QUERY"

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:必填。您的 Google Cloud 项目 ID,列在 IAM 设置中。
  • REGION:必填。Compute Engine 区域。
  • TEMPLATE_VERSION:必填。指定 latest 以使用最新 模板版本,或指定特定版本的日期,例如 2023-03-17_v0.1.0-beta (访问 gs://dataproc-templates-binaries 或运行 gcloud storage ls gs://dataproc-templates-binaries 以列出可用的模板版本)。
  • SUBNET:可选。如果未指定子网,系统会选择 default 网络中指定 REGION 内的子网。

    示例: projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET_NAME

  • HOST 和 PORT:必填。 源 Hive 数据库主机的主机名或 IP 地址和端口。

    示例: 10.0.0.33:9083

  • DATASET:必填。BigQuery 输出数据集名称。
  • TABLE:必填。BigQuery 输出表名称。
  • QUERY:必填。用于从 Hive 提取数据的查询。
  • TEMP_BUCKET:必填。Cloud Storage 存储桶 名称(仅指定存储桶的名称)。此存储桶用于 BigQuery 加载。
  • MODE:必填。BigQuery 输出的写入模式。 选项:Append、Overwrite、Ignore、 或 ErrorIfExists。
  • TEMPVIEW 和 TEMPVIEW_SQL_QUERY:可选。 您可以使用这两个可选参数在将数据加载到 BigQuery 时应用 Spark SQL 转换。 TEMPVIEW 是临时视图名称,TEMPVIEW_SQL_QUERY 是查询语句。TEMPVIEW 和 TEMPVIEW_SQL_QUERY 中的表名必须匹配。
  • SERVICE_ACCOUNT:可选。如果未提供,系统会使用 默认的 Compute Engine 服务账号 。
  • PROPERTY 和 PROPERTY_VALUE: 可选。以逗号分隔的 Spark 属性=value 对列表。
  • LABEL 和 LABEL_VALUE: 可选。以英文逗号分隔的 label=value 对列表。
  • LOG_LEVEL:可选。日志记录级别。可以是 ALL、DEBUG、ERROR、FATAL、INFO、 OFF、TRACE 或 WARN 之一。默认值:INFO。
  • KMS_KEY:可选。用于加密的 Cloud Key Management Service。如果未指定密钥,系统会使用对静态数据进行加密 Google-owned and Google-managed encryption key。

    示例:projects/PROJECT_ID/regions/REGION/keyRings/KEY_RING_NAME/cryptoKeys/KEY_NAME

HTTP 方法和网址:

POST https://dataproc.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/batches

请求 JSON 正文:


{
  "environmentConfig":{
    "executionConfig":{
      "subnetworkUri":"SUBNET",
      "kmsKey": "KMS_KEY",
      "serviceAccount": "SERVICE_ACCOUNT"
    }
  },
  "labels": {
    "LABEL": "LABEL_VALUE"
  },
  "runtimeConfig": {
    "version": "2.3",
    "properties": {
      "spark.hadoop.hive.metastore.uris":"thrift://HOST:PORT",
      "PROPERTY": "PROPERTY_VALUE"
    }
  },
  "sparkBatch":{
    "mainClass":"com.google.cloud.dataproc.templates.main.DataProcTemplate",
    "args":[
      "--template","HIVETOBIGQUERY",
      "--templateProperty","log.level=LOG_LEVEL",
      "--templateProperty","hivetobq.bigquery.location=PROJECT_ID.DATASET.TABLE",
      "--templateProperty","hivetobq.sql=QUERY",
      "--templateProperty","hivetobq.temp.gcs.bucket=TEMP_BUCKET",
      "--templateProperty","hivetobq.write.mode=MODE",
      "--templateProperty","hivetobq.temp.table=TEMPVIEW",
      "--templateProperty","hivetobq.temp.query=TEMPVIEW_SQL_QUERY"
    ],
    "jarFileUris":[
      "gs://dataproc-templates-binaries/TEMPLATE_VERSION/java/dataproc-templates.jar"
    ]
  }
}

如需发送您的请求,请展开以下选项之一:

您应该收到类似以下内容的 JSON 响应:


{
  "name": "projects/PROJECT_ID/regions/REGION/operations/OPERATION_ID",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.dataproc.v1.BatchOperationMetadata",
    "batch": "projects/PROJECT_ID/locations/REGION/batches/BATCH_ID",
    "batchUuid": "de8af8d4-3599-4a7c-915c-798201ed1583",
    "createTime": "2023-02-24T03:31:03.440329Z",
    "operationType": "BATCH",
    "description": "Batch"
  }
}