使用 gcloud CLI 创建 Dataproc 集群

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

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

准备工作

  1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. 确保您的 Google Cloud 项目已启用结算功能

  4. 启用 Dataproc API。

    启用 API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. 确保您的 Google Cloud 项目已启用结算功能

  7. 启用 Dataproc API。

    启用 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 即可完成删除操作。

后续步骤