gcloud CLI を使用して Dataproc クラスタを作成する
このページでは、Google Cloud CLI の gcloud コマンドライン ツールを使用して Dataproc クラスタを作成し、クラスタで Apache Spark ジョブを実行してから、クラスタ内のワーカー数を変更する方法について説明します。
API Explorer を使用したクイックスタートと同じまたは類似のタスクを行う方法は、 Google Cloud コンソール( Google Cloud コンソールを使用して Dataproc クラスタを作成する)と、クライアント ライブラリ(クライアント ライブラリを使用して 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
ジョブを実行する - ジョブの pi 計算アプリケーションの主なメソッドが含まれる
class
- ジョブのコードが含まれる jar ファイルの場所
- ジョブに渡すパラメータ - この場合はタスクの数(
1000
)
ジョブの実行中の出力と最終出力がターミナル ウィンドウに表示されます。
Waiting for job output... ... Pi is roughly 3.14118528 ... Job finished successfully.
クラスタの更新
クラスタ内のワーカーの数を変更するには、次のコマンドを実行します。
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 ジョブを記述して実行する方法を学習する。