使用用戶端程式庫建立叢集
以下程式碼範例說明如何使用 Cloud 用戶端程式庫建立 Managed Service for Apache Spark 叢集、在叢集上執行工作,然後刪除叢集。
您也可以使用下列方法執行這些工作:
- API REST 要求 (請參閱「快速入門導覽課程:使用 API Explorer」)
- Google Cloud 控制台 (請參閱「使用 Google Cloud 控制台建立叢集」)
- Google Cloud CLI (請參閱「使用 Google Cloud CLI 建立叢集」)
事前準備
- 登入 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 API.
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.-
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 API.
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.
執行程式碼
嘗試逐步操作說明:按一下「Open in Cloud Shell」(在 Cloud Shell 開啟),執行 Python Cloud 用戶端程式庫逐步操作說明,建立叢集、執行 PySpark 工作,然後刪除叢集。
Go
Java
- 安裝用戶端程式庫 詳情請參閱「設定 Java 開發環境」。
- 設定驗證方法
- 複製並執行 GitHub 程式碼範例。
Node.js
- 安裝用戶端程式庫 詳情請參閱「設定 Node.js 開發環境」。
- 設定驗證方法
- 複製並執行 GitHub 程式碼範例。
Python
- 安裝用戶端程式庫 詳情請參閱「設定 Python 開發環境」。
- 設定驗證方法
- 複製並執行 GitHub 程式碼範例。
查看輸出內容
程式碼會將工作驅動程式記錄檔輸出至 Cloud Storage 的預設 Managed Service for Apache Spark 暫存 bucket。您可以在 Google Cloud 控制台的專案中,透過 Google Cloud 控制台查看 Managed Service for Apache Spark「Jobs」(工作) 頁面,查看工作驅動程式輸出內容。按一下工作 ID,即可在「Job details」(工作詳細資料) 頁面查看工作輸出內容。
後續步驟
- 請參閱 Managed Service for Apache Spark Cloud 用戶端程式庫的「其他資源」。