從 Hive 至 BigQuery 範本

使用 Serverless for Apache Spark Hive to BigQuery 範本,將資料從 Hive 擷取至 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,列出可用的範本版本)。
  • SUBNET:選用。如未指定子網路,系統會選取 default 網路中指定 REGION 的子網路。

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

  • HOSTPORT:必填。 來源 Hive 資料庫主機的主機名稱或 IP 位址和通訊埠。

    示例:10.0.0.33:9083

  • DATASET:必填。BigQuery 輸出資料集名稱。
  • TABLE:必填。BigQuery 輸出資料表名稱。
  • QUERY:必填。從 Hive 擷取資料的查詢。
  • TEMP_BUCKET:必填。Cloud Storage 值區名稱 (只需指定該值區名稱即可)。這個 bucket 用於 BigQuery 載入作業。
  • MODE:必填。BigQuery 輸出內容的寫入模式。 選項:AppendOverwriteIgnoreErrorIfExists
  • TEMPVIEWTEMPVIEW_SQL_QUERY:選用。 您可以使用這兩個選用參數,在將資料載入 BigQuery 時套用 Spark SQL 轉換。 TEMPVIEW 是臨時檢視區塊名稱,TEMPVIEW_SQL_QUERY 則是查詢陳述式。TEMPVIEW 和 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" \
    --service-account="SERVICE_ACCOUNT" \
    --properties="spark.hadoop.hive.metastore.uris=thrift://HOST:PORT,PROPERTY=PROPERTY_VALUE" \
    --kms-key="KMS_KEY" \
    --labels="LABEL=LABEL_VALUE" \
    -- --template HIVETOBIGQUERY \
    --templateProperty log.level="LOG_LEVEL" \
    --templateProperty hivetobq.bigquery.location="PROJECT_ID.DATASET.TABLE" \
    --templateProperty hivetobq.sql="QUERY" \
    --templateProperty hivetobq.temp.gcs.bucket="TEMP_BUCKET" \
    --templateProperty hivetobq.write.mode="MODE" \
    --templateProperty hivetobq.temp.table="TEMPVIEW" \
    --templateProperty hivetobq.temp.query="TEMPVIEW_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" `
    --service-account="SERVICE_ACCOUNT" `
    --properties="spark.hadoop.hive.metastore.uris=thrift://HOST:PORT,PROPERTY=PROPERTY_VALUE" `
    --kms-key="KMS_KEY" `
    --labels="LABEL=LABEL_VALUE" `
    -- --template HIVETOBIGQUERY `
    --templateProperty log.level="LOG_LEVEL" `
    --templateProperty hivetobq.bigquery.location="PROJECT_ID.DATASET.TABLE" `
    --templateProperty hivetobq.sql="QUERY" `
    --templateProperty hivetobq.temp.gcs.bucket="TEMP_BUCKET" `
    --templateProperty hivetobq.write.mode="MODE" `
    --templateProperty hivetobq.temp.table="TEMPVIEW" `
    --templateProperty hivetobq.temp.query="TEMPVIEW_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" ^
    --service-account="SERVICE_ACCOUNT" ^
    --properties="spark.hadoop.hive.metastore.uris=thrift://HOST:PORT,PROPERTY=PROPERTY_VALUE" ^
    --kms-key="KMS_KEY" ^
    --labels="LABEL=LABEL_VALUE" ^
    -- --template HIVETOBIGQUERY ^
    --templateProperty log.level="LOG_LEVEL" ^
    --templateProperty hivetobq.bigquery.location="PROJECT_ID.DATASET.TABLE" ^
    --templateProperty hivetobq.sql="QUERY" ^
    --templateProperty hivetobq.temp.gcs.bucket="TEMP_BUCKET" ^
    --templateProperty hivetobq.write.mode="MODE" ^
    --templateProperty hivetobq.temp.table="TEMPVIEW" ^
    --templateProperty hivetobq.temp.query="TEMPVIEW_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,列出可用的範本版本)。
  • SUBNET:選用。如未指定子網路,系統會選取 default 網路中指定 REGION 的子網路。

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

  • HOSTPORT:必填。 來源 Hive 資料庫主機的主機名稱或 IP 位址和通訊埠。

    示例:10.0.0.33:9083

  • DATASET:必填。BigQuery 輸出資料集名稱。
  • TABLE:必填。BigQuery 輸出資料表名稱。
  • QUERY:必填。從 Hive 擷取資料的查詢。
  • TEMP_BUCKET:必填。Cloud Storage 值區名稱 (只需指定該值區名稱即可)。這個 bucket 用於 BigQuery 載入作業。
  • MODE:必填。BigQuery 輸出內容的寫入模式。 選項:AppendOverwriteIgnoreErrorIfExists
  • TEMPVIEWTEMPVIEW_SQL_QUERY:選用。 您可以使用這兩個選用參數,在將資料載入 BigQuery 時套用 Spark SQL 轉換。 TEMPVIEW 是臨時檢視區塊名稱,TEMPVIEW_SQL_QUERY 則是查詢陳述式。TEMPVIEW 和 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": {
      "spark.hadoop.hive.metastore.uris":"thrift://HOST:PORT",
      "PROPERTY": "PROPERTY_VALUE"
    }
  },
  "sparkBatch":{
    "mainClass":"com.google.cloud.dataproc.templates.main.DataProcTemplate",
    "args":[
      "--template","HIVETOBIGQUERY",
      "--templateProperty","log.level=LOG_LEVEL",
      "--templateProperty","hivetobq.bigquery.location=PROJECT_ID.DATASET.TABLE",
      "--templateProperty","hivetobq.sql=QUERY",
      "--templateProperty","hivetobq.temp.gcs.bucket=TEMP_BUCKET",
      "--templateProperty","hivetobq.write.mode=MODE",
      "--templateProperty","hivetobq.temp.table=TEMPVIEW",
      "--templateProperty","hivetobq.temp.query=TEMPVIEW_SQL_QUERY"
    ],
    "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"
  }
}