Cloud Storage 到 Cloud Spanner 範本
使用 Serverless for Apache Spark Cloud Storage to Spanner 範本,將資料從 Cloud Storage 擷取至 Spanner。
使用範本
使用 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] - INSTANCE:必填。Spanner 執行個體 ID。
- DATABASE:必填。Spanner 資料庫 ID。
- TABLE:必填。Spanner 輸出資料表名稱。
- SPANNER_JDBC_DIALECT:必填。Spanner JDBC 方言。
選項:
googlesql
或postgresql
。預設值為googlesql
。 - MODE:選用。Spanner 輸出內容的寫入模式。
選項:
Append
、Overwrite
、Ignore
或ErrorifExists
。預設值為ErrorifExists
。 - PRIMARY_KEY:必填。建立 Spanner 輸出資料表時,需要以半形逗號分隔的主鍵資料欄。
- BATCHSIZE:選用。在一次往返中插入 Spanner 資料表的記錄數。預設值為 1000。
- 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 GCSTOSPANNER \ --templateProperty log.level="LOG_LEVEL" \ --templateProperty project.id="PROJECT_ID" \ --templateProperty gcs.spanner.input.format="FORMAT" \ --templateProperty gcs.spanner.input.location="CLOUD_STORAGE_INPUT_PATH" \ --templateProperty gcs.spanner.output.instance="INSTANCE" \ --templateProperty gcs.spanner.output.database="DATABASE" \ --templateProperty gcs.spanner.output.table="TABLE" \ --templateProperty gcs.spanner.output.saveMode="MODE" \ --templateProperty gcs.spanner.output.primaryKey="PRIMARY_KEY" \ --templateProperty gcs.spanner.output.batchInsertSize="BATCHSIZE" \ --templateProperty spanner.jdbc.dialect="SPANNER_JDBC_DIALECT"
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 GCSTOSPANNER ` --templateProperty log.level="LOG_LEVEL" ` --templateProperty project.id="PROJECT_ID" ` --templateProperty gcs.spanner.input.format="FORMAT" ` --templateProperty gcs.spanner.input.location="CLOUD_STORAGE_INPUT_PATH" ` --templateProperty gcs.spanner.output.instance="INSTANCE" ` --templateProperty gcs.spanner.output.database="DATABASE" ` --templateProperty gcs.spanner.output.table="TABLE" ` --templateProperty gcs.spanner.output.saveMode="MODE" ` --templateProperty gcs.spanner.output.primaryKey="PRIMARY_KEY" ` --templateProperty gcs.spanner.output.batchInsertSize="BATCHSIZE" ` --templateProperty spanner.jdbc.dialect="SPANNER_JDBC_DIALECT"
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 GCSTOSPANNER ^ --templateProperty log.level="LOG_LEVEL" ^ --templateProperty project.id="PROJECT_ID" ^ --templateProperty gcs.spanner.input.format="FORMAT" ^ --templateProperty gcs.spanner.input.location="CLOUD_STORAGE_INPUT_PATH" ^ --templateProperty gcs.spanner.output.instance="INSTANCE" ^ --templateProperty gcs.spanner.output.database="DATABASE" ^ --templateProperty gcs.spanner.output.table="TABLE" ^ --templateProperty gcs.spanner.output.saveMode="MODE" ^ --templateProperty gcs.spanner.output.primaryKey="PRIMARY_KEY" ^ --templateProperty gcs.spanner.output.batchInsertSize="BATCHSIZE" ^ --templateProperty spanner.jdbc.dialect="SPANNER_JDBC_DIALECT"
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] - INSTANCE:必填。Spanner 執行個體 ID。
- DATABASE:必填。Spanner 資料庫 ID。
- TABLE:必填。Spanner 輸出資料表名稱。
- SPANNER_JDBC_DIALECT:必填。Spanner JDBC 方言。
選項:
googlesql
或postgresql
。預設值為googlesql
。 - MODE:選用。Spanner 輸出內容的寫入模式。
選項:
Append
、Overwrite
、Ignore
或ErrorifExists
。預設值為ErrorifExists
。 - PRIMARY_KEY:必填。建立 Spanner 輸出資料表時,需要以半形逗號分隔的主鍵資料欄。
- BATCHSIZE:選用。在一次往返中插入 Spanner 資料表的記錄數。預設值為 1000。
- 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","GCSTOSPANNER", "--templateProperty","project.id=PROJECT_ID", "--templateProperty","log.level=LOG_LEVEL", "--templateProperty","gcs.spanner.input.format=FORMAT", "--templateProperty","gcs.spanner.input.location=CLOUD_STORAGE_INPUT_PATH", "--templateProperty","gcs.spanner.output.instance=INSTANCE", "--templateProperty","gcs.spanner.output.database=DATABASE", "--templateProperty","gcs.spanner.output.table=TABLE", "--templateProperty","gcs.spanner.output.saveMode=MODE", "--templateProperty","gcs.spanner.output.primaryKey=PRIMARY_KEY", "--templateProperty","gcs.spanner.output.batchInsertSize=BATCHSIZE", "--templateProperty spanner.jdbc.dialect=SPANNER_JDBC_DIALECT" ], "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" } }