Cloud Pub/Sub to Cloud Storage 템플릿

서버리스 Dataproc 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 네트워크의 지정된 리전에 있는 서브넷이 선택됩니다.

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

  • TEMPLATE_VERSION: 필수. 최신 템플릿 버전 또는 특정 버전의 날짜로 latest를 지정합니다(예: 2023-03-17_v0.1.0-beta). 사용 가능한 템플릿 버전을 나열하려면 gs://dataproc-templates-binaries를 방문하거나 gsutil ls gs://dataproc-templates-binaries를 실행하세요.
  • PUBSUB_SUBSCRIPTION_PROJECT_ID: 필수. 읽어올 입력 Pub/Sub 구독을 포함하는 IAM 설정에 나열된 Google Cloud 프로젝트 ID입니다.
  • SUBSCRIPTION: 필수. Pub/Sub 구독 이름입니다.
  • CLOUD_STORAGE_OUTPUT_BUCKET_NAME: 필수. 출력을 저장할 Cloud Storage 버킷 이름입니다.

    참고: 출력 파일은 버킷 안의 output/ 폴더에 저장됩니다.

  • FORMAT: 필수. 출력 데이터 형식입니다. 옵션: avro 또는 json.

    참고: avro의 경우 'file:///usr/lib/spark/external/spark-avro.jar'을 jars gcloud CLI 플래그 또는 API 필드에 추가해야 합니다.

    예시(file:// 접두사는 Dataproc Serverless jar 파일을 참조):

    --jars=file:///usr/lib/spark/external/spark-avro.jar, [ ... 기타 jar]
  • 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: 선택사항. 로깅 수준입니다. ALL, DEBUG, ERROR, FATAL, INFO, OFF, TRACE, WARN 중 하나일 수 있습니다. 기본값은 INFO입니다.
  • KMS_KEY: 선택사항. 암호화에 사용할 Cloud Key Management Service 키입니다. 키를 지정하지 않으면 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.1" \
    --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.1" `
    --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.1" ^
    --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 네트워크의 지정된 리전에 있는 서브넷이 선택됩니다.

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

  • TEMPLATE_VERSION: 필수. 최신 템플릿 버전 또는 특정 버전의 날짜로 latest를 지정합니다(예: 2023-03-17_v0.1.0-beta). 사용 가능한 템플릿 버전을 나열하려면 gs://dataproc-templates-binaries를 방문하거나 gsutil ls gs://dataproc-templates-binaries를 실행하세요.
  • PUBSUB_SUBSCRIPTION_PROJECT_ID: 필수. 읽어올 입력 Pub/Sub 구독을 포함하는 IAM 설정에 나열된 Google Cloud 프로젝트 ID입니다.
  • SUBSCRIPTION: 필수. Pub/Sub 구독 이름입니다.
  • CLOUD_STORAGE_OUTPUT_BUCKET_NAME: 필수. 출력을 저장할 Cloud Storage 버킷 이름입니다.

    참고: 출력 파일은 버킷 안의 output/ 폴더에 저장됩니다.

  • FORMAT: 필수. 출력 데이터 형식입니다. 옵션: avro 또는 json.

    참고: avro의 경우 'file:///usr/lib/spark/external/spark-avro.jar'을 jars gcloud CLI 플래그 또는 API 필드에 추가해야 합니다.

    예시(file:// 접두사는 Dataproc Serverless jar 파일을 참조):

    --jars=file:///usr/lib/spark/external/spark-avro.jar, [ ... 기타 jar]
  • 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: 선택사항. 로깅 수준입니다. ALL, DEBUG, ERROR, FATAL, INFO, OFF, TRACE, WARN 중 하나일 수 있습니다. 기본값은 INFO입니다.
  • KMS_KEY: 선택사항. 암호화에 사용할 Cloud Key Management Service 키입니다. 키를 지정하지 않으면 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.1",
    "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/external/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"
  }
}