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.
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
- 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.
-
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.
-
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.
-
-
Verify that billing is enabled for your Google Cloud project.
-
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
-
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.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.
-
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.
-
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.
-
-
Verify that billing is enabled for your Google Cloud project.
-
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
-
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.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.
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 AndaPROJECT_NUMBER
nomor projectSERVICE_ACCOUNT_ROLE
: setiap peran individual
Download dan instal Java Development Kit (JDK) versi 17. Pastikan variabel lingkungan
JAVA_HOME
telah 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 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.
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 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.
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
- 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
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
Gunakan perintah
gcloud dataflow flex-template build
untuk membuat Template Flex bernamawordcount-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.
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>,...
Untuk melihat status tugas Dataflow di konsol Google Cloud , buka halaman Jobs Dataflow.
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>,...
Untuk melihat status tugas Dataflow di konsol Google Cloud , buka halaman Jobs Dataflow.
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>,...
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
-
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
-
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
- Pelajari cara mengonfigurasi Template Fleksibel.
- Lihat daftar template yang disediakan Google.
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
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.
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