Halaman ini menunjukkan cara menggunakan alat command line Google Cloud CLI gcloud untuk membuat cluster Dataproc, menjalankan tugas Apache Spark di cluster, lalu mengubah jumlah pekerja di cluster.
Anda dapat mengetahui cara melakukan tugas yang sama atau serupa dengan Panduan memulai menggunakan API Explorer, konsol Google Cloud di Membuat cluster Dataproc menggunakan konsol Google Cloud , dan menggunakan library klien di Membuat cluster Dataproc menggunakan library klien.
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.
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.
-
Pastikan Anda memiliki izin yang diperlukan untuk menyelesaikan panduan ini.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Dataproc 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 dataproc.googleapis.com
-
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.
-
Pastikan Anda memiliki izin yang diperlukan untuk menyelesaikan panduan ini.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Dataproc 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 dataproc.googleapis.com
-
Dataproc Editor (
roles/dataproc.editor) di project -
Pengguna Akun Layanan (
roles/iam.serviceAccountUser) di akun layanan default Compute Engine - Tugas berjalan di
example-cluster. classberisi metode utama untuk SparkPi, yang menghitung nilaipiperkiraan. integritas aplikasi.- File jar berisi kode tugas.
1000adalah parameter tugas. Menentukan jumlah tugas (iterasi) yang dilakukan tugas untuk menghitung nilaipi.- Untuk menghapus
example-cluster, jalankan perintahclusters delete:gcloud dataproc clusters delete example-cluster \ --region=REGION
- Pelajari cara menulis dan menjalankan tugas Spark Scala.
Peran yang diperlukan
Peran IAM tertentu diperlukan untuk menjalankan contoh di halaman ini. Bergantung pada kebijakan organisasi, peran ini mungkin sudah diberikan. Untuk memeriksa pemberian peran, lihat Apakah Anda perlu memberikan peran?.
Untuk mengetahui informasi selengkapnya tentang pemberian peran, lihat Mengelola akses ke project,folder, dan organisasi.
Peran pengguna
Untuk mendapatkan izin yang Anda perlukan untuk membuat cluster Dataproc, minta administrator untuk memberi Anda peran IAM berikut:
Peran akun layanan
Untuk memastikan bahwa akun layanan default Compute Engine memiliki izin yang diperlukan untuk membuat cluster Dataproc, minta administrator Anda untuk memberikan peran IAM Dataproc Worker (roles/dataproc.worker) kepada akun layanan default Compute Engine di project.
Membuat cluster
Untuk membuat cluster bernama example-cluster, jalankan perintah
gcloud Dataproc clusters create berikut.
gcloud dataproc clusters create example-cluster --region=REGION
Ganti kode berikut:
REGION: Tentukan region tempat cluster akan berada.
Mengirim tugas
Untuk mengirimkan contoh tugas Spark yang menghitung nilai kasar untuk pi, jalankan perintah
gcloud Dataproc jobs submit spark berikut:
gcloud dataproc jobs submit spark --cluster example-cluster \ --region=REGION \ --class org.apache.spark.examples.SparkPi \ --jars file:///usr/lib/spark/examples/jars/spark-examples.jar -- 1000
Catatan:
Ganti kode berikut:
REGION: Tentukan region cluster.
Tugas sedang berjalan dan output terakhir ditampilkan di jendela terminal:
Waiting for job output... ... Pi is roughly 3.14118528 ... Job finished successfully.
Mengupdate cluster
Untuk mengubah jumlah worker di cluster menjadi lima, jalankan perintah berikut:
gcloud dataproc clusters update example-cluster \ --region=REGION \ --num-workers 5
Output perintah menampilkan detail cluster:
workerConfig: ... instanceNames: - example-cluster-w-0 - example-cluster-w-1 - example-cluster-w-2 - example-cluster-w-3 - example-cluster-w-4 numInstances: 5 statusHistory: ... - detail: Add 3 workers.
Untuk mengurangi jumlah worker node ke nilai asli 2, jalankan
perintah berikut:
gcloud dataproc clusters update example-cluster \ --region=REGION \ --num-workers 2
Pembersihan
Agar akun Google Cloud Anda tidak dikenai biaya untuk resource yang digunakan di halaman ini, ikuti langkah-langkah berikut.