搭配使用 Serverless for Apache Spark 與代管筆記本

本頁面說明如何使用 Google Cloud Serverless for Apache Spark,在 Vertex AI Workbench 代管筆記本執行個體的無伺服器 Spark 上執行筆記本檔案。

受管理筆記本執行個體可提交筆記本檔案的程式碼,在 Serverless for Apache Spark 服務上執行。這項服務會在代管運算基礎架構中執行程式碼,並視需要自動調度資源。因此,您不需要佈建及管理自己的叢集。

Serverless for Apache Spark 費用只會在工作負載執行時產生。

需求條件

如要在 Serverless for Apache Spark 上執行筆記本檔案,請參閱下列需求。

  • Serverless for Apache Spark 工作階段必須與受管理筆記本執行個體位於相同區域。

  • 專案不得啟用「需要 OS 登入」(constraints/compute.requireOsLogin) 限制。請參閱「管理機構中的 OS 登入」。

  • 如要在 Serverless for Apache Spark 上執行筆記本檔案,您必須提供具有特定權限的服務帳戶。您可以將這些權限授予預設服務帳戶,也可以提供自訂服務帳戶。請參閱本頁面的「權限」一節

  • Apache Spark 無伺服器工作階段會使用虛擬私有雲 (VPC) 網路執行工作負載。虛擬私有雲子網路必須符合特定條件。 請參閱「Google Cloud Serverless for Apache Spark for Spark 網路設定」一文中的相關規定。

權限

如要確保服務帳戶具備必要權限,可在 Serverless for Apache Spark 上執行筆記本檔案,請要求管理員授予服務帳戶專案的「Dataproc 編輯者」 (roles/dataproc.editor) IAM 角色。

如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

這個預先定義的角色具備在 Serverless for Apache Spark 上執行筆記本檔案所需的權限。如要查看確切的必要權限,請展開「Required permissions」(必要權限) 部分:

所需權限

如要在 Serverless for Apache Spark 上執行筆記本檔案,您必須具備下列權限:

  • dataproc.agents.create
  • dataproc.agents.delete
  • dataproc.agents.get
  • dataproc.agents.update
  • dataproc.session.create
  • dataproc.sessions.get
  • dataproc.sessions.list
  • dataproc.sessions.terminate
  • dataproc.sessions.delete
  • dataproc.tasks.lease
  • dataproc.tasks.listInvalidatedLeases
  • dataproc.tasks.reportStatus

管理員或許還可透過自訂角色或其他預先定義的角色,授予服務帳戶這些權限。

事前準備

  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.

    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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Notebooks, Vertex AI, and Dataproc APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Notebooks, Vertex AI, and Dataproc APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  8. 如尚未設定,請先設定符合「Google Cloud Serverless for Apache Spark 網路設定」所列需求的 VPC 網路。
  9. 開啟 JupyterLab

    1. 前往 Google Cloud 控制台的「Managed notebooks」頁面。

      前往代管型筆記本

    2. 按一下代管型筆記本執行個體名稱旁的「Open JupyterLab」(開啟 JupyterLab)

    啟動 Serverless for Apache Spark 工作階段

    如要啟動 Serverless for Apache Spark 工作階段,請完成下列步驟。

    1. 在受管理筆記本執行個體的 JupyterLab 介面中, 選取「啟動器」分頁標籤,然後選取「無伺服器 Spark」。 如果「啟動器」分頁未開啟,請選取「檔案」> 新啟動器開啟。

      系統會顯示「建立無伺服器 Spark 工作階段」對話方塊。

    2. 在「工作階段名稱」欄位中,輸入工作階段名稱。

    3. 在「執行設定」部分,輸入要使用的「服務帳戶」。如未輸入服務帳戶,工作階段會使用 Compute Engine 預設服務帳戶

    4. 在「網路設定」部分,選取符合Google Cloud Serverless for Apache Spark 網路設定中列出需求的網路「網路」和「子網路」

    5. 點選「建立」

      系統會開啟新的筆記本檔案。 您建立的 Serverless for Apache Spark 工作階段是執行筆記本檔案程式碼的核心。

    在 Serverless for Apache Spark 和其他核心上執行程式碼

    1. 將程式碼新增至新筆記本檔案,然後執行程式碼。

    2. 如要在其他核心上執行程式碼,請變更核心

    3. 如要再次在 Serverless for Apache Spark 工作階段中執行程式碼,請將核心改回 Serverless for Apache Spark 核心。

    終止 Serverless for Apache Spark 工作階段

    您可以在 JupyterLab 介面或 Google Cloud 控制台中,終止 Serverless for Apache Spark 工作階段。筆記本檔案中的程式碼會保留。

    JupyterLab

    1. 在 JupyterLab 中,關閉建立 Serverless for Apache Spark 工作階段時建立的筆記本檔案。

    2. 在隨即顯示的對話方塊中,按一下「終止工作階段」

    Google Cloud 控制台

    1. 前往 Google Cloud 控制台的「Dataproc sessions」(Dataproc 工作階段) 頁面。

      前往 Dataproc 工作階段

    2. 選取要終止的連線,然後按一下「終止」

    刪除 Serverless for Apache Spark 工作階段

    您可以使用 Google Cloud 控制台刪除 Serverless for Apache Spark 工作階段。筆記本檔案中的程式碼會保留。

    1. 前往 Google Cloud 控制台的「Dataproc sessions」(Dataproc 工作階段) 頁面。

      前往 Dataproc 工作階段

    2. 選取要刪除的會話,然後按一下「刪除」

    後續步驟