Cloud Pub/Sub 到 Cloud Storage 範本

使用「Serverless for Apache Spark Cloud Pub/Sub to Cloud Storage」範本,從 Pub/Sub 擷取資料至 Cloud Storage。

使用範本

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

gcloud

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

  • PROJECT_ID:必填。IAM 設定中列出的 Google Cloud 專案 ID。
  • REGION:必填。Compute Engine 區域
  • SUBNET:選用。如未指定子網路,系統會選取 default 網路中指定 REGION 的子網路。

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

  • TEMPLATE_VERSION:必填。指定 latest 代表最新範本版本,或指定特定版本的日期,例如 2023-03-17_v0.1.0-beta (請前往 gs://dataproc-templates-binaries 或執行 gcloud storage ls gs://dataproc-templates-binaries 列出可用的範本版本)。
  • PUBSUB_SUBSCRIPTION_PROJECT_ID:必填。身分與存取權管理設定中列出的專案 ID,其中包含要讀取的輸入 Pub/Sub 訂閱項目。 Google Cloud
  • SUBSCRIPTION:必填。Pub/Sub 訂閱項目名稱。
  • CLOUD_STORAGE_OUTPUT_BUCKET_NAME:必填。輸出內容的儲存位置 Cloud Storage bucket 名稱。

    注意:輸出檔案會儲存在 bucket 內的 output/ 資料夾中。

  • FORMAT:必填。輸出資料格式。選項:avrojson

    注意:如果 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]
  • TIMEOUT:選用。串流終止前的時間 (以毫秒為單位)。預設為 60000
  • DURATION:選用。寫入 Cloud Storage 的頻率 (以秒為單位)。預設值為 15 秒
  • NUM_RECEIVERS:選用。從 Pub/Sub 訂閱項目平行讀取的串流數量。預設為 5
  • BATCHSIZE:選用。單次往返 Cloud Storage 要插入的記錄數。預設為 1000
  • 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=PUBSUBTOGCS \
    --templateProperty log.level="LOG_LEVEL" \
    --templateProperty pubsubtogcs.input.project.id="PUBSUB_SUBSCRIPTION_PROJECT_ID" \
    --templateProperty pubsubtogcs.input.subscription="SUBSCRIPTION" \
    --templateProperty pubsubtogcs.gcs.bucket.name="CLOUD_STORAGE_OUTPUT_BUCKET_NAME" \
    --templateProperty pubsubtogcs.gcs.output.data.format="FORMAT" \
    --templateProperty pubsubtogcs.timeout.ms="TIMEOUT" \
    --templateProperty pubsubtogcs.streaming.duration.seconds="DURATION" \
    --templateProperty pubsubtogcs.total.receivers="NUM_RECEIVERS" \
    --templateProperty pubsubtogcs.batch.size="BATCHSIZE"

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=PUBSUBTOGCS `
    --templateProperty log.level="LOG_LEVEL" `
    --templateProperty pubsubtogcs.input.project.id="PUBSUB_SUBSCRIPTION_PROJECT_ID" `
    --templateProperty pubsubtogcs.input.subscription="SUBSCRIPTION" `
    --templateProperty pubsubtogcs.gcs.bucket.name="CLOUD_STORAGE_OUTPUT_BUCKET_NAME" `
    --templateProperty pubsubtogcs.gcs.output.data.format="FORMAT" `
    --templateProperty pubsubtogcs.timeout.ms="TIMEOUT" `
    --templateProperty pubsubtogcs.streaming.duration.seconds="DURATION" `
    --templateProperty pubsubtogcs.total.receivers="NUM_RECEIVERS" `
    --templateProperty pubsubtogcs.batch.size="BATCHSIZE"

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=PUBSUBTOGCS ^
    --templateProperty log.level="LOG_LEVEL" ^
    --templateProperty pubsubtogcs.input.project.id="PUBSUB_SUBSCRIPTION_PROJECT_ID" ^
    --templateProperty pubsubtogcs.input.subscription="SUBSCRIPTION" ^
    --templateProperty pubsubtogcs.gcs.bucket.name="CLOUD_STORAGE_OUTPUT_BUCKET_NAME" ^
    --templateProperty pubsubtogcs.gcs.output.data.format="FORMAT" ^
    --templateProperty pubsubtogcs.timeout.ms="TIMEOUT" ^
    --templateProperty pubsubtogcs.streaming.duration.seconds="DURATION" ^
    --templateProperty pubsubtogcs.total.receivers="NUM_RECEIVERS" ^
    --templateProperty pubsubtogcs.batch.size="BATCHSIZE"

REST

使用任何要求資料之前,請先替換以下項目:

  • PROJECT_ID:必填。IAM 設定中列出的 Google Cloud 專案 ID。
  • REGION:必填。Compute Engine 區域
  • SUBNET:選用。如未指定子網路,系統會選取 default 網路中指定 REGION 的子網路。

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

  • TEMPLATE_VERSION:必填。指定 latest 代表最新範本版本,或指定特定版本的日期,例如 2023-03-17_v0.1.0-beta (請前往 gs://dataproc-templates-binaries 或執行 gcloud storage ls gs://dataproc-templates-binaries 列出可用的範本版本)。
  • PUBSUB_SUBSCRIPTION_PROJECT_ID:必填。身分與存取權管理設定中列出的專案 ID,其中包含要讀取的輸入 Pub/Sub 訂閱項目。 Google Cloud
  • SUBSCRIPTION:必填。Pub/Sub 訂閱項目名稱。
  • CLOUD_STORAGE_OUTPUT_BUCKET_NAME:必填。輸出內容的儲存位置 Cloud Storage bucket 名稱。

    注意:輸出檔案會儲存在 bucket 內的 output/ 資料夾中。

  • FORMAT:必填。輸出資料格式。選項:avrojson

    注意:如果 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]
  • TIMEOUT:選用。串流終止前的時間 (以毫秒為單位)。預設為 60000
  • DURATION:選用。寫入 Cloud Storage 的頻率 (以秒為單位)。預設值為 15 秒
  • NUM_RECEIVERS:選用。從 Pub/Sub 訂閱項目平行讀取的串流數量。預設為 5
  • BATCHSIZE:選用。單次往返 Cloud Storage 要插入的記錄數。預設為 1000
  • 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","PUBSUBTOGCS",
      "--templateProperty","log.level=LOG_LEVEL",
      "--templateProperty","pubsubtogcs.input.project.id=PUBSUB_SUBSCRIPTION_PROJECT_ID",
      "--templateProperty","pubsubtogcs.input.subscription=SUBSCRIPTION",
      "--templateProperty","pubsubtogcs.gcs.bucket.name=CLOUD_STORAGE_OUTPUT_BUCKET_NAME",
      "--templateProperty","pubsubtogcs.gcs.output.data.format=FORMAT",
      "--templateProperty","pubsubtogcs.timeout.ms=TIMEOUT",
      "--templateProperty","pubsubtogcs.streaming.duration.seconds=DURATION",
      "--templateProperty","pubsubtogcs.total.receivers=NUM_RECEIVERS",
      "--templateProperty","pubsubtogcs.batch.size=BATCHSIZE"
    ],
    "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"
  }
}