Membuat pipeline Dataflow menggunakan Java
Dokumen ini menunjukkan cara menyiapkan project Google Cloud , membuat contoh pipeline yang dibangun dengan Apache Beam SDK untuk Java, dan menjalankan contoh pipeline di layanan Dataflow. Pipeline membaca file teks dari Cloud Storage, menghitung jumlah kata unik dalam file, lalu menulis kembali jumlah kata ke Cloud Storage. Untuk pengantar pipeline WordCount, lihat video Cara menggunakan WordCount di Apache Beam.
Tutorial ini memerlukan Maven, tetapi project contoh juga dapat dikonversi dari Maven ke Gradle. Untuk mempelajari lebih lanjut, lihat Opsional: Mengonversi dari Maven ke Gradle.
Untuk mengikuti panduan langkah demi langkah tugas ini langsung di Google Cloud konsol, klik Pandu saya:
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, Cloud Logging, Cloud Storage, Google Cloud Storage JSON, BigQuery, Cloud Pub/Sub, Cloud Datastore, and Cloud Resource Manager APIs:
gcloud services enable dataflow
compute_component logging storage_component storage_api bigquery pubsub datastore.googleapis.com cloudresourcemanager.googleapis.com -
Create local authentication credentials for your user account:
gcloud auth application-default login
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,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.
-
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, Cloud Logging, Cloud Storage, Google Cloud Storage JSON, BigQuery, Cloud Pub/Sub, Cloud Datastore, and Cloud Resource Manager APIs:
gcloud services enable dataflow
compute_component logging storage_component storage_api bigquery pubsub datastore.googleapis.com cloudresourcemanager.googleapis.com -
Create local authentication credentials for your user account:
gcloud auth application-default login
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,myemail@example.com
.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
gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com" --role=SERVICE_ACCOUNT_ROLE
- Ganti
PROJECT_ID
dengan project ID Anda. - Ganti
PROJECT_NUMBER
dengan nomor project Anda. Untuk menemukan nomor project Anda, lihat Mengidentifikasi project atau gunakan perintahgcloud projects describe
. - Ganti
SERVICE_ACCOUNT_ROLE
dengan setiap peran individual.
-
Create a Cloud Storage bucket and configure it as follows:
-
Set the storage class to
S
(Standar). -
Tetapkan lokasi penyimpanan sebagai berikut:
US
(Amerika Serikat). -
Ganti
BUCKET_NAME
dengan nama bucket yang unik. Jangan sertakan informasi sensitif pada nama bucket karena namespace bucket bersifat global dan dapat dilihat publik. - Salin perintah berikut, karena Anda akan memerlukannya di bagian selanjutnya:
- Nama bucket Cloud Storage Anda.
- ID project Google Cloud Anda. Untuk menemukan ID ini, lihat Mengidentifikasi project.
- Download dan instal
Java Development Kit (JDK) versi 11. (Dataflow terus mendukung versi 8.) Pastikan variabel lingkungan
JAVA_HOME
telah ditetapkan dan menunjuk ke penginstalan JDK Anda. - Download dan instal Apache Maven, dengan mengikuti panduan penginstalan Maven untuk sistem operasi spesifik Anda.
- Di shell atau terminal, gunakan
Maven Archetype Plugin untuk membuat project Maven di komputer Anda yang
berisi contoh
WordCount
Apache Beam SDK:mvn archetype:generate \ -DarchetypeGroupId=org.apache.beam \ -DarchetypeArtifactId=beam-sdks-java-maven-archetypes-examples \ -DarchetypeVersion=2.66.0 \ -DgroupId=org.example \ -DartifactId=word-count-beam \ -Dversion="0.1" \ -Dpackage=org.apache.beam.examples \ -DinteractiveMode=false
Perintah ini akan membuat direktori baru bernama
word-count-beam
di bawah direktori Anda saat ini. Direktoriword-count-beam
berisi filepom.xml
sederhana dan serangkaian contoh pipeline yang menghitung kata dalam file teks. - Verifikasi bahwa direktori
word-count-beam
Anda berisi filepom.xml
:Linux atau macOS
cd word-count-beam/ ls
Outputnya adalah sebagai berikut:
pom.xml src
Windows
cd word-count-beam/ dir
Outputnya adalah sebagai berikut:
pom.xml src
- Pastikan project Maven Anda berisi contoh pipeline:
Linux atau macOS
ls src/main/java/org/apache/beam/examples/
Outputnya adalah sebagai berikut:
DebuggingWordCount.java WindowedWordCount.java common MinimalWordCount.java WordCount.java
Windows
dir src/main/java/org/apache/beam/examples/
Outputnya adalah sebagai berikut:
DebuggingWordCount.java WindowedWordCount.java common MinimalWordCount.java WordCount.java
- Di shell atau terminal, jalankan pipeline
WordCount
secara lokal dari direktoriword-count-beam
Anda:mvn compile exec:java \ -Dexec.mainClass=org.apache.beam.examples.WordCount \ -Dexec.args="--output=counts"
File output memiliki awalan
counts
dan ditulis ke direktoriword-count-beam
. Objek ini berisi kata-kata unik dari teks input dan jumlah kemunculan setiap kata. - Di shell atau terminal, bangun dan jalankan pipeline
WordCount
di layanan Dataflow dari direktoriword-count-beam
Anda:mvn -Pdataflow-runner compile exec:java \ -Dexec.mainClass=org.apache.beam.examples.WordCount \ -Dexec.args="--project=PROJECT_ID \ --gcpTempLocation=gs://BUCKET_NAME/temp/ \ --output=gs://BUCKET_NAME/output \ --runner=DataflowRunner \ --region=REGION"
Ganti kode berikut:
PROJECT_ID
: Google Cloud project ID AndaBUCKET_NAME
: nama bucket Cloud Storage AndaREGION
: region Dataflow, sepertius-central1
Di konsol Google Cloud , buka halaman Jobs Dataflow.
Buka TugasHalaman Jobs menampilkan detail semua lowongan yang tersedia, termasuk statusnya. Status tugas wordcount awalnya Running, lalu diperbarui menjadi Succeeded.
Di Google Cloud konsol, buka halaman Bucket Cloud Storage.
Buka BucketsHalaman Buckets menampilkan daftar semua bucket penyimpanan di project Anda.
- Klik bucket penyimpanan yang Anda buat.
Halaman Bucket details menampilkan file output dan file staging yang dibuat oleh tugas Dataflow Anda.
-
Hapus bucket:
gcloud storage buckets delete BUCKET_NAME
Mencabut 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
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
- Pelajari model pemrograman Apache Beam.
- Pelajari cara menggunakan Apache Beam untuk membangun pipeline.
- Pelajari contoh WordCount dan Game Seluler.
gcloud storage buckets create gs://BUCKET_NAME --default-storage-class STANDARD --location US
Mendapatkan kode pipeline
Apache Beam SDK adalah model pemrograman open source untuk pipeline pemrosesan data. Anda menentukan pipeline ini dengan program Apache Beam dan dapat memilih runner, seperti Dataflow, untuk menjalankan pipeline Anda.
Untuk pengantar mendetail tentang konsep Apache Beam yang digunakan dalam contoh ini, lihat Contoh WordCount Apache Beam. Petunjuk di bagian berikutnya menggunakan
WordCount.java
.Menjalankan pipeline secara lokal
Menjalankan pipeline di layanan Dataflow
Melihat hasil Anda
Pembersihan
Agar tidak menimbulkan biaya pada akun Google Cloud Anda untuk resource yang digunakan di halaman ini, hapus project Google Cloud yang berisi resource tersebut.
Menghapus project
Cara termudah untuk menghilangkan penagihan adalah dengan menghapus Google Cloud project yang Anda buat untuk panduan memulai.
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
Menghapus resource satu per satu
Jika Anda ingin mempertahankan Google Cloud project yang Anda gunakan dalam panduan memulai ini, hapus masing-masing resource:
Langkah berikutnya
-
Set the storage class to