了解如何在 Managed Service for Apache Spark 计算基础架构上提交批处理工作负载,该基础架构会根据需要扩缩资源。
准备工作
设置项目,并根据需要授予 Identity and Access Management 角色。
设置项目
根据需要执行以下一个或多个步骤:
- 登录您的 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.
根据需要授予 IAM 角色
您需要拥有某些 IAM 角色才能运行本页面的示例。根据组织政策,这些角色可能已授予。如需检查角色授予情况,请参阅 是否需要授予角色?。
如需详细了解如何授予角色,请参阅 管理对项目、文件夹和组织的访问权限。
用户角色
如需获得提交无服务器批处理工作负载所需的权限,请让您的管理员为您授予以下 IAM 角色:
-
Dataproc Editor (
roles/dataproc.editor) 针对项目的 -
Service Account User (
roles/iam.serviceAccountUser) 针对 Compute Engine 默认服务帐号的
服务账号角色
如需确保 Compute Engine 默认服务帐号具有提交无服务器批处理工作负载所需的
权限,
请让您的管理员向项目中的 Compute Engine 默认服务帐号授予
Dataproc Worker (roles/dataproc.worker)
IAM 角色。
提交 Spark 批处理工作负载
您可以使用 Google Cloud 控制台、Google Cloud CLI 或 REST API 来创建和提交 Managed Service for Apache Spark 批处理工作负载。
控制台
在 Google Cloud 控制台中,前往 Managed Service for Apache Spark 批处理。
点击创建 。
选择并填写以下字段,以提交用于计算 pi 近似值的 Spark 批处理工作负载:
- 批处理信息:
- 批处理 ID:指定批处理工作负载的 ID。此值必须为 4-63 个小写字符。有效字符为
/[a-z][0-9]-/。 - 区域:选择将在其中运行工作负载的区域 。
- 批处理 ID:指定批处理工作负载的 ID。此值必须为 4-63 个小写字符。有效字符为
- 容器:
- 批处理类型:Spark。
- 运行时版本:确认或选择
3.0运行时版本。 - 主类:
org.apache.spark.examples.SparkPi
- Jar 文件 (此文件预安装在 Managed Service for Apache Spark 执行环境中)。
file:///usr/lib/spark/examples/jars/spark-examples.jar
- 参数:1000。
- 执行配置: 选择 Service Account (服务账号)。默认情况下, 批处理将使用 Compute Engine 默认服务账号运行。您可以指定自定义服务帐号。 默认或自定义服务帐号必须具有 Dataproc Worker 角色。
- 网络配置: 在会话区域中选择一个子网 。Managed Service for Apache Spark 会在 指定的子网中启用 专用 Google 访问通道 (PGA)。如需了解网络连接要求,请参阅 Managed Service for Apache Spark 网络配置。
- 属性: 输入
Key(属性名称)和Value,以在 Spark 批处理工作负载中设置 受支持的 Spark 属性 。注意:与 Managed Service for Apache Spark 集群属性不同, Managed Service for Apache Spark 工作负载属性不包含spark:前缀。 - 其他选项:
- 您可以将批处理工作负载配置为使用外部 自行管理式 Hive Metastore。
- 您可以使用 Persistent History Server (PHS)。 PHS 必须位于您运行批处理工作负载的区域。
- 批处理信息:
点击提交 以运行 Spark 批处理工作负载。
gcloud
如需提交 Spark 批处理工作负载以计算近似值
pi,请在终端窗口或
Cloud Shell中本地运行以下 gcloud CLI
gcloud dataproc batches submit spark
命令。
gcloud dataproc batches submit spark \ --region=REGION \ --version=3.0 \ --jars=file:///usr/lib/spark/examples/jars/spark-examples.jar \ --class=org.apache.spark.examples.SparkPi \ -- 1000
请替换以下内容:
- REGION: 指定您的工作负载将在其中运行的 区域 。
- 其他选项: 您可以添加
gcloud dataproc batches submit spark标志,以指定其他工作负载选项和 Spark 属性。--jars:示例 JAR 文件预安装在 Spark 执行环境中,传递给 SparkPi 工作负载的1000命令参数指定了 pi 估算逻辑的 1000 次迭代(工作负载输入参数包含在“-- ”后面)。--subnet:您可以添加此标志,以指定会话区域中子网的名称。如果您未指定子网,Managed Service for Apache Spark 会在会话区域中选择default子网。Managed Service for Apache Spark 会在 子网中启用专用 Google 访问通道 (PGA)。如需了解网络连接要求,请参阅 Managed Service for Apache Spark 网络配置。--tags: 您可以添加此标志,以指定用于流量控制的网络标记。使用网络 标记来限制连接。在生产环境中,建议的做法是 将防火墙规则限制为 Spark 工作负载使用的 IP 地址。--properties:您可以添加此标志,以输入 受支持的 Spark 属性 供 Spark 批处理工作负载使用。--deps-bucket:您可以添加此标志,以指定 Managed Service for Apache Spark 将在其中上传工作负载依赖项的 Cloud Storage 存储桶。存储桶的gs://URI 前缀不是必需的;您可以指定存储桶路径或存储桶名称。Managed Service for Apache Spark 会先将本地 文件上传到存储桶中的/dependencies文件夹,然后再运行 批处理工作负载。注意: 如果您的批处理 工作负载引用本地机器上的文件,则必须 使用此标志。--ttl:您可以添加--ttl标志,以指定批处理生命周期的时长。当工作负载超出 此时长时,系统会无条件终止该工作负载,而不会等待 正在进行的工作完成。使用s、m、h或d(秒、分钟、小时或天)后缀指定时长。最小值是 10 分钟 (10m), 最大值是 14 天 (14d)。- 1.1 或 2.0 运行时批处理: 如果未指定
--ttl,则允许 1.1 或 2.0 运行时批处理工作负载运行,直到其自然退出(如果工作负载不退出,则会永远运行)。 - 2.1+ 运行时批处理: 如果未指定
--ttl(适用于 2.1 或更高版本运行时批处理工作负载),则默认值为4h。
- 1.1 或 2.0 运行时批处理: 如果未指定
--service-account:您可以指定用于运行工作负载的服务账号。如果您未指定服务帐号, 则工作负载将在 Compute Engine 默认服务账号下运行。 您的服务帐号必须具有 Dataproc Worker 角色。- Hive Metastore:以下命令将批处理工作负载配置为使用具有标准 Spark 配置的外部
自行管理式 Hive Metastore
。
gcloud dataproc batches submit spark\ --properties=spark.sql.catalogImplementation=hive,spark.hive.metastore.uris=METASTORE_URI,spark.hive.metastore.warehouse.dir=WAREHOUSE_DIR> \ other args ... - Persistent History Server:
- 以下命令会在单节点 Managed Service for Apache Spark
集群上创建 PHS。PHS 必须位于您运行批处理工作负载的区域,
并且 Cloud Storage bucket-name 必须
存在。
gcloud dataproc clusters create PHS_CLUSTER_NAME \ --region=REGION \ --single-node \ --enable-component-gateway \ --properties=spark:spark.history.fs.logDirectory=gs://bucket-name/phs/*/spark-job-history
- 提交批处理工作负载,并指定正在运行的 Persistent History Server。
gcloud dataproc batches submit spark \ --region=REGION \ --jars=file:///usr/lib/spark/examples/jars/spark-examples.jar \ --class=org.apache.spark.examples.SparkPi \ --history-server-cluster=projects/project-id/regions/region/clusters/PHS-cluster-name \ -- 1000
- 以下命令会在单节点 Managed Service for Apache Spark
集群上创建 PHS。PHS 必须位于您运行批处理工作负载的区域,
并且 Cloud Storage bucket-name 必须
存在。
- 运行时版本:使用
--version标志为工作负载指定 Managed Service for Apache Spark 运行时版本。gcloud dataproc batches submit spark \ --region=REGION \ --jars=file:///usr/lib/spark/examples/jars/spark-examples.jar \ --class=org.apache.spark.examples.SparkPi \ --version=VERSION -- 1000
API
本部分介绍如何创建批处理工作负载
以计算pi的近似值
,方法是使用 Managed Service for Apache Spark
batches.create`
在使用任何请求数据之前, 请先进行以下替换:
- project-id:一个 Google Cloud 项目 ID。
- region:Managed Service for Apache Spark 将在其中运行工作负载的 Compute Engine 区域 。
- PROJECT_ID:您的 Google Cloud 项目 ID。 项目 ID 列在项目信息 部分中,位于 控制台 Google Cloud 信息中心。
- REGION:会话区域。
注意:
HTTP 方法和网址:
POST https://dataproc.googleapis.com/v1/projects/project-id/locations/region/batches
请求 JSON 正文:
{
"sparkBatch":{
"args":[
"1000"
],
"runtimeConfig": {
"version": "2.3",
},
"jarFileUris":[
"file:///usr/lib/spark/examples/jars/spark-examples.jar"
],
"mainClass":"org.apache.spark.examples.SparkPi"
}
}
如需发送您的请求,请展开以下选项之一:
您应该收到类似以下内容的 JSON 响应:
{
"name":"projects/project-id/locations/region/batches/batch-id",
"uuid":",uuid",
"createTime":"2021-07-22T17:03:46.393957Z",
"sparkBatch":{
"mainClass":"org.apache.spark.examples.SparkPi",
"args":[
"1000"
],
"jarFileUris":[
"file:///usr/lib/spark/examples/jars/spark-examples.jar"
]
},
"runtimeInfo":{
"outputUri":"gs://dataproc-.../driveroutput"
},
"state":"SUCCEEDED",
"stateTime":"2021-07-22T17:06:30.301789Z",
"creator":"account-email-address",
"runtimeConfig":{
"version":"2.3",
"properties":{
"spark:spark.executor.instances":"2",
"spark:spark.driver.cores":"2",
"spark:spark.executor.cores":"2",
"spark:spark.app.name":"projects/project-id/locations/region/batches/batch-id"
}
},
"environmentConfig":{
"peripheralsConfig":{
"sparkHistoryServerConfig":{
}
}
},
"operation":"projects/project-id/regions/region/operation-id"
}
估算工作负载费用
Managed Service for Apache Spark 工作负载会消耗数据计算单元 (DCU) 和 Shuffle 存储资源。如需查看输出 Managed Service for Apache Spark UsageMetrics 以估算工作负载资源消耗和费用的示例,请参阅 Managed Service for Apache Spark 价格。
后续步骤
了解: