Dalam dokumen ini, Anda akan menggunakan komponen Google Cloudyang dapat ditagih berikut:
- Dataproc
- Compute Engine
- Cloud Scheduler
Untuk membuat perkiraan biaya berdasarkan proyeksi penggunaan Anda,
gunakan kalkulator harga.
Sebelum memulai
Menyiapkan project
- Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
-
In the Google Cloud console, on the project selector page, select or create 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.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Dataproc, Compute Engine, and Cloud Scheduler APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
In the Google Cloud console, on the project selector page, select or create 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.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Dataproc, Compute Engine, and Cloud Scheduler APIs, if any are not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
Membuat peran khusus
- Buka halaman IAM & Admin → Roles
di konsol Google Cloud .
- Klik CREATE ROLE untuk membuka halaman Create Role.
- Lengkapi kolom Title, Description, ID, Launch stage. Saran: Gunakan "Dataproc Workflow Template Create" sebagai judul peran.
- Klik ADD PERMISSIONS,
- Di formulir Add permissions, klik Filter, lalu pilih "Permission". Lengkapi filter untuk membaca "Permission: dataproc.workflowTemplates.instantiate".
- Klik kotak centang di sebelah kiri izin yang tercantum, lalu
klik Add.

- Di halaman Create Role, klik ADD PERMISSIONS lagi untuk mengulangi sub-langkah sebelumnya guna menambahkan izin "iam.serviceAccounts.actAs" ke peran khusus. Halaman Create Role kini mencantumkan dua izin.

- Klik CREATE di halaman peran khusus.
Peran khusus tercantum di halaman Roles.

Membuat akun layanan
Di console Google Cloud , buka halaman Akun Layanan.
Pilih project Anda.
Klik Buat Akun Layanan.
Di kolom Service account name, masukkan nama
workflow-scheduler. Konsol Google Cloud akan mengisi kolom ID akun layanan berdasarkan nama ini.Opsional: Di kolom Deskripsi akun layanan, masukkan deskripsi akun layanan.
Klik Create and continue.
Klik kolom Select a role dan pilih peran khusus Dataproc Workflow Template Create yang Anda buat di langkah sebelumnya.
Klik Lanjutkan.
Di kolom Peran admin akun layanan, masukkan alamat email Akun Google Anda.
Klik Selesai untuk menyelesaikan pembuatan akun layanan.
Membuat template alur kerja.
Salin dan jalankan perintah yang tercantum di bawah ini di jendela terminal lokal atau di Cloud Shell untuk membuat dan menentukan template alur kerja.
Catatan:
- Perintah ini menentukan region "us-central1". Anda dapat menentukan
region yang berbeda atau menghapus flag
--regionjika sebelumnya Anda telah menjalankangcloud config set compute/regionuntuk menetapkan properti region. - Urutan "-- " (spasi tanda hubung) dalam perintah
add-jobmeneruskan argumen1000ke tugas SparkPi, yang menentukan jumlah sampel yang akan digunakan untuk memperkirakan nilai Pi.
- Buat template alur kerja.
gcloud dataproc workflow-templates create sparkpi \ --region=us-central1
- Tambahkan tugas Spark ke template alur kerja sparkpi. ID langkah "compute" diperlukan, dan mengidentifikasi tugas SparkPi yang ditambahkan.
gcloud dataproc workflow-templates add-job spark \ --workflow-template=sparkpi \ --step-id=compute \ --class=org.apache.spark.examples.SparkPi \ --jars=file:///usr/lib/spark/examples/jars/spark-examples.jar \ --region=us-central1 \ -- 1000
- Gunakan cluster terkelola, node tunggal untuk menjalankan alur kerja. Managed Service untuk Apache Spark akan membuat cluster, menjalankan alur kerja di dalamnya, lalu menghapus cluster saat alur kerja selesai.
gcloud dataproc workflow-templates set-managed-cluster sparkpi \ --cluster-name=sparkpi \ --single-node \ --region=us-central1
- Klik nama
sparkpidi halaman Workflows Managed Service untuk Apache Spark di konsol Google Cloud untuk membuka halaman Workflow template details. Konfirmasi atribut template sparkpi.
Buat tugas Cloud Scheduler
Buka halaman Cloud Scheduler di konsolGoogle Cloud (Anda mungkin perlu memilih project untuk membuka halaman). Klik CREATE JOB.
Masukkan atau pilih informasi tugas berikut:
- Pilih region: "us-central" atau region lain tempat Anda membuat template alur kerja.
- Name: "sparkpi"
- Frequency: "* * * * *" memilih setiap menit; "0 9 * * 1" memilih setiap Senin pukul 09.00. Lihat Menentukan Jadwal Tugas untuk nilai unix-cron lainnya. Catatan: Anda dapat mengklik tombol RUN NOW pada Jobs Cloud Scheduler di konsol Google Cloud untuk menjalankan dan menguji tugas, terlepas dari frekuensi yang Anda tetapkan untuk tugas tersebut.
- Zona waktu: Pilih timezone Anda. Ketik "Amerika Serikat" untuk mencantumkan zona waktu AS.
- Target: "HTTP"
- URL: Sisipkan URL berikut setelah menyisipkan
your-project-id. Ganti "us-central1" jika Anda membuat template alur kerja di region lain. URL ini akan memanggil
API
workflowTemplates.instantiateManaged Service untuk Apache Spark untuk menjalankan template alur kerja sparkpi Anda.https://dataproc.googleapis.com/v1/projects/your-project-id/regions/us-central1/workflowTemplates/sparkpi:instantiate?alt=json
- Metode HTTP:
- "POST"
- Body: "{}"
- Header autentikasi:
- "Tambahkan token OAuth"
- Service account: Masukkan service account address dari akun layanan yang Anda buat untuk tutorial ini.
Anda dapat menggunakan alamat akun berikut setelah menyisipkan your-project-id:
workflow-scheduler@your-project-id.iam.gserviceaccount.com
- Cakupan: Anda dapat mengabaikan item ini.
- Klik CREATE.
Menguji tugas alur kerja terjadwal
Di baris tugas
sparkpipada halaman Jobs di Cloud Scheduler, klik RUN NOW.Tunggu beberapa menit, lalu buka halaman Workflows Managed Service untuk Apache Spark untuk memverifikasi bahwa alur kerja sparkpi telah selesai.

Setelah alur kerja menghapus cluster terkelola, detail tugas akan tetap ada di konsol Google Cloud . Klik tugas
compute...yang tercantum di halaman Jobs Managed Service for Apache Spark untuk melihat detail tugas alur kerja.
Pembersihan
Alur kerja dalam tutorial ini menghapus cluster terkelolanya saat alur kerja selesai. Dengan mempertahankan alur kerja, Anda dapat menjalankan ulang alur kerja dan tidak dikenai biaya. Anda dapat menghapus resource lain yang dibuat dalam tutorial ini untuk menghindari biaya berulang.
Menghapus project
- Di konsol Google Cloud , buka halaman Manage resources.
- Pada daftar project, pilih project yang ingin Anda hapus, lalu klik Delete.
- Pada dialog, ketik project ID, lalu klik Shut down untuk menghapus project.
Menghapus template alur kerja Anda
gcloud dataproc workflow-templates delete sparkpi \ --region=us-central1
Menghapus tugas Cloud Scheduler
Buka halaman Tugas Cloud Scheduler di konsol Google Cloud , pilih kotak di sebelah kiri fungsi sparkpi, lalu klik DELETE.
Menghapus akun layanan Anda
Buka halaman IAM & Admin → Service Accounts
di konsol Google Cloud , centang kotak di sebelah kiri akun layanan workflow-scheduler...,
lalu klik DELETE.
Langkah berikutnya
- Lihat Ringkasan template alur kerja Managed Service untuk Apache Spark.
- Lihat Solusi penjadwalan alur kerja.