使用 gcloud CLI 建立 Dataproc 叢集
本頁說明如何使用 Google Cloud CLI gcloud 指令列工具建立 Dataproc 叢集、在叢集中執行 Apache Spark 工作,然後修改叢集中的工作站數量。
。想瞭解如何執行相同或類似的工作,可以參閱使用 API Explorer 的快速入門導覽課程、使用 Google Cloud 控制台建立 Dataproc 叢集中的 Google Cloud 控制台,以及使用用戶端程式庫建立 Dataproc 叢集中的用戶端程式庫。
事前準備
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Dataproc API.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Dataproc API.
建立叢集
如要建立名為 example-cluster
的叢集,請執行下列指令:
gcloud dataproc clusters create example-cluster --region=REGION
指令輸出內容會確認叢集建立作業:
Waiting for cluster creation operation...done. Created [... example-cluster]
如要瞭解如何選取地區,請參閱「可用地區與區域」一節。如要查看可用地區清單,您可以執行 gcloud compute regions list
指令。如要瞭解地區端點,請參閱「地區端點」。
提交工作
如要提交計算 pi
粗略值的範例 Spark 工作,請執行下列指令:
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
這個指令會指定下列項目:
- 您要在指定區域的
example-cluster
叢集上執行spark
工作 class
,其中包含工作計算圓周率時採用的主要方法- 含有工作程式碼的 jar 檔案所在位置
- 您要傳送至工作的任何參數;在本例中指工作數,值為
1000
終端機視窗會顯示工作執行中和最終輸出的內容:
Waiting for job output... ... Pi is roughly 3.14118528 ... Job finished successfully.
更新叢集
如要將叢集中的工作站數量變更為 5,請執行下列指令:
gcloud dataproc clusters update example-cluster \ --region=REGION \ --num-workers 5
指令輸出內容會顯示叢集的詳細資料。例如:
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.
如要將工作站節點數減至原始值,請使用相同指令:
gcloud dataproc clusters update example-cluster \ --region=REGION \ --num-workers 2
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取您在本頁所用資源的費用,請按照下列步驟操作。
如要刪除
example-cluster
,請執行clusters delete
指令:gcloud dataproc clusters delete example-cluster \ --region=REGION
如要確認並完成叢集刪除作業,請按下 y,然後在系統提示時按下 Enter。
後續步驟
- 瞭解如何編寫及執行 Spark Scala 工作。