Mem-build dan menjalankan Template Flex


Dengan Template Flex Dataflow, Anda dapat mengemas pipeline Dataflow untuk deployment. Tutorial ini menunjukkan cara membuat Template Flex Dataflow, lalu menjalankan tugas Dataflow menggunakan template tersebut.

Tujuan

  • Buat Template Flex Dataflow.
  • Gunakan template untuk menjalankan tugas Dataflow.

Biaya

Dalam dokumen ini, Anda akan menggunakan komponen Google Cloudyang dapat ditagih berikut:

Untuk membuat perkiraan biaya berdasarkan proyeksi penggunaan Anda, gunakan kalkulator harga.

Pengguna Google Cloud baru mungkin memenuhi syarat untuk mendapatkan uji coba gratis.

Setelah menyelesaikan tugas yang dijelaskan dalam dokumen ini, Anda dapat menghindari penagihan berkelanjutan dengan menghapus resource yang Anda buat. Untuk mengetahui informasi selengkapnya, lihat Pembersihan.

Sebelum memulai

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.

  4. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    gcloud init
  5. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Dataflow, Compute Engine, Logging, Cloud Storage, Cloud Storage JSON, Resource Manager, Artifact Registry, and Cloud Build API:

    gcloud services enable dataflow compute_component logging storage_component storage_api cloudresourcemanager.googleapis.com artifactregistry.googleapis.com cloudbuild.googleapis.com
  8. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

  9. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/iam.serviceAccountUser

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: your project ID.
    • USER_IDENTIFIER: the identifier for your user account—for example, [email protected].
    • ROLE: the IAM role that you grant to your user account.
  10. Install the Google Cloud CLI.

  11. Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.

  12. Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:

    gcloud init
  13. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  14. Verify that billing is enabled for your Google Cloud project.

  15. Enable the Dataflow, Compute Engine, Logging, Cloud Storage, Cloud Storage JSON, Resource Manager, Artifact Registry, and Cloud Build API:

    gcloud services enable dataflow compute_component logging storage_component storage_api cloudresourcemanager.googleapis.com artifactregistry.googleapis.com cloudbuild.googleapis.com
  16. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

  17. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/iam.serviceAccountUser

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: your project ID.
    • USER_IDENTIFIER: the identifier for your user account—for example, [email protected].
    • ROLE: the IAM role that you grant to your user account.
  18. Beri peran ke akun layanan default Compute Engine Anda. Jalankan perintah berikut satu kali untuk setiap peran IAM berikut:

    • roles/dataflow.admin
    • roles/dataflow.worker
    • roles/storage.objectAdmin
    • roles/artifactregistry.writer
    gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:PROJECT_NUMBER[email protected]" --role=SERVICE_ACCOUNT_ROLE

    Ganti kode berikut:

    • PROJECT_ID: project ID Anda
    • PROJECT_NUMBER nomor project
    • SERVICE_ACCOUNT_ROLE: setiap peran individual
  19. Menyiapkan lingkungan

    Instal SDK dan persyaratan apa pun untuk lingkungan pengembangan Anda.

    Java

    1. Download dan instal Java Development Kit (JDK) versi 17. Pastikan variabel lingkungan JAVA_HOME telah ditetapkan dan mengarah ke penginstalan JDK Anda.

    2. Download dan instal Apache Maven dengan mengikuti panduan penginstalan Maven untuk sistem operasi spesifik Anda.

    Python

    Instal Apache Beam SDK untuk Python.

    Go

    Gunakan Panduan download dan penginstalan Go untuk mendownload dan menginstal Go untuk sistem operasi spesifik Anda. Untuk mempelajari lingkungan runtime Go yang didukung oleh Apache Beam, lihat Dukungan runtime Apache Beam.

    Download contoh kode.

    Java

    1. Buat clone repositori java-docs-samples.

      git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
    2. Buka contoh kode untuk tutorial ini.

      cd java-docs-samples/dataflow/flex-templates/getting_started
    3. Bangun project Java menjadi file JAR Uber.

      mvn clean package

      File JAR Uber ini memiliki semua dependensi yang disematkan di dalamnya. Anda dapat menjalankan file ini sebagai aplikasi mandiri tanpa dependensi eksternal pada library lain.

    Python

    1. Buat clone repositori python-docs-samples.

      git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
    2. Buka contoh kode untuk tutorial ini.

      cd python-docs-samples/dataflow/flex-templates/getting_started

    Go

    1. Buat clone repositori golang-samples.

      git clone https://github.com/GoogleCloudPlatform/golang-samples.git
    2. Buka contoh kode untuk tutorial ini.

      cd golang-samples/dataflow/flex-templates/wordcount
    3. Kompilasikan biner Go.

      CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o wordcount .

    Membuat bucket Cloud Storage

    Gunakan perintah gcloud storage buckets create untuk membuat bucket Cloud Storage:

    gcloud storage buckets create gs://BUCKET_NAME
    

    Ganti BUCKET_NAME dengan nama untuk bucket Cloud Storage Anda. Nama bucket Cloud Storage harus unik secara global dan memenuhi persyaratan penamaan bucket.

    Membuat repositori Artifact Registry

    Buat repositori Artifact Registry tempat Anda akan mengirim image container Docker untuk template.

    1. Gunakan perintah gcloud artifacts repositories create untuk membuat repositori Artifact Registry baru.

      gcloud artifacts repositories create REPOSITORY \
       --repository-format=docker \
       --location=LOCATION

      Ganti kode berikut:

      • REPOSITORY: nama untuk repositori Anda. Nama repositori harus unik untuk setiap lokasi repositori dalam project.
      • LOCATION: lokasi regional atau multi-regional untuk repositori.
    2. Gunakan perintah gcloud auth configure-docker untuk mengonfigurasi Docker agar mengautentikasi permintaan untuk Artifact Registry. Perintah ini mengupdate konfigurasi Docker Anda, sehingga Anda dapat terhubung dengan Artifact Registry untuk mengirim image.

      gcloud auth configure-docker LOCATION-docker.pkg.dev

    Template Fleksibel juga dapat menggunakan image yang disimpan di registry pribadi. Untuk mengetahui informasi selengkapnya, lihat Menggunakan image dari registry pribadi.

    Buat Template Flex

    Pada langkah ini, Anda menggunakan perintah gcloud dataflow flex-template build untuk membuat Template Flex.

    Template Fleksibel terdiri dari komponen berikut:

    • Image container Docker yang mengemas kode pipeline Anda. Untuk Template Fleksibel Java dan Python, image Docker dibangun dan dikirim ke repositori Artifact Registry saat Anda menjalankan perintah gcloud dataflow flex-template build.
    • File spesifikasi template. File ini adalah dokumen JSON yang berisi lokasi image penampung serta metadata tentang template, seperti parameter pipeline.

    Repositori contoh di GitHub berisi file metadata.json.

    Untuk memperluas template dengan metadata tambahan, Anda dapat membuat file metadata.json Anda sendiri.

    Java

    gcloud dataflow flex-template build gs://BUCKET_NAME/getting_started-java.json \
     --image-gcr-path "LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/getting-started-java:latest" \
     --sdk-language "JAVA" \
     --flex-template-base-image JAVA17 \
     --metadata-file "metadata.json" \
     --jar "target/flex-template-getting-started-1.0.jar" \
     --env FLEX_TEMPLATE_JAVA_MAIN_CLASS="com.example.dataflow.FlexTemplateGettingStarted"

    Ganti kode berikut:

    • BUCKET_NAME: nama bucket Cloud Storage yang Anda buat sebelumnya
    • LOCATION: lokasi
    • PROJECT_ID: Google Cloud project ID
    • REPOSITORY: nama repositori Artifact Registry yang Anda buat sebelumnya

    Python

    gcloud dataflow flex-template build gs://BUCKET_NAME/getting_started-py.json \
     --image-gcr-path "LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/getting-started-python:latest" \
     --sdk-language "PYTHON" \
     --flex-template-base-image "PYTHON3" \
     --metadata-file "metadata.json" \
     --py-path "." \
     --env "FLEX_TEMPLATE_PYTHON_PY_FILE=getting_started.py" \
     --env "FLEX_TEMPLATE_PYTHON_REQUIREMENTS_FILE=requirements.txt"

    Ganti kode berikut:

    • BUCKET_NAME: nama bucket Cloud Storage yang Anda buat sebelumnya
    • LOCATION: lokasi
    • PROJECT_ID: Google Cloud project ID
    • REPOSITORY: nama repositori Artifact Registry yang Anda buat sebelumnya

    Go

    1. Gunakan perintah gcloud builds submit untuk membangun image Docker menggunakan Dockerfile dengan Cloud Build. Perintah ini akan membangun file dan mengirimkannya ke repositori Artifact Registry Anda.

      gcloud builds submit --tag LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/dataflow/wordcount-go:latest .

      Ganti kode berikut:

      • LOCATION: lokasi
      • PROJECT_ID: Google Cloud project ID
      • REPOSITORY: nama repositori Artifact Registry yang Anda buat sebelumnya
    2. Gunakan perintah gcloud dataflow flex-template build untuk membuat Template Flex bernama wordcount-go.json di bucket Cloud Storage Anda.

      gcloud dataflow flex-template build gs://BUCKET_NAME/samples/dataflow/templates/wordcount-go.json \
        --image "LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/dataflow/wordcount-go:latest" \
        --sdk-language "GO" \
        --metadata-file "metadata.json"

      Ganti BUCKET_NAME dengan nama bucket Cloud Storage yang Anda buat sebelumnya.

    Jalankan Template Flex

    Pada langkah ini, Anda akan menggunakan template untuk menjalankan tugas Dataflow.

    Java

    1. Gunakan perintah gcloud dataflow flex-template run untuk menjalankan tugas Dataflow yang menggunakan Template Flex.

      gcloud dataflow flex-template run "getting-started-`date +%Y%m%d-%H%M%S`" \
       --template-file-gcs-location "gs://BUCKET_NAME/getting_started-java.json" \
       --parameters output="gs://BUCKET_NAME/output-" \
       --additional-user-labels "LABELS" \
       --region "REGION"

      Ganti kode berikut:

      • BUCKET_NAME: nama bucket Cloud Storage yang Anda buat sebelumnya
      • REGION: Region
      • LABELS: Opsional. Label yang dilampirkan ke tugas Anda, menggunakan format <key1>=<val1>,<key2>=<val2>,...
    2. Untuk melihat status tugas Dataflow di konsol Google Cloud , buka halaman Jobs Dataflow.

      Buka Tugas

    Jika tugas berhasil dijalankan, tugas akan menulis output ke file bernama gs://BUCKET_NAME/output--00000-of-00001.txt di bucket Cloud Storage Anda.

    Python

    1. Gunakan perintah gcloud dataflow flex-template run untuk menjalankan tugas Dataflow yang menggunakan Template Flex.

      gcloud dataflow flex-template run "getting-started-`date +%Y%m%d-%H%M%S`" \
       --template-file-gcs-location "gs://BUCKET_NAME/getting_started-py.json" \
       --parameters output="gs://BUCKET_NAME/output-" \
       --additional-user-labels "LABELS" \
       --region "REGION"

      Ganti kode berikut:

      • BUCKET_NAME: nama bucket Cloud Storage yang Anda buat sebelumnya
      • REGION: Region
      • LABELS: Opsional. Label yang dilampirkan ke tugas Anda, menggunakan format <key1>=<val1>,<key2>=<val2>,...
    2. Untuk melihat status tugas Dataflow di konsol Google Cloud , buka halaman Jobs Dataflow.

      Buka Tugas

    Jika tugas berhasil dijalankan, tugas akan menulis output ke file bernama gs://BUCKET_NAME/output--00000-of-00001.txt di bucket Cloud Storage Anda.

    Go

    1. Gunakan perintah gcloud dataflow flex-template run untuk menjalankan tugas Dataflow yang menggunakan Template Flex.

      gcloud dataflow flex-template run "wordcount-go-`date +%Y%m%d-%H%M%S`" \
       --template-file-gcs-location "gs://BUCKET_NAME/samples/dataflow/templates/wordcount-go.json" \
       --parameters output="gs://BUCKET_NAME/samples/dataflow/templates/counts.txt" \
       --additional-user-labels "LABELS" \
       --region "REGION"

      Ganti kode berikut:

      • BUCKET_NAME: nama bucket Cloud Storage yang Anda buat sebelumnya
      • REGION: Region
      • LABELS: Opsional. Label yang dilampirkan ke tugas Anda, menggunakan format <key1>=<val1>,<key2>=<val2>,...
    2. Untuk melihat status tugas Dataflow di konsol Google Cloud , buka halaman Jobs Dataflow.

      Buka Tugas

    Jika tugas berhasil dijalankan, tugas akan menulis output ke file bernama gs://BUCKET_NAME/samples/dataflow/templates/count.txt di bucket Cloud Storage Anda.

    Pembersihan

    Agar tidak perlu membayar biaya pada akun Google Cloud Anda untuk resource yang digunakan dalam tutorial ini, hapus project yang berisi resource tersebut, atau simpan project dan hapus setiap resource.

    Menghapus project

      Delete a Google Cloud project:

      gcloud projects delete PROJECT_ID

    Menghapus resource satu per satu

    1. Hapus bucket Cloud Storage dan semua objek di bucket tersebut.
      gcloud storage rm gs://BUCKET_NAME --recursive
    2. Hapus repositori Artifact Registry.
      gcloud artifacts repositories delete REPOSITORY \
          --location=LOCATION
    3. Batalkan peran yang Anda berikan ke akun layanan default Compute Engine. Jalankan perintah berikut satu kali untuk setiap peran IAM berikut:
      • roles/dataflow.admin
      • roles/dataflow.worker
      • roles/storage.objectAdmin
      • roles/artifactregistry.writer
      gcloud projects remove-iam-policy-binding PROJECT_ID \
          --member=serviceAccount:PROJECT_NUMBER[email protected] \
          --role=SERVICE_ACCOUNT_ROLE
    4. Optional: Revoke the authentication credentials that you created, and delete the local credential file.

      gcloud auth application-default revoke
    5. Optional: Revoke credentials from the gcloud CLI.

      gcloud auth revoke

    Langkah berikutnya