Cloud Storage to Cloud Storage 模板
使用“无服务器 Apache Spark Cloud Storage 到 Cloud Storage”模板将数据从 Cloud Storage 提取到 Cloud Storage。
使用模板
使用 gcloud CLI 或 Dataproc API 运行模板。
gcloud
在使用下面的命令数据之前,请先进行以下替换:
- PROJECT_ID:必填。IAM 设置中列出的 Google Cloud 项目 ID。
- REGION:必填。Compute Engine 区域。
- SUBNET:可选。如果未指定子网,则会选择
default网络中指定区域的子网。示例:
projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET_NAME - TEMPLATE_VERSION:必填。指定
latest以使用最新模板版本,或指定特定版本的日期,例如2023-03-17_v0.1.0-beta(访问 gs://dataproc-templates-binaries 或运行gcloud storage ls gs://dataproc-templates-binaries以列出可用的模板版本)。 - CLOUD_STORAGE_INPUT_PATH:必填。将从中读取输入数据的 Cloud Storage 路径。
示例:
gs://example-bucket/example-folder/ - FORMAT:必填。输入数据格式。选项:
avro、parquet或orc。 注意:如果值为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,[ ... other jars] -
CLOUD_STORAGE_OUTPUT_PATH:
必需。将存储输出的 Cloud Storage 路径。
示例:
gs://example-bucket/example-folder/ -
OUTPUT_FILE_FORMAT:
必需。输出数据格式。选项:
avro、csv、parquet、json或orc。 注意:如果值为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 文件] -
MODE:
必需。Cloud Storage 输出的写入模式。
选项:
Append、Overwrite、Ignore或ErrorIfExists。 - TEMP_TABLE 和 TEMP_QUERY:可选。 您可以使用这两个可选参数在将数据加载到 Cloud Storage 时应用 Spark SQL 转换。 其中,TEMP_TABLE 是临时视图名称,TEMP_QUERY 是查询语句。TEMP_TABLE 和 TEMP_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,file:///usr/lib/spark/connector/spark-avro.jar" \ --subnet="SUBNET" \ --kms-key="KMS_KEY" \ --service-account="SERVICE_ACCOUNT" \ --properties="PROPERTY=PROPERTY_VALUE" \ --labels="LABEL=LABEL_VALUE" \ -- --template=GCSTOGCS \ --templateProperty log.level="LOG_LEVEL" \ --templateProperty project.id="PROJECT_ID" \ --templateProperty gcs.gcs.input.location="CLOUD_STORAGE_INPUT_PATH" \ --templateProperty gcs.gcs.input.format="INPUT_FILE_FORMAT" \ --templateProperty gcs.gcs.output.location="CLOUD_STORAGE_OUTPUT_PATH" \ --templateProperty gcs.gcs.output.format="OUTPUT_FILE_FORMAT" \ --templateProperty gcs.gcs.write.mode="MODE" \ --templateProperty gcs.gcs.temp.table="TEMP_TABLE" \ --templateProperty gcs.gcs.temp.query="TEMP_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,file:///usr/lib/spark/connector/spark-avro.jar" ` --subnet="SUBNET" ` --kms-key="KMS_KEY" ` --service-account="SERVICE_ACCOUNT" ` --properties="PROPERTY=PROPERTY_VALUE" ` --labels="LABEL=LABEL_VALUE" ` -- --template=GCSTOGCS ` --templateProperty log.level="LOG_LEVEL" ` --templateProperty project.id="PROJECT_ID" ` --templateProperty gcs.gcs.input.location="CLOUD_STORAGE_INPUT_PATH" ` --templateProperty gcs.gcs.input.format="INPUT_FILE_FORMAT" ` --templateProperty gcs.gcs.output.location="CLOUD_STORAGE_OUTPUT_PATH" ` --templateProperty gcs.gcs.output.format="OUTPUT_FILE_FORMAT" ` --templateProperty gcs.gcs.write.mode="MODE" ` --templateProperty gcs.gcs.temp.table="TEMP_TABLE" ` --templateProperty gcs.gcs.temp.query="TEMP_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,file:///usr/lib/spark/connector/spark-avro.jar" ^ --subnet="SUBNET" ^ --kms-key="KMS_KEY" ^ --service-account="SERVICE_ACCOUNT" ^ --properties="PROPERTY=PROPERTY_VALUE" ^ --labels="LABEL=LABEL_VALUE" ^ -- --template=GCSTOGCS ^ --templateProperty log.level="LOG_LEVEL" ^ --templateProperty project.id="PROJECT_ID" ^ --templateProperty gcs.gcs.input.location="CLOUD_STORAGE_INPUT_PATH" ^ --templateProperty gcs.gcs.input.format="INPUT_FILE_FORMAT" ^ --templateProperty gcs.gcs.output.location="CLOUD_STORAGE_OUTPUT_PATH" ^ --templateProperty gcs.gcs.output.format="OUTPUT_FILE_FORMAT" ^ --templateProperty gcs.gcs.write.mode="MODE" ^ --templateProperty gcs.gcs.temp.table="TEMP_TABLE" ^ --templateProperty gcs.gcs.temp.query="TEMP_QUERY"
REST
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:必填。IAM 设置中列出的 Google Cloud 项目 ID。
- REGION:必填。Compute Engine 区域。
- SUBNET:可选。如果未指定子网,则会选择
default网络中指定区域的子网。示例:
projects/PROJECT_ID/regions/REGION/subnetworks/SUBNET_NAME - TEMPLATE_VERSION:必填。指定
latest以使用最新模板版本,或指定特定版本的日期,例如2023-03-17_v0.1.0-beta(访问 gs://dataproc-templates-binaries 或运行gcloud storage ls gs://dataproc-templates-binaries以列出可用的模板版本)。 - CLOUD_STORAGE_INPUT_PATH:必填。将从中读取输入数据的 Cloud Storage 路径。
示例:
gs://example-bucket/example-folder/ - FORMAT:必填。输入数据格式。选项:
avro、parquet或orc。 注意:如果值为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,[ ... other jars] -
CLOUD_STORAGE_OUTPUT_PATH:
必需。将存储输出的 Cloud Storage 路径。
示例:
gs://example-bucket/example-folder/ -
OUTPUT_FILE_FORMAT:
必需。输出数据格式。选项:
avro、csv、parquet、json或orc。 注意:如果值为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 文件] -
MODE:
必需。Cloud Storage 输出的写入模式。
选项:
Append、Overwrite、Ignore或ErrorIfExists。 - TEMP_TABLE 和 TEMP_QUERY:可选。 您可以使用这两个可选参数在将数据加载到 Cloud Storage 时应用 Spark SQL 转换。 其中,TEMP_TABLE 是临时视图名称,TEMP_QUERY 是查询语句。TEMP_TABLE 和 TEMP_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","GCSTOGCS",
"--templateProperty","project.id=PROJECT_ID",
"--templateProperty","log.level=LOG_LEVEL",
"--templateProperty","gcs.gcs.input.location=CLOUD_STORAGE_INPUT_PATH",
"--templateProperty","gcs.gcs.input.format=INPUT_FILE_FORMAT",
"--templateProperty","gcs.gcs.output.location=CLOUD_STORAGE_OUTPUT_PATH",
"--templateProperty","gcs.gcs.output.format=OUTPUT_FILE_FORMAT",
"--templateProperty","gcs.gcs.write.mode=MODE",
"--templateProperty","gcs.gcs.temp.table=TEMP_TABLE",
"--templateProperty","gcs.gcs.temp.query=TEMP_QUERY"
],
"jarFileUris":[
"gs://dataproc-templates-binaries/TEMPLATE_VERSION/java/dataproc-templates.jar",
"file:///usr/lib/spark/connector/spark-avro.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"
}
}