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
}
} | 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
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
}
}
} | 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:
| 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
}
}
} | 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:
| 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 |
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot