Cloud Storage to Cloud Spanner テンプレート

Managed Service for Apache Spark Cloud Storage to Spanner テンプレートを使用して、Cloud Storage から Spanner にデータを抽出します。

テンプレートの使用

gcloud CLI または Managed Service for Apache Spark API を使用してテンプレートを実行します。

gcloud

後述のコマンドデータを使用する前に、 次のように置き換えます。

  • PROJECT_ID: 必須。IAM 設定に載っている プロジェクト ID Google Cloud 。
  • REGION: 必須。Compute Engine のリージョン
  • SUBNET: 省略可。サブネットが指定されていない場合、default ネットワークの指定された REGION のサブネットが選択されます。

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

  • TEMPLATE_VERSION: 必須。最新のテンプレート バージョンまたは特定バージョンの日付(2023-03-17_v0.1.0-beta など)には latest を指定します(gs://dataproc-templates-binaries にアクセスするか、gcloud storage ls gs://dataproc-templates-binaries を実行して、使用可能なテンプレート バージョンを一覧表示します)。
  • CLOUD_STORAGE_INPUT_PATH: 必須。入力データの読み取り元の Cloud Storage パス。

    : gs://example-bucket/example-folder/

  • FORMAT: 必須。入力データ形式オプション: avroparquetorc注: avro の場合、jars gcloud CLI フラグまたは API フィールドに「file:///usr/lib/spark/connector/spark-avro.jar」を追加する必要があります。

    例(file:// 接頭辞は Managed Service 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 ダイアレクト。オプション: googlesqlpostgresql。 デフォルトは googlesql です。
  • MODE: 省略可。Spanner 出力の書き込みモード。オプション: AppendOverwriteIgnoreErrorifExists。デフォルトは ErrorifExists です。
  • PRIMARY_KEY: 必須。Spanner 出力テーブルを作成するときに必要なカンマ区切りの主キー列。
  • BATCHSIZE: 省略可。Spanner テーブルに 1 回のラウンド トリップで挿入するレコード数。デフォルトは 1,000 です。
  • SERVICE_ACCOUNT: 省略可。指定されていない場合は、デフォルトの Compute Engine サービス アカウントが使用されます。
  • PROPERTYPROPERTY_VALUE: 省略可。Spark プロパティ=value ペアのカンマ区切りのリスト。
  • LABELLABEL_VALUE: 省略可。label=value ペアのカンマ区切りのリスト。
  • LOG_LEVEL: 省略可。ロギングのレベル。ALLDEBUGERRORFATALINFOOFFTRACEWARN のいずれかです。デフォルト: 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 設定に載っている プロジェクト ID Google Cloud 。
  • REGION: 必須。Compute Engine のリージョン
  • SUBNET: 省略可。サブネットが指定されていない場合、default ネットワークの指定された REGION のサブネットが選択されます。

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

  • TEMPLATE_VERSION: 必須。最新のテンプレート バージョンまたは特定バージョンの日付(2023-03-17_v0.1.0-beta など)には latest を指定します(gs://dataproc-templates-binaries にアクセスするか、gcloud storage ls gs://dataproc-templates-binaries を実行して、使用可能なテンプレート バージョンを一覧表示します)。
  • CLOUD_STORAGE_INPUT_PATH: 必須。入力データの読み取り元の Cloud Storage パス。

    : gs://example-bucket/example-folder/

  • FORMAT: 必須。入力データ形式オプション: avroparquetorc注: avro の場合、jars gcloud CLI フラグまたは API フィールドに「file:///usr/lib/spark/connector/spark-avro.jar」を追加する必要があります。

    例(file:// 接頭辞は Managed Service 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 ダイアレクト。オプション: googlesqlpostgresql。 デフォルトは googlesql です。
  • MODE: 省略可。Spanner 出力の書き込みモード。オプション: AppendOverwriteIgnoreErrorifExists。デフォルトは ErrorifExists です。
  • PRIMARY_KEY: 必須。Spanner 出力テーブルを作成するときに必要なカンマ区切りの主キー列。
  • BATCHSIZE: 省略可。Spanner テーブルに 1 回のラウンド トリップで挿入するレコード数。デフォルトは 1,000 です。
  • SERVICE_ACCOUNT: 省略可。指定されていない場合は、デフォルトの Compute Engine サービス アカウントが使用されます。
  • PROPERTYPROPERTY_VALUE: 省略可。Spark プロパティ=value ペアのカンマ区切りのリスト。
  • LABELLABEL_VALUE: 省略可。label=value ペアのカンマ区切りのリスト。
  • LOG_LEVEL: 省略可。ロギングのレベル。ALLDEBUGERRORFATALINFOOFFTRACEWARN のいずれかです。デフォルト: 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 メソッドと URL:

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"
  }
}