使用 gcloud CLI 创建 Dataproc 集群

本页面介绍了如何使用 Google Cloud CLI gcloud 命令行工具创建 Google Cloud Dataproc 集群,在该集群中运行简单的 Apache Spark 作业,然后修改该集群中的工作器数量。

如需了解如何使用 API Explorer、Google Cloud 控制台和客户端库执行相同或类似的任务,请参阅快速入门:使用 API Explorer快速入门:使用 Google Cloud 控制台创建 Dataproc 集群快速入门:使用客户端库创建 Dataproc 集群

准备工作

  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. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Dataproc API.

    Enable the API

创建集群

运行以下命令可以创建名为 example-cluster 的集群。 如需了解如何选择区域,请参阅可用区域和区域(您也可以运行 gcloud compute regions list 命令查看可用区域的列表)。另请参阅区域端点,了解区域端点。

gcloud dataproc clusters create example-cluster --region=region

系统会在命令输出中确认创建集群:

...
Waiting for cluster creation operation...done.
Created [... example-cluster]

提交作业

要提交计算 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.

更新集群

要将集群中的工作器数量更改为 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 账号产生费用,请按照以下步骤操作。

  • 运行 clusters delete 可删除示例集群。
    gcloud dataproc clusters delete example-cluster \
        --region=region
    
    系统会提示您确认是否要删除集群。输入 y 即可完成删除操作。

后续步骤