Cloud Storage to BigQuery テンプレート

Dataproc Serverless Cloud Storage to BigQuery テンプレートを使用して、Cloud Storage から BigQuery にデータを抽出します。

テンプレートの使用

gcloud CLI または Dataproc API を使用してテンプレートを実行します。

gcloudREST

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

  • PROJECT_ID: 必須。IAM 設定に載っている Google Cloud プロジェクト ID。
  • 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:// 接頭辞は Dataproc Serverless 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 が所有し、Google が管理する鍵を使用して、保存時に暗号化されます。

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

リクエストのデータを使用する前に、次のように置き換えます。

  • PROJECT_ID: 必須。IAM 設定に載っている Google Cloud プロジェクト ID。
  • 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:// 接頭辞は Dataproc Serverless 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 が所有し、Google が管理する鍵を使用して、保存時に暗号化されます。

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

リクエストを送信するには、次のいずれかのオプションを展開します。

リクエスト本文を request.json という名前のファイルに保存して、次のコマンドを実行します。

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://dataproc.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/batches"

リクエスト本文を request.json という名前のファイルに保存して、次のコマンドを実行します。

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://dataproc.googleapis.com/v1/projects/PROJECT_ID/locations/REGION/batches" | Select-Object -Expand Content

次のような 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"
  }
}