Dengan Template Flex Dataflow, Anda dapat mengemas pipeline Dataflow untuk deployment. Tutorial ini menunjukkan cara membuat contoh Template Flex, 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 Cloud Platform yang dapat ditagih berikut:
Untuk membuat perkiraan biaya berdasarkan proyeksi penggunaan Anda, gunakan kalkulator harga.
Setelah menyelesaikan tugas yang dijelaskan dalam dokumen ini, Anda dapat menghindari penagihan berkelanjutan dengan menghapus resource yang Anda buat. Untuk mengetahui informasi selengkapnya, baca bagian Pembersihan.
Sebelum memulai
- Sign in to your Google Cloud Platform 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.
-
Install the Google Cloud CLI.
-
Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.
-
Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith 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_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Dataflow, Compute Engine, Logging, Cloud Storage, Cloud Storage JSON, Resource Manager, and Artifact Registry API:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable dataflow compute_component logging storage_component storage_api cloudresourcemanager.googleapis.com artifactregistry.googleapis.com
-
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.
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/iam.serviceAccountUsergcloud 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,myemail@example.com.ROLE: The IAM role that you grant to your user account.
-
Install the Google Cloud CLI.
-
Jika Anda menggunakan penyedia identitas (IdP) eksternal, Anda harus login ke gcloud CLI dengan identitas gabungan Anda terlebih dahulu.
-
Untuk melakukan inisialisasi gcloud CLI, jalankan perintah berikut:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith 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_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Dataflow, Compute Engine, Logging, Cloud Storage, Cloud Storage JSON, Resource Manager, and Artifact Registry API:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.gcloud services enable dataflow compute_component logging storage_component storage_api cloudresourcemanager.googleapis.com artifactregistry.googleapis.com
-
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.
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/iam.serviceAccountUsergcloud 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,myemail@example.com.ROLE: The IAM role that you grant to your user account.
Berikan peran ke akun layanan default Compute Engine Anda. Jalankan perintah berikut satu kali untuk setiap peran IAM berikut:
roles/dataflow.adminroles/dataflow.workerroles/storage.objectAdminroles/artifactregistry.writer
gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com" --role=SERVICE_ACCOUNT_ROLE
Ganti kode berikut:
PROJECT_ID: project ID AndaPROJECT_NUMBERnomor project AndaSERVICE_ACCOUNT_ROLE: setiap peran individu
Download dan instal Java Development Kit (JDK) versi 17. Pastikan variabel lingkungan
JAVA_HOMEtelah ditetapkan dan mengarah ke penginstalan JDK Anda.Download dan instal Apache Maven dengan mengikuti panduan penginstalan Maven untuk sistem operasi spesifik Anda.
Buat clone repositori
java-docs-samples.git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
Buka contoh kode untuk tutorial ini.
cd java-docs-samples/dataflow/flex-templates/getting_started
Bangun project Java menjadi file Uber JAR.
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.
Buat clone repositori
python-docs-samples.git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Buka contoh kode untuk tutorial ini.
cd python-docs-samples/dataflow/flex-templates/getting_started
Buat clone repositori
golang-samples.git clone https://github.com/GoogleCloudPlatform/golang-samples.git
Buka contoh kode untuk tutorial ini.
cd golang-samples/dataflow/flex-templates/wordcount
Kompilasikan biner Go.
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o wordcount .
Gunakan perintah
gcloud artifacts repositories createuntuk 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.
Gunakan perintah
gcloud auth configure-dockeruntuk 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
- 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.
- 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
- 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
- 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
Gunakan perintah
gcloud dataflow flex-template rununtuk 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-" \ --region "REGION"
Ganti kode berikut:
- BUCKET_NAME: nama bucket Cloud Storage yang Anda buat sebelumnya
- REGION: Region
Untuk melihat status tugas Dataflow di konsolGoogle Cloud , buka halaman Tugas Dataflow.
Gunakan perintah
gcloud dataflow flex-template rununtuk 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-" \ --region "REGION"
Ganti kode berikut:
- BUCKET_NAME: nama bucket Cloud Storage yang Anda buat sebelumnya
- REGION: Region
Untuk melihat status tugas Dataflow di konsolGoogle Cloud , buka halaman Tugas Dataflow.
Gunakan perintah
gcloud dataflow flex-template rununtuk 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" \ --region "REGION"
Ganti kode berikut:
- BUCKET_NAME: nama bucket Cloud Storage yang Anda buat sebelumnya
- REGION: Region
Untuk melihat status tugas Dataflow di konsol Google Cloud , buka halaman Jobs Dataflow.
-
Hapus bucket Cloud Storage dan semua objek di bucket tersebut.
gcloud storage rm gs://BUCKET_NAME --recursive
-
Hapus repositori Artifact Registry.
gcloud artifacts repositories delete REPOSITORY \ --location=LOCATION
-
Cabut peran yang Anda berikan ke akun layanan default Compute Engine. Jalankan perintah berikut satu kali untuk setiap
peran IAM berikut:
roles/dataflow.adminroles/dataflow.workerroles/storage.objectAdminroles/artifactregistry.writer
gcloud projects remove-iam-policy-binding PROJECT_ID \ --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --role=SERVICE_ACCOUNT_ROLE
-
Optional: Revoke the authentication credentials that you created, and delete the local credential file.
gcloud auth application-default revoke
-
Optional: Revoke credentials from the gcloud CLI.
gcloud auth revoke
Menyiapkan lingkungan
Instal SDK dan persyaratan apa pun untuk lingkungan pengembangan Anda.
Java
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
Python
Go
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.
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:
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:
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:
Go
gcloud dataflow flex-template build gs://BUCKET_NAME/samples/dataflow/templates/wordcount-go.json \ --image-gcr-path "LOCATION-docker.pkg.dev/PROJECT_ID/REPOSITORY/dataflow/wordcount-go:latest" \ --sdk-language "GO" \ --flex-template-base-image "GO" \ --metadata-file "metadata.json" \ --go-binary-path="." \ --env "FLEX_TEMPLATE_GO_BINARY=wordcount"
Ganti kode berikut:
Jalankan Template Flex
Pada langkah ini, Anda akan menggunakan template untuk menjalankan tugas Dataflow.
Java
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
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
Jika tugas berhasil dijalankan, tugas akan menulis output ke file bernama
gs://BUCKET_NAME/samples/dataflow/templates/count.txt di
bucket Cloud Storage Anda.
Jika tugas Anda gagal dijalankan dan pesan error
A Timeout in polling error message ditampilkan, lihat
Memecahkan masalah waktu tunggu Flex Template.
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