Cloud Storage to BigQuery 模板

使用 Dataproc Serverless Cloud Storage to BigQuery 模板将数据从 Cloud Storage 提取到 BigQuery。

使用模板

使用 gcloud CLI 或 Dataproc API 运行模板。

gcloud

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

  • PROJECT_ID:必填。IAM 设置中列出的 Google Cloud 项目 ID。
  • REGION:必填。Compute Engine 区域
  • TEMPLATE_VERSION:必填。指定 latest 表示最新的模板版本,指定特定版本的日期,例如 2023-03-17_v0.1.0-beta(访问 gs://dataproc-templates-binaries 或运行 gsutil ls gs://dataproc-templates-binaries 列出可用的模板版本)。
  • CLOUD_STORAGE_PATH:必填。来源 Cloud Storage 路径。

    示例: gs://dataproc-templates/hive_to_cloud_storage_output"

  • FORMAT:必填。输入数据格式。选项:avroparquetcsvjson注意:如果为 avro,您必须将“file:///usr/lib/spark/external/spark-avro.jar”添加到 jars gcloud CLI 标志或 API 字段中。

    示例(file:// 前缀引用 Dataproc Serverless jar 文件)

    --jars=file:///usr/lib/spark/external/spark-avro.jar, [、... 其他 jar 文件]
  • DATASET:必填。目标 BigQuery 数据集。
  • TABLE:必填。目标 BigQuery 表。
  • TEMP_BUCKET:必填。用于在加载到 BigQuery 之前暂存数据的临时 Cloud Storage 存储桶。
  • SUBNET:可选。如果未指定子网,则系统会选择 default 网络中指定 REGION 中的子网。

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

  • TEMPVIEWSQL_QUERY:可选。 您可以使用这两个可选参数在将数据加载到 BigQuery 中时应用 Spark SQL 转换。TEMPVIEW 是临时视图名称,SQL_QUERY 是查询语句。TEMPVIEW 与 SQL_QUERY 中的表名必须匹配。
  • SERVICE_ACCOUNT:可选。如果未提供此项,系统会使用默认 Compute Engine 服务帐号
  • PROPERTYPROPERTY_VALUE:可选。以英文逗号分隔的 Spark 属性=value对列表。
  • LABELLABEL_VALUE:可选。以英文逗号分隔的 label=value 对列表。
  • LOG_LEVEL:可选。日志记录级别。可以是 ALLDEBUGERRORFATALINFOOFFTRACEWARN 中的一个。默认值:INFO
  • KMS_KEY:可选。要用于加密的 Cloud Key Management Service 密钥。如果未指定密钥,系统会使用 Google 管理的密钥对数据进行静态加密

    示例: 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="1.1" \
    --project="PROJECT_ID" \
    --region="REGION" \
    --jars="gs://dataproc-templates-binaries/TEMPLATE_VERSION/java/dataproc-templates.jar" \
    --subnet="SUBNET" \
    --kms-key="KMS_KEY" \
    --service-account="SERVICE_ACCOUNT" \
    --properties="PROPERTY=PROPERTY_VALUE" \
    --labels="LABEL=LABEL_VALUE" \
    -- --template=GCSTOBIGQUERY \
    --templateProperty log.level="LOG_LEVEL" \
    --templateProperty project.id="PROJECT_ID" \
    --templateProperty gcs.bigquery.input.location="CLOUD_STORAGE_PATH" \
    --templateProperty gcs.bigquery.input.format="FORMAT" \
    --templateProperty gcs.bigquery.output.dataset="DATASET" \
    --templateProperty gcs.bigquery.output.table="TABLE" \
    --templateProperty gcs.bigquery.temp.bucket.name="TEMP_BUCKET" \
    --templateProperty gcs.bigquery.temp.table="TEMPVIEW" \
    --templateProperty gcs.bigquery.temp.query="SQL_QUERY"

Windows (PowerShell)

gcloud dataproc batches submit spark `
    --class=com.google.cloud.dataproc.templates.main.DataProcTemplate `
    --version="1.1" `
    --project="PROJECT_ID" `
    --region="REGION" `
    --jars="gs://dataproc-templates-binaries/TEMPLATE_VERSION/java/dataproc-templates.jar" `
    --subnet="SUBNET" `
    --kms-key="KMS_KEY" `
    --service-account="SERVICE_ACCOUNT" `
    --properties="PROPERTY=PROPERTY_VALUE" `
    --labels="LABEL=LABEL_VALUE" `
    -- --template=GCSTOBIGQUERY `
    --templateProperty log.level="LOG_LEVEL" `
    --templateProperty project.id="PROJECT_ID" `
    --templateProperty gcs.bigquery.input.location="CLOUD_STORAGE_PATH" `
    --templateProperty gcs.bigquery.input.format="FORMAT" `
    --templateProperty gcs.bigquery.output.dataset="DATASET" `
    --templateProperty gcs.bigquery.output.table="TABLE" `
    --templateProperty gcs.bigquery.temp.bucket.name="TEMP_BUCKET" `
    --templateProperty gcs.bigquery.temp.table="TEMPVIEW" `
    --templateProperty gcs.bigquery.temp.query="SQL_QUERY"

Windows (cmd.exe)

gcloud dataproc batches submit spark ^
    --class=com.google.cloud.dataproc.templates.main.DataProcTemplate ^
    --version="1.1" ^
    --project="PROJECT_ID" ^
    --region="REGION" ^
    --jars="gs://dataproc-templates-binaries/TEMPLATE_VERSION/java/dataproc-templates.jar" ^
    --subnet="SUBNET" ^
    --kms-key="KMS_KEY" ^
    --service-account="SERVICE_ACCOUNT" ^
    --properties="PROPERTY=PROPERTY_VALUE" ^
    --labels="LABEL=LABEL_VALUE" ^
    -- --template=GCSTOBIGQUERY ^
    --templateProperty log.level="LOG_LEVEL" ^
    --templateProperty project.id="PROJECT_ID" ^
    --templateProperty gcs.bigquery.input.location="CLOUD_STORAGE_PATH" ^
    --templateProperty gcs.bigquery.input.format="FORMAT" ^
    --templateProperty gcs.bigquery.output.dataset="DATASET" ^
    --templateProperty gcs.bigquery.output.table="TABLE" ^
    --templateProperty gcs.bigquery.temp.bucket.name="TEMP_BUCKET" ^
    --templateProperty gcs.bigquery.temp.table="TEMPVIEW" ^
    --templateProperty gcs.bigquery.temp.query="SQL_QUERY"

REST

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

  • PROJECT_ID:必填。IAM 设置中列出的 Google Cloud 项目 ID。
  • REGION:必填。Compute Engine 区域
  • TEMPLATE_VERSION:必填。指定 latest 表示最新的模板版本,指定特定版本的日期,例如 2023-03-17_v0.1.0-beta(访问 gs://dataproc-templates-binaries 或运行 gsutil ls gs://dataproc-templates-binaries 列出可用的模板版本)。
  • CLOUD_STORAGE_PATH:必填。来源 Cloud Storage 路径。

    示例: gs://dataproc-templates/hive_to_cloud_storage_output"

  • FORMAT:必填。输入数据格式。选项:avroparquetcsvjson注意:如果为 avro,您必须将“file:///usr/lib/spark/external/spark-avro.jar”添加到 jars gcloud CLI 标志或 API 字段中。

    示例(file:// 前缀引用 Dataproc Serverless jar 文件)

    --jars=file:///usr/lib/spark/external/spark-avro.jar, [、... 其他 jar 文件]
  • DATASET:必填。目标 BigQuery 数据集。
  • TABLE:必填。目标 BigQuery 表。
  • TEMP_BUCKET:必填。用于在加载到 BigQuery 之前暂存数据的临时 Cloud Storage 存储桶。
  • SUBNET:可选。如果未指定子网,则系统会选择 default 网络中指定 REGION 中的子网。

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

  • TEMPVIEWSQL_QUERY:可选。 您可以使用这两个可选参数在将数据加载到 BigQuery 中时应用 Spark SQL 转换。TEMPVIEW 是临时视图名称,SQL_QUERY 是查询语句。TEMPVIEW 与 SQL_QUERY 中的表名必须匹配。
  • SERVICE_ACCOUNT:可选。如果未提供此项,系统会使用默认 Compute Engine 服务帐号
  • PROPERTYPROPERTY_VALUE:可选。以英文逗号分隔的 Spark 属性=value对列表。
  • LABELLABEL_VALUE:可选。以英文逗号分隔的 label=value 对列表。
  • LOG_LEVEL:可选。日志记录级别。可以是 ALLDEBUGERRORFATALINFOOFFTRACEWARN 中的一个。默认值:INFO
  • KMS_KEY:可选。要用于加密的 Cloud Key Management Service 密钥。如果未指定密钥,系统会使用 Google 管理的密钥对数据进行静态加密

    示例: 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": "1.1",
    "properties": {
      "PROPERTY": "PROPERTY_VALUE"
    }
  },
  "sparkBatch":{
    "mainClass":"com.google.cloud.dataproc.templates.main.DataProcTemplate",
    "args":[
      "--template", "GCSTOBIGQUERY",
      "--templateProperty","log.level=LOG_LEVEL",
      "--templateProperty","project.id=PROJECT_ID",
      "--templateProperty","gcs.bigquery.input.location=CLOUD_STORAGE_PATH",
      "--templateProperty","gcs.bigquery.input.format=FORMAT",
      "--templateProperty","gcs.bigquery.output.dataset=DATASET",
      "--templateProperty","gcs.bigquery.output.table=TABLE",
      "--templateProperty","gcs.bigquery.temp.bucket.name=TEMP_BUCKET",
      "--templateProperty","gcs.bigquery.temp.table=TEMPVIEW",
      "--templateProperty","gcs.bigquery.temp.query=SQL_QUERY"
    ],
    "jarFileUris":[
      "file:///usr/lib/spark/external/spark-avro.jar", "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"
  }
}