在本文档中,您将使用 Google Cloud的以下收费组件:
- Managed Service for Apache Spark
- Compute Engine
- Managed Airflow
如需根据您的预计使用情况来估算费用,请使用价格计算器。
准备工作
设置项目
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Dataproc, Compute Engine, and Managed Airflow APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Dataproc, Compute Engine, and Managed Airflow APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init
创建 Managed Service for Apache Spark 工作流模板
在本地终端窗口或 Cloud Shell 中复制并运行以下命令,以创建和定义工作流模板。
- 创建
sparkpi工作流模板。gcloud dataproc workflow-templates create sparkpi \ --region=us-central1 - 将 Spark 作业添加到
sparkpi工作流模板。“compute”step-id标志用于识别 SparkPi 作业。gcloud dataproc workflow-templates add-job spark \ --workflow-template=sparkpi \ --step-id=compute \ --class=org.apache.spark.examples.SparkPi \ --jars=file:///usr/lib/spark/examples/jars/spark-examples.jar \ --region=us-central1 \ -- 1000 - 使用托管单节点集群运行工作流。Managed Service for Apache Spark 将创建集群,对其运行工作流,然后在工作流完成时删除集群。
gcloud dataproc workflow-templates set-managed-cluster sparkpi \ --cluster-name=sparkpi \ --single-node \ --region=us-central1 - 确认创建工作流模板。
控制台
点击 Google Cloud 控制台中的 Managed Service for Apache Spark 工作流页面上的
sparkpi名称,以打开工作流模板详细信息页面。点击工作流模板的名称以确认sparkpi模板属性。
gcloud 命令
运行以下命令:
gcloud dataproc workflow-templates describe sparkpi --region=us-central1
创建 DAG 并将其上传到 Cloud Storage
- 创建或使用现有的托管式 Airflow 环境。
- 设置环境变量。
Airflow 界面
- 在工具栏中,点击 Admin > Variables。
- 点击创建。
- 请输入以下信息:
- Key:
project_id - 值:PROJECT_ID - 您的 Google Cloud 项目 ID
- Key:
- 点击保存。
gcloud 命令
输入以下命令:
ENVIRONMENT是 Managed Airflow 环境的名称LOCATION是 Managed Airflow 环境所在的区域PROJECT_ID是包含 Managed Airflow 环境的项目的项目 ID
gcloud composer environments run ENVIRONMENT --location LOCATION variables set -- project_id PROJECT_ID - 在工具栏中,点击 Admin > Variables。
- 将以下 DAG 代码从本地复制到名为“composer-dataproc-dag.py”的文件中,该文件使用 DataprocInstantiateWorkflowTemplateOperator。
Airflow 2
Airflow 1
- 将 DAG 上传到 Cloud Storage 中的环境文件夹。上传成功完成后,点击 Managed Airflow 环境页面上的 DAGs 文件夹链接。
查看任务的状态
Airflow 界面
- 打开 Airflow 网页界面。
- 在 DAG 页面上,点击 DAG 名称(例如
dataproc_workflow_dag)。 - 在 DAG 详细信息页面上,点击 Graph View。
- 查看状态:
- 失败:任务被红色框圈起。
您还可以将指针悬停在任务上,然后查看 State: Failed。

- 成功:任务被绿色框圈起。您还可以将指针悬停在任务上,然后查看 State: Success。

- 失败:任务被红色框圈起。
您还可以将指针悬停在任务上,然后查看 State: Failed。
控制台
点击“工作流”标签页以查看工作流状态。
gcloud 命令
gcloud dataproc operations list \
--region=us-central1 \
--filter="labels.goog-dataproc-workflow-template-id=sparkpi"
清理
为避免系统向您的 Google Cloud 账号收取费用,您可以删除本教程中使用的资源: