Cloud Storage to BigQuery 模板
使用“Serverless for Apache Spark 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 或运行gcloud storage ls gs://dataproc-templates-binaries以列出可用的模板版本)。 - CLOUD_STORAGE_PATH:必填。源 Cloud Storage 路径。
示例:
gs://dataproc-templates/hive_to_cloud_storage_output" - FORMAT:必填。输入数据格式。选项:
avro、parquet、csv或json。 注意:如果值为avro,您必须将“file:///usr/lib/spark/connector/spark-avro.jar”添加到jarsgcloud CLI 标志或 API 字段中。示例(
file://前缀引用 Serverless for Apache Spark JAR 文件):--jars=file:///usr/lib/spark/connector/spark-avro.jar,[, ... 其他 jar] - DATASET:必填。目标 BigQuery 数据集。
- TABLE:必填。目标 BigQuery 表。
- TEMP_BUCKET:必填。用于在将数据加载到 BigQuery 之前暂存数据的临时 Cloud Storage 存储桶。
- SUBNET:可选。如果未指定子网,系统会选择
default网络中指定区域的子网。示例:
projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET_NAME - TEMPVIEW 和 SQL_QUERY:可选。 您可以使用这两个可选参数在将数据加载到 BigQuery 时应用 Spark SQL 转换。其中,TEMPVIEW 是临时视图名称,SQL_QUERY 是查询语句。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="1.2" \ --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.2" ` --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.2" ^ --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 或运行gcloud storage ls gs://dataproc-templates-binaries以列出可用的模板版本)。 - CLOUD_STORAGE_PATH:必填。源 Cloud Storage 路径。
示例:
gs://dataproc-templates/hive_to_cloud_storage_output" - FORMAT:必填。输入数据格式。选项:
avro、parquet、csv或json。 注意:如果值为avro,您必须将“file:///usr/lib/spark/connector/spark-avro.jar”添加到jarsgcloud CLI 标志或 API 字段中。示例(
file://前缀引用 Serverless for Apache Spark JAR 文件):--jars=file:///usr/lib/spark/connector/spark-avro.jar,[, ... 其他 jar] - DATASET:必填。目标 BigQuery 数据集。
- TABLE:必填。目标 BigQuery 表。
- TEMP_BUCKET:必填。用于在将数据加载到 BigQuery 之前暂存数据的临时 Cloud Storage 存储桶。
- SUBNET:可选。如果未指定子网,系统会选择
default网络中指定区域的子网。示例:
projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET_NAME - TEMPVIEW 和 SQL_QUERY:可选。 您可以使用这两个可选参数在将数据加载到 BigQuery 时应用 Spark SQL 转换。其中,TEMPVIEW 是临时视图名称,SQL_QUERY 是查询语句。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": "1.2",
"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/connector/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"
}
}