将 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 Login”(constraints/compute.requireOsLogin) 限制条件。请参阅管理组织中的 OS Login

  • 如需在 Serverless for Apache Spark 上运行笔记本文件,您必须提供具有特定权限的服务账号。您可以将这些权限授予默认服务账号或提供自定义服务账号。请参阅此页面的“权限”部分

  • Serverless for Apache Spark 会话使用虚拟私有云 (VPC) 网络来执行工作负载。 VPC 子网必须满足特定要求。请参阅 Google Cloud Serverless for Apache Spark 网络配置中的要求。

权限

为了确保服务账号拥有在 Serverless for Apache Spark 上运行笔记本文件所需的权限,请让您的管理员为服务账号授予项目的 Dataproc Editor (roles/dataproc.editor) IAM 角色。

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

此预定义角色包含在 Serverless for Apache Spark 上运行笔记本文件所需的权限。如需查看所需的确切权限,请展开所需权限部分:

所需权限

在 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 (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 (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 控制台中,打开托管式笔记本页面。

      前往“代管式笔记本”

    2. 在代管式笔记本实例的名称旁边,点击打开 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 会话页面。

      前往“Dataproc 会话”

    2. 选择您要终止的会话,然后点击终止

    删除 Serverless for Apache Spark 会话

    您可以使用 Google Cloud 控制台删除 Serverless for Apache Spark 会话。笔记本文件中的代码会被保留。

    1. 在 Google Cloud 控制台中,前往 Dataproc 会话页面。

      前往“Dataproc 会话”

    2. 选择您要删除的会话,然后点击删除

    后续步骤