Cloud Storage 到 Cloud Storage 範本
使用 Serverless for Apache Spark Cloud Storage to Cloud Storage 範本,從 Cloud Storage 擷取資料至 Cloud Storage。
使用範本
使用 gcloud CLI 或 Dataproc API 執行範本。
gcloud
使用下方的任何指令資料之前,請先替換以下項目:
- PROJECT_ID:必填。IAM 設定中列出的 Google Cloud 專案 ID。
- REGION:必填。Compute Engine 區域。
- SUBNET:選用。如未指定子網路,系統會選取
default
網路中指定 REGION 的子網路。示例:
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
」新增至jars
gcloud 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
」新增至jars
gcloud CLI 旗標或 API 欄位。範例 (
file://
前置字串參照 Serverless for Apache Spark JAR 檔案):--jars=file:///usr/lib/spark/connector/spark-avro.jar,
[ ... other jars] -
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
網路中指定 REGION 的子網路。示例:
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
」新增至jars
gcloud 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
」新增至jars
gcloud CLI 旗標或 API 欄位。範例 (
file://
前置字串參照 Serverless for Apache Spark JAR 檔案):--jars=file:///usr/lib/spark/connector/spark-avro.jar,
[ ... other jars] -
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" } }