在本文件中,您會使用 Google Cloud的下列計費元件:
- Dataproc
- Compute Engine
- Cloud Composer
您可以使用 Pricing Calculator 根據預測用量產生預估費用。
事前準備
設定專案
- 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.
Make sure that billing is enabled for your Google Cloud project.
Enable the Dataproc, Compute Engine, and Cloud Composer APIs.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
To initialize the gcloud CLI, run the following command:
gcloud init
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Make sure that billing is enabled for your Google Cloud project.
Enable the Dataproc, Compute Engine, and Cloud Composer APIs.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
To initialize the gcloud CLI, run the following command:
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 UI
- 在工具列中,依序按一下「管理」>「變數」。
- 按一下「建立」。
- 輸入下列資訊:
- 鍵:
project_id
- Val:PROJECT_ID - 您的 Google Cloud 專案 ID
- 鍵:
- 按一下 [儲存]。
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
- 在工具列中,依序按一下「管理」>「變數」。
- 將下列 DAG 程式碼複製到本機的「composer-dataproc-dag.py」檔案中,該檔案會使用 DataprocInstantiateWorkflowTemplateOperator。
Airflow 2
Airflow 1
- 將DAG上傳至 Cloud Storage 中的環境資料夾。上傳完成後,請按一下 Cloud Composer 環境頁面中的「DAG 資料夾」連結。
- 開啟 Airflow 網頁介面。
- 在 DAG 頁面中,按一下 DAG 名稱 (例如
dataproc_workflow_dag
)。 - 在 DAG 詳細資料頁面中,按一下「圖表檢視」。
- 檢查狀態:
- 失敗:工作會以紅色方框標示。您也可以將游標懸停在工作上,然後查看「狀態:失敗」。
- 成功:工作周圍有綠色方塊。您也可以將滑鼠游標懸停在工作上,確認工具提示是否顯示「State: Success」。
- 失敗:工作會以紅色方框標示。您也可以將游標懸停在工作上,然後查看「狀態:失敗」。
- 請參閱「Dataproc 工作流程範本總覽」
- 請參閱工作流程排程解決方案
建立 Dataproc 工作流程範本
在本機終端機視窗或 Cloud Shell 中複製並執行下列指令,即可建立並定義工作流程範本。
建立 DAG 並上傳至 Cloud Storage
查看工作狀態
Airflow UI
控制台
按一下「工作流程」分頁標籤,查看工作流程狀態。

gcloud 指令
gcloud dataproc operations list \ --region=us-central1 \ --filter="labels.goog-dataproc-workflow-template-id=sparkpi"
正在清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取費用,您可以刪除本教學課程中使用的資源: