提交 Apache Spark 批次工作負載

瞭解如何在 Managed Service for Apache Spark 運算基礎架構上提交批次工作負載,並視需要調度資源。

事前準備

設定專案,並視需要授予 Identity and Access Management 角色。

設定專案

視需要執行下列一或多個步驟:

  1. 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

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

  3. Enable the Dataproc API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

視需要授予 IAM 角色

您必須具備特定 IAM 角色,才能執行本頁中的範例。視組織政策而定,這些角色可能已授予。如要查看角色授予,請參閱「是否需要授予角色?」一節。

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

使用者角色

如要取得提交無伺服器批次工作負載所需的權限,請要求管理員授予您下列 IAM 角色:

服務帳戶角色

為確保 Compute Engine 預設服務帳戶具備提交無伺服器批次工作負載所需的權限,請要求管理員將專案的 Dataproc 工作站 (roles/dataproc.worker) IAM 角色授予 Compute Engine 預設服務帳戶。

提交 Spark 批次工作負載

您可以使用 Google Cloud 控制台、Google Cloud CLI 或 REST API,建立及提交 Managed Service for Apache Spark 批次工作負載。

控制台

  1. 在 Google Cloud 控制台中,前往 Managed Service for Apache Spark 的「Jobs」(工作) 頁面。

    前往 Managed Service for Apache Spark 工作

  2. 按一下「Submit job」(提交工作),開啟「Submit a job」(提交工作) 頁面。

  3. 選取並填寫下列欄位,提交計算圓周率近似值的 Spark 批次工作負載:

    • 選取工作的運算類型:選取「無伺服器 Spark」。
    • 無伺服器工作詳細資料:
      • 「Job ID」(工作 ID):指定批次工作負載的 ID (或接受預設 ID)。這個值必須介於 4 至 63 個小寫字元之間,開頭和結尾須為小寫英文字母或數字,且只能使用小寫英文字母、數字和連字號 (/[a-z0-9-]/)。
      • 執行階段設定檔:在「Regions」(區域) 清單中選取區域,然後在「Runtime profiles」(執行階段設定檔) 清單中選取「Default service configuration」(預設服務設定) (或已儲存的執行階段設定檔)。
      • 設定執行階段設定檔 (選用):在「Summary」(摘要) 卡片中,按一下「Configure」(設定) (或按一下「Runtime profile」(執行階段設定檔) 選單中的「Create new profile」(建立新設定檔)),開啟執行階段設定檔設定面板,您可以在其中自訂下列設定,然後按一下「Use configuration」(使用設定) (或「Save new profile」(儲存新設定檔)):
        • 「Tier」(級別) 和「Executor configuration」(執行器設定):選取運算級別 (「Premium」或「Standard」) 和執行器型態。
        • 其他設定:
          • 「Runtime configuration」(執行階段設定):按一下「Edit」(編輯),確認或選取 3.0 Spark 執行階段版本,或指定自訂 Spark 映像檔、Cloud Storage 暫存 bucket 或 Python 套件存放區。
          • 「Metastore configuration」(Metastore 設定):按一下「Edit」(編輯),設定工作負載使用 Lakehouse 目錄或外部自行管理的 Dataproc Metastore。
          • 網路和安全性:按一下「Edit」(編輯),選取「Service Account」(服務帳戶) (根據預設,工作負載會使用 Compute Engine 預設服務帳戶執行,您也可以指定具備 Dataproc Worker 角色的自訂服務帳戶),然後選取工作負載區域中的「Subnetwork」(子網路) (Managed Service for Apache Spark 會在指定的子網路上啟用 Private Google Access (PGA),詳情請參閱「Managed Service for Apache Spark 網路設定」)。
    • 檔案和構件:
      • 工作類型:選取「Spark (Scala / Java)」。
      • 「Driver configuration」(驅動程式設定):選取「Main class」(主要類別)。
      • Main class:
        org.apache.spark.examples.SparkPi
      • 引數:輸入 1000,然後按下 Enter。
      • Jar 檔案 (這個檔案已預先安裝在 Managed Service for Apache Spark 執行環境中):
        file:///usr/lib/spark/examples/jars/spark-examples.jar
    • 工作執行和屬性:
      • 「Spark properties」(Spark 屬性):按一下「Add property」(新增屬性),輸入支援的 Spark 屬性「Key」(鍵) (屬性名稱) 和「Value」(值),以便在 Spark 批次工作負載設定。注意:與 Managed Service for Apache Spark 叢集屬性不同,Managed Service for Apache Spark 工作負載屬性不含 spark: 前置字串。
      • 標籤:(選用) 按一下「新增標籤」,將「鍵」和「值」標籤組合附加至工作負載。
  4. 按一下「Submit」(提交),執行 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 會在子網路上啟用Private Google Access (PGA)。如需網路連線需求,請參閱「Managed Service for Apache Spark 網路設定」。
    • --tags:您可以新增這個旗標,指定用於流量控管的網路標記。使用網路標記限制連線。在正式環境中,建議您將防火牆規則限制為 Spark 工作負載使用的 IP 位址。
    • --properties:您可以新增這個旗標,輸入 Spark 批次工作負載要使用的支援 Spark 屬性。
    • --deps-bucket:您可以新增這個旗標,指定 Managed Service for Apache Spark 要將工作負載依附元件上傳至哪個 Cloud Storage bucket。您不一定要提供 bucket 的 gs:// URI 前置字串,只要指定 bucket 路徑或 bucket 名稱即可。Managed Service for Apache Spark 會先將本機檔案上傳至 bucket 中的 /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。
    • --service-account:您可以指定要用於執行工作負載的服務帳戶。如未指定服務帳戶,工作負載會在 Compute Engine 預設服務帳戶下執行。服務帳戶必須具備 Dataproc 工作者角色。
    • 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 ...
              
    • 永久記錄伺服器:
      1. 下列指令會在單一節點的 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
                     
      2. 提交批次工作負載,並指定執行中的永久記錄伺服器。
        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 旗標,指定工作負載的 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

本節說明如何使用 Managed Service for Apache Spark batches.create` 建立批次工作負載,以計算 pi 的近似值。

使用任何要求資料之前,請先修改下列項目的值:

  • project-id: Google Cloud 專案 ID。
  • region:Managed Service for Apache Spark 執行工作負載的 Compute Engine 區域。
  • 注意:

    • PROJECT_ID:您的 Google Cloud 專案 ID。 專案 ID 會列在 Google Cloud 控制台資訊主頁的「Project info」(專案資訊) 部分。
    • 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) 和重組儲存空間資源。如需輸出 Managed Service for Apache Spark UsageMetrics 的範例,以預估工作負載資源消耗量和費用,請參閱「Managed Service for Apache Spark 定價」。

後續步驟

瞭解下列內容: