在本文档中,您将使用 Google Cloud的以下收费组件:
- Dataproc
 - Compute Engine
 - Cloud Composer
 
 
 
 
  如需根据您的预计使用量来估算费用,请使用价格计算器。
  
准备工作
设置项目
- 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.
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
      (
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 Cloud Composer 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. - 
      
Install the 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
      (
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 Cloud Composer 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. - 
      
Install the Google Cloud CLI.
 - 
          
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
 - 
        
如需初始化 gcloud CLI,请运行以下命令:
gcloud init - 创建 
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 - 使用托管单节点集群运行工作流。Dataproc 将创建集群,对其运行工作流,然后在工作流完成时删除集群。
gcloud dataproc workflow-templates set-managed-cluster sparkpi \ --cluster-name=sparkpi \ --single-node \ --region=us-central1 - 确认创建工作流模板。
控制台
点击 Google Cloud 控制台中的 Dataproc 工作流页面上的
sparkpi名称,以打开工作流模板详细信息页面。点击工作流模板的名称以确认sparkpi模板属性。
  gcloud 命令
运行以下命令:
gcloud dataproc workflow-templates describe sparkpi --region=us-central1 - 创建或使用现有的 Cloud Composer 环境。
 - 设置环境变量。
Airflow 界面
- 在工具栏中,点击 Admin > Variables。
          
       - 点击创建。
          
       -  请输入以下信息:
      
- Key:
project_id - 值:PROJECT_ID - 您的 Google Cloud 项目 ID
 
 - Key:
 - 点击保存。
 
gcloud 命令
输入以下命令:
ENVIRONMENT是 Cloud Composer 环境的名称LOCATION是 Cloud Composer 环境所在的地区PROJECT_ID是包含 Cloud Composer 环境的项目 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 中的环境文件夹。上传成功完成后,点击 Cloud Composer 环境页面上的 DAG 文件夹链接。
      
   - 打开 Airflow 网页界面。
 - 在 DAG 页面上,点击 DAG 名称(例如 
dataproc_workflow_dag)。 - 在 DAG 详细信息页面上,点击 Graph View。
 - 查看状态:
   
- 失败:任务被红色框圈起。
     您还可以将指针悬停在任务上,然后查看 State: Failed。

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

 
 - 失败:任务被红色框圈起。
     您还可以将指针悬停在任务上,然后查看 State: Failed。
 - 请参阅 Dataproc 工作流模板概览。
 - 请参阅工作流安排解决方案。
 
创建 Dataproc 工作流模板
在本地终端窗口或 Cloud Shell 中复制并运行以下命令,以创建和定义工作流模板。
创建 DAG 并将其上传到 Cloud Storage
查看任务的状态
Airflow 界面
控制台
点击“工作流”标签页以查看工作流状态。
  gcloud 命令
gcloud dataproc operations list \
    --region=us-central1 \
    --filter="labels.goog-dataproc-workflow-template-id=sparkpi"
    清理
为避免系统向您的 Google Cloud 账号收取费用,您可以删除本教程中使用的资源: