了解如何使用 Dataproc Serverless 在 Dataproc 管理的计算基础架构上提交批处理工作负载,该基础架构会根据需要扩缩资源。
准备工作
- 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 API.
-
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 API.
在 Google Cloud 控制台中,前往 Dataproc 批次。
点击创建。
选择并填写以下字段,提交用于计算 pi 近似值的 Spark 批处理工作负载:
- 批处理信息:
- 批处理 ID:为批处理工作负载指定一个 ID。此值必须为 4-63 个小写字符。有效字符为
/[a-z][0-9]-/
。 - 区域:选择将在其中运行工作负载的区域。
- 批处理 ID:为批处理工作负载指定一个 ID。此值必须为 4-63 个小写字符。有效字符为
- 容器:
- 批处理类型:Spark。
- 运行时版本:系统会选择默认的运行时版本。(可选)您可以指定非默认的 Dataproc Serverless 运行时版本。
- 主要类:
org.apache.spark.examples.SparkPi
- jar 文件(此文件预安装在 Dataproc Serverless Spark 执行环境中)。
file:///usr/lib/spark/examples/jars/spark-examples.jar
- 参数:1000。
- 执行配置:您可以指定要用于运行工作负载的服务账号。如果您未指定服务账号,则工作负载将在 Compute Engine 默认服务账号下运行。您的服务账号必须具有 Dataproc Worker 角色。
- 网络配置:在会话区域中选择一个子网。Dataproc Serverless 会在指定的子网上启用专用 Google 访问通道 (PGA)。如需了解网络连接性要求,请参阅 Dataproc Serverless for Spark 网络配置。
- 属性:输入要在 Spark 批处理工作负载上设置的受支持的 Spark 属性的
Key
(属性名称)和Value
。注意:与 Dataproc on Compute Engine 集群属性不同,Dataproc Serverless for Spark 工作负载属性不包含spark:
前缀。 - 其他选项:
- 您可以将批处理工作负载配置为使用外部自行管理的 Hive Metastore。
- 您可以使用 Persistent History Server (PHS)。PHS 必须位于您运行批处理工作负载的区域。
- 批处理信息:
点击提交以运行 Spark 批处理工作负载。
- REGION: 指定将在其中运行工作负载的区域。
- 其他选项:您可以添加
gcloud dataproc batches submit spark
标志来指定其他工作负载选项和 Spark 属性。--jars
:示例 JAR 文件预安装在 Spark 执行环境中,传递给 SparkPi 工作负载的1000
命令参数指定了 pi 估算逻辑的 1000 次迭代(工作负载输入参数包含在“--”后面)。--subnet
:您可以添加此标志,以指定会话区域中的子网名称。如果您未指定子网,Dataproc Serverless 会在会话区域中选择default
子网。Dataproc Serverless 会在子网上启用专用 Google 访问通道 (PGA)。如需了解网络连接性要求,请参阅 Dataproc Serverless for Spark 网络配置。--properties
:您可以添加此标志,输入要供 Spark 批处理工作负载使用的受支持的 Spark 属性。--deps-bucket
:您可以添加此标志以指定一个 Cloud Storage 存储桶,Dataproc Serverless 将在其中上传工作负载依赖项。存储桶的gs://
URI 前缀不是必需的;您可以指定存储桶路径或存储桶名称。运行批处理工作负载之前,Dataproc Serverless for Spark 会将本地文件上传到存储桶中的/dependencies
文件夹。注意:如果您的批处理工作负载引用本地机器上的文件,则必须添加此标志。--ttl
:您可以添加--ttl
标志以指定批处理生命周期的时长。如果工作负载超出此时长,系统会无条件终止该工作负载,而不会等待正在进行的工作完成。使用s
、m
、h
或d
(秒、分钟、小时或天)后缀指定时长。最小值为 10 分钟 (10m
),最大值为 14 天 (14d
)。- 1.1 或 2.0 运行时批处理:如果未为 1.1 或 2.0 运行时批处理工作负载指定
--ttl
,则允许该工作负载运行到自然退出(如果不退出,则永久运行)。 - 2.1 及更高版本的运行时批处理作业:如果未为 2.1 或更高版本的运行时批处理作业指定
--ttl
,则默认为4h
。
- 1.1 或 2.0 运行时批处理:如果未为 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 ...
- 永久性历史记录服务器:
- 以下命令会在单节点 Dataproc 集群上创建 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
- 以下命令会在单节点 Dataproc 集群上创建 PHS。PHS 必须位于您运行批处理工作负载的区域,并且 Cloud Storage bucket-name 必须存在。
- 运行时版本:使用
--version
标志指定工作负载的 Dataproc Serverless 运行时版本。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
- project-id: Google Cloud 项目 ID。
- region:Dataproc Serverless 将在其中运行工作负载的 Compute Engine 区域。
- PROJECT_ID:您的 Google Cloud 项目 ID。 项目 ID 列在 Google Cloud 控制台信息中心的项目信息部分中。
- REGION:会话区域。
提交 Spark 批处理工作负载
您可以使用 Google Cloud 控制台、Google Cloud CLI 或 Dataproc Serverless API 创建和提交 Dataproc Serverless for Spark 批处理工作负载。
控制台
gcloud
如需提交 Spark 批处理工作负载以计算 pi
的近似值,请在终端窗口或 Cloud Shell 中本地运行以下 gcloud CLI gcloud dataproc batches submit spark
命令。
gcloud dataproc batches submit spark \ --region=REGION \ --jars=file:///usr/lib/spark/examples/jars/spark-examples.jar \ --class=org.apache.spark.examples.SparkPi \ -- 1000
请替换以下内容:
API
本部分介绍如何使用 Dataproc Serverless for Spark batches.create
` 创建批处理工作负载以计算 pi
的近似值。
在使用任何请求数据之前,请先进行以下替换:
注意:
HTTP 方法和网址:
POST https://dataproc.googleapis.com/v1/projects/project-id/locations/region/batches
请求 JSON 正文:
{ "sparkBatch":{ "args":[ "1000" ], "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":{ "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" }
估算工作负载费用
Dataproc Serverless for Spark 工作负载会消耗数据计算单元 (DCU) 和 shuffle 存储资源。如需查看输出 Dataproc UsageMetrics 以估算工作负载资源消耗和费用的示例,请参阅 Dataproc Serverless 价格。
后续步骤
了解: