运行 Apache Spark 批处理工作负载

了解如何使用 Serverless for Apache Spark 在 Dataproc 管理的计算基础架构上提交批量工作负载,该基础架构可根据需要扩缩资源。

准备工作

  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.

    Go to project selector

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

  4. Enable the Dataproc API.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

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

  7. Enable the Dataproc API.

    Enable the API

  8. 提交 Spark 批处理工作负载

    您可以使用 Google Cloud 控制台、Google Cloud CLI 或 Serverless for Apache Spark API 来创建和提交 Serverless for Apache Spark 批处理工作负载。

    控制台

    1. 在 Google Cloud 控制台中,前往 Dataproc 批次

    2. 点击创建

    3. 选择并填写以下字段,以提交用于计算 pi 近似值的 Spark 批处理工作负载:

    4. 点击提交以运行 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
    

    请替换以下内容:

    • REGION: 指定工作负载将运行的区域
    • 其他选项:您可以添加 gcloud dataproc batches submit spark 标志来指定其他工作负载选项和 Spark 属性。
      • --version: 您可以指定非默认的 Apache Spark 无服务器运行时版本
      • --jars:示例 JAR 文件预安装在 Spark 执行环境中,传递给 SparkPi 工作负载的 1000 命令实参指定了 pi 估算逻辑的 1000 次迭代(工作负载输入实参包含在“--”后面)。
      • --subnet: 您可以添加此标志来指定会话区域中子网的名称。 如果您未指定子网,Serverless for Apache Spark 会选择会话区域中的 default 子网。Serverless for Apache Spark 会在子网上启用专用 Google 访问通道 (PGA)。如需了解网络连接要求,请参阅 Google Cloud Serverless for Apache Spark 网络配置
      • --properties:您可以添加此标志,输入受支持的 Spark 属性供 Spark 批处理工作负载使用。
      • --deps-bucket: 您可以添加此标志以指定一个 Cloud Storage 存储桶,Serverless for Apache Spark 将在其中上传工作负载依赖项。存储桶的 gs:// URI 前缀不是必需的;您可以指定存储桶路径或存储桶名称。Serverless for Apache Spark 会将本地文件上传到存储桶中的 /dependencies 文件夹,然后再运行批处理工作负载。注意:如果您的批处理工作负载引用本地机器上的文件,则此标志是必需的
      • --ttl:您可以添加 --ttl 标志来指定批次生命周期时长。如果工作负载超过此时长,系统会无条件终止该工作负载,而不会等待正在进行的工作完成。使用 smhd(秒、分钟、小时或天)后缀指定时长。最小值为 10 分钟 (10m),最大值为 14 天 (14d)。
        • 1.1 或 2.0 运行时批处理:如果未为 1.1 或 2.0 运行时批处理工作负载指定 --ttl,则允许该工作负载运行直至自然退出(如果永不退出,则永远运行)。
        • 2.1 及更高版本的运行时批次:如果未为 2.1 及更高版本的运行时批次工作负载指定 --ttl,则默认值为 4h
      • --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
        1. 以下命令会在单节点 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
                       
        2. 提交批处理工作负载,并指定正在运行的 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
                        
      • 运行时版本: 使用 --version 标志可为工作负载指定 Serverless 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

    本部分介绍如何使用 Serverless for Apache Spark batches.create`pi

    在使用任何请求数据之前,请先进行以下替换:

    • project-id: Google Cloud 项目 ID。
    • region:Compute Engine 区域, Google Cloud Serverless for Apache Spark 将在该区域中运行工作负载。
    • 注意

      • 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"
        ],
        "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"
    }
    

    估算工作负载费用

    无服务器 Apache Spark 工作负载会消耗数据计算单元 (DCU) 和 shuffle 存储资源。如需查看输出 Dataproc UsageMetrics 以估算工作负载资源消耗和费用的示例,请参阅 Serverless for Apache Spark 价格

    后续步骤

    了解: