使用 gcloud CLI 创建 Dataproc 集群

本页介绍了如何使用 Google Cloud CLI gcloud 命令行工具创建 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

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

    Go to project selector

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

  7. Enable the Dataproc API.

    Enable the 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.

更新集群

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

  1. 如需删除 example-cluster,请运行 clusters delete 命令:

    gcloud dataproc clusters delete example-cluster \
        --region=REGION
    

  2. 如需确认并完成集群删除,请按 y,然后在出现提示时按 Enter 键。

后续步骤