Cloud Storage to BigQuery テンプレート

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

テンプレートの使用

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

gcloud

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

  • PROJECT_ID: 必須。IAM 設定に載っている プロジェクト ID。 Google Cloud
  • REGION: 必須。Compute Engine のリージョン
  • TEMPLATE_VERSION: 必須。最新のテンプレート バージョンまたは特定バージョンの日付(2023-03-17_v0.1.0-beta など)には latest を指定します(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: 必須。入力データ形式選択肢: avroparquetcsvjson注: 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]
  • DATASET: 必須。宛先の BigQuery データセット
  • TABLE: 必須。宛先の BigQuery テーブル
  • TEMP_BUCKET: 必須。BigQuery に読み込む前にデータをステージングするために使用される一時的な Cloud Storage バケット。
  • SUBNET: 省略可。サブネットが指定されていない場合、default ネットワークの指定された REGION のサブネットが選択されます。

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

  • TEMPVIEWSQL_QUERY: 省略可。 これら 2 つのオプションのパラメータを使用して、BigQuery にデータを読み込むときに Spark SQL 変換を適用できます。 TEMPVIEW は一時的なビュー名で、SQL_QUERY はクエリ ステートメントです。TEMPVIEW と SQL_QUERY のテーブル名は一致する必要があります。
  • 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=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 設定に載っている プロジェクト ID。 Google Cloud
  • REGION: 必須。Compute Engine のリージョン
  • TEMPLATE_VERSION: 必須。最新のテンプレート バージョンまたは特定バージョンの日付(2023-03-17_v0.1.0-beta など)には latest を指定します(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: 必須。入力データ形式選択肢: avroparquetcsvjson注: 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]
  • DATASET: 必須。宛先の BigQuery データセット
  • TABLE: 必須。宛先の BigQuery テーブル
  • TEMP_BUCKET: 必須。BigQuery に読み込む前にデータをステージングするために使用される一時的な Cloud Storage バケット。
  • SUBNET: 省略可。サブネットが指定されていない場合、default ネットワークの指定された REGION のサブネットが選択されます。

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

  • TEMPVIEWSQL_QUERY: 省略可。 これら 2 つのオプションのパラメータを使用して、BigQuery にデータを読み込むときに Spark SQL 変換を適用できます。 TEMPVIEW は一時的なビュー名で、SQL_QUERY はクエリ ステートメントです。TEMPVIEW と SQL_QUERY のテーブル名は一致する必要があります。
  • 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", "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"
  }
}