Cloud Storage 到 BigQuery 範本

使用 Serverless for Apache Spark Cloud Storage to BigQuery 範本,將資料從 Cloud Storage 擷取至 BigQuery。

使用範本

使用 gcloud CLI 或 Dataproc API 執行範本。

gcloud

使用下方的任何指令資料之前,請先替換以下項目:

  • PROJECT_ID:必填。IAM 設定中列出的 Google Cloud 專案 ID。
  • REGION:必填。Compute Engine 區域
  • TEMPLATE_VERSION:必填。指定 latest 代表最新範本版本,或指定特定版本的日期,例如 2023-03-17_v0.1.0-beta (請前往 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,則必須將「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]
  • DATASET:必填。目的地 BigQuery 資料集。
  • TABLE:必填。目的地 BigQuery 資料表。
  • TEMP_BUCKET:必填。暫時的 Cloud Storage 值區,用於在載入 BigQuery 前暫存資料。
  • SUBNET:選用。如未指定子網路,系統會選取 default 網路中指定 REGION 的子網路。

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

  • TEMPVIEWSQL_QUERY:選用。 您可以使用這兩個選用參數,在將資料載入 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 設定中列出的 Google Cloud 專案 ID。
  • REGION:必填。Compute Engine 區域
  • TEMPLATE_VERSION:必填。指定 latest 代表最新範本版本,或指定特定版本的日期,例如 2023-03-17_v0.1.0-beta (請前往 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,則必須將「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]
  • DATASET:必填。目的地 BigQuery 資料集。
  • TABLE:必填。目的地 BigQuery 資料表。
  • TEMP_BUCKET:必填。暫時的 Cloud Storage 值區,用於在載入 BigQuery 前暫存資料。
  • SUBNET:選用。如未指定子網路,系統會選取 default 網路中指定 REGION 的子網路。

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

  • TEMPVIEWSQL_QUERY:選用。 您可以使用這兩個選用參數,在將資料載入 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 方法和網址:

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