Updated on 2026-11-06 GMT+08:00

Using APIs to Connect to a Spark Endpoint and Submit Jobs

After creating a Spark endpoint on the AI DataLake console, you can submit Spark jobs to it and use its compute resources to process data. Currently, you can submit SQL, Python, and JAR jobs using APIs.

Prerequisites

  • You have obtained the workspace ID.

    Log in to the AI DataLake console and find the target workspace ID on the Workspace Management page.

  • You have obtained the Spark endpoint name (endpoint_name).

    Log in to the AI DataLake console, choose Engine Endpoints > Spark, and find the target Spark endpoint name.

  • You have created a job file based on the job type, uploaded the file to an OBS bucket, and recorded the OBS path.

Precautions

If LakeFormation is integrated, the IAM account name cannot contain hyphens (-). Otherwise, an error will occur when you submit the Spark job.

Submitting a SparkSQL Job

Use a tool, such as Postman, to call the Executing a Spark SQL Job – RunSparkSql API to submit a Spark SQL job. The following is a code example:

POST /v2/workspaces/{workspace_id}/spark-sqls

{
  "endpoint_name": "spark-endpoint-01",
  "catalog_context": {
  "catalog_name": "hive_catalog",
  "database_name": "default"
  },
  "statement": "SELECT * FROM table_name LIMIT 100",
  "timeout": {
  "queued_timeout": 180,
  "running_timeout": 720
  }
}
Table 1 SparkSQL job parameters

Parameter

Description

Example Value

endpoint_name

The endpoint name

spark-endpoint-01

catalog_name

The data catalog name

hive_catalog

database_name

The database name

default

statement

The SQL query statement

SELECT * FROM table_name LIMIT 100

queued_timeout

The queue timeout duration

180

running_timeout

The execution timeout duration

720

Submitting a Spark JAR Job

Use a tool such as Postman to call the API for Starting a Spark Job – RunSparkJob to submit a Spark JAR job. The following is a code example:
POST /v2/workspaces/{workspace_id}/spark-jobs

{
  "name": "spark-jar-job-example",
  "endpoint_name": "spark-endpoint",
  "job_config": {
    "type": "spark_jar_job",
    "main_class": "com.example.SparkJobDriver",
    "main_jar": "obs://bucket/path/job.jar",
    "main_args": [
      "--input",
      "obs://bucket/input",
      "--output",
      "obs://bucket/output"
    ]
  },
  "image": {
    "image_id": "dd877a59-5cbd-44c5-b8a7-dc464cca****",
    "image_version_id": "01ae8ee6-af8a-48a8-895b-8d4a70bf****"
  },
  "resource_config": {
    "executor_number": 2,
    "driver_resource_spec": {
      "cpu": 2000,
      "memory": 4096
    },
    "executor_resource_spec": {
      "cpu": 2000,
      "memory": 4096
    }
  }
}
Table 2 Spark JAR job parameters

Parameter

Description

Example Value

name

The job name

spark-jar-job-example

endpoint_name

The endpoint name

spark-endpoint

type

The job type

spark_jar_job

main_class

The main class name that specifies the entry class of the Spark JAR job.

com.example.SparkJobDriver

main_jar

The main JAR package path

obs://bucket/path/job.jar

main_args

Entry class parameters

"--input",

"obs://bucket/input",

"--output",

"obs://bucket/output"

image_id

The image ID. To obtain this value, call the API for Listing Custom Images.

dd877a59-5cbd-44c5-b8a7-dc464cca****

image_version_id

NOTE:

If the resource allocation mode of the endpoint is Hybrid or On-demand and an image snapshot is created for the selected image version, the snapshot is automatically associated when you select the image version. When the endpoint starts, the snapshot is automatically used to create image resources. No additional operation is required. For details about how to create an image snapshot, see Image Acceleration.

The image version. To view the image version, perform the following steps:

  1. On the AI DataLake console, choose Image Management in the left navigation pane.
  2. On the Built-in Images or Custom Images tab, click the image name.
  3. Verify the version of the built-in or custom image.
    • Check the built-in image version: Click the Versions tab, and view the image version in the Image Version column.
    • Check the custom image version: View the image version in the Image Version column.

      If no custom image is available, register one manually. For details, see Registering a Custom Image.

20e48735-f273-413c-a0f5-4cd4544f****

executor_number

The number of executors

2

driver_resource_spec

Driver resources

2 vCPUs | 4 GB

executor_resource_spec

Executor resources

2 vCPUs | 4 GB

Submitting a Spark Python Job

Use a tool such as Postman to call the API for Starting a Spark Job – RunSparkJob to submit a Spark Python job. The following is a code example:

POST /v2/workspaces/{workspace_id}/spark-jobs

{
  "name": "python-job-example",
  "endpoint_name": "spark-endpoint",
  "job_config": {
    "type": "spark_python_job",
    "main_python_file": "obs://bucket/path/job.py",
    "main_args": [
      "--input-path",
      "obs://bucket/input"
    ]
  },
  "image": {
    "image_id": "dd877a59-5cbd-44c5-b8a7-dc464cca****",
    "image_version_id": "01ae8ee6-af8a-48a8-895b-8d4a70bf****"
  },
  "resource_config": {
    "executor_number": 2,
    "driver_resource_spec": {
      "cpu": 2000,
      "memory": 4096
    },
    "executor_resource_spec": {
      "cpu": 2000,
      "memory": 4096
    }
  }
}
Table 3 Spark Python job parameters

Parameter

Description

Example Value

name

The job name

python-job-example

endpoint_name

The endpoint name

spark-endpoint

type

The job type

spark_python_job

main_python_file

The Python path

obs://bucket/path/job.py

--input-path

Entry class parameters

obs://bucket/input

image_id

The image ID. To obtain this value, call the API for Listing Custom Images.

dd877a59-5cbd-44c5-b8a7-dc464cca****

image_version_id

NOTE:

If the resource allocation mode of the endpoint is Hybrid or On-demand and an image snapshot is created for the selected image version, the snapshot is automatically associated when you select the image version. When the endpoint starts, the snapshot is automatically used to create image resources. No additional operation is required. For details about how to create an image snapshot, see Image Acceleration.

The image version. To view the image version, perform the following steps:

  1. On the AI DataLake console, choose Image Management in the left navigation pane.
  2. On the Built-in Images or Custom Images tab, click the image name.
  3. Verify the version of the built-in or custom image.
    • Check the built-in image version: Click the Versions tab, and view the image version in the Image Version column.
    • Check the custom image version: View the image version in the Image Version column.

      If no custom image is available, register one manually. For details, see Registering a Custom Image.

20e48735-f273-413c-a0f5-4cd4544f****

executor_number

The number of executors

2

driver_resource_spec

Driver resources

2 vCPUs | 4 GB

executor_resource_spec

Executor resources

2 vCPUs | 4 GB