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:

Pandu saya


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, 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
  8. 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.

  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, myemail@example.com.
    • 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, 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
  16. 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.

  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, myemail@example.com.
    • 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
    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 perintah gcloud projects describe.
    • Ganti SERVICE_ACCOUNT_ROLE dengan setiap peran individual.
  19. 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.
    • gcloud storage buckets create gs://BUCKET_NAME --default-storage-class STANDARD --location US
    • Salin perintah berikut, karena Anda akan memerlukannya di bagian selanjutnya:
    • 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.
    • 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.

      1. 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. Direktori word-count-beam berisi file pom.xml sederhana dan serangkaian contoh pipeline yang menghitung kata dalam file teks.

      2. Verifikasi bahwa direktori word-count-beam Anda berisi file pom.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
      3. 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

      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

      • Di shell atau terminal, jalankan pipeline WordCount secara lokal dari direktori word-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 direktori word-count-beam. Objek ini berisi kata-kata unik dari teks input dan jumlah kemunculan setiap kata.

      Menjalankan pipeline di layanan Dataflow

      • Di shell atau terminal, bangun dan jalankan pipeline WordCount di layanan Dataflow dari direktori word-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 Anda
        • BUCKET_NAME: nama bucket Cloud Storage Anda
        • REGION: region Dataflow, seperti us-central1

      Melihat hasil Anda

      1. Di konsol Google Cloud , buka halaman Jobs Dataflow.

        Buka Tugas

        Halaman Jobs menampilkan detail semua lowongan yang tersedia, termasuk statusnya. Status tugas wordcount awalnya Running, lalu diperbarui menjadi Succeeded.

      2. Di Google Cloud konsol, buka halaman Bucket Cloud Storage.

        Buka Buckets

        Halaman Buckets menampilkan daftar semua bucket penyimpanan di project Anda.

      3. Klik bucket penyimpanan yang Anda buat.

        Halaman Bucket details menampilkan file output dan file staging yang dibuat oleh tugas Dataflow 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:

      1. Hapus bucket:
        gcloud storage buckets delete BUCKET_NAME
      2. 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
      3. Optional: Revoke the authentication credentials that you created, and delete the local credential file.

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

        gcloud auth revoke

      Langkah berikutnya