Submitting a Spark Job
Description
This section describes how to submit a Spark job using AI DataLake SDK.
Prerequisites
- The Python SDK environment has been configured and initialized. For details, see Obtaining and Initializing the Spark SDK.
- Permissions related to APIs and SQL jobs have been configured. For details, see Permissions and Supported Actions.
- The compute resources and endpoints in use are available.
Functions
- SparkJarJob
# Synchronous submission function submit_and_wait(); # Asynchronous submission function submit();
- SparkPythonJob
# Synchronous submission function submit_and_wait(); # Asynchronous submission function submit();
- SparkSqlScriptingJob
# Synchronous submission function submit_and_wait(); # Asynchronous submission function submit();
Sample Code
- spark_jar_job
try: jar_job = client.new_jar_job_builder( "test-jar-job", "org.example.WordCount", "obs://obs-bucket/spark-jar-test-1.0-SNAPSHOT.jar", ) \ .endpoint_name("endpoint_name") \ .catalog_name("catalog_name") \ .main_args(["500000"]) \ .job_agency("job_agency") \ .driver_cpu(1000).driver_memory(4096) \ .executor_cpu(1000).executor_memory(4096) \ .executor_number(2) \ .labels([{"key": "env", "value": "dev"}]) \ .spark_config({ "spark.dynamicAllocation.enabled": "true", "spark.dynamicAllocation.maxExecutors": "3", }) \ .build() # Submit asynchronously, then wait jar_job.submit_and_wait() logger.info("JAR job completed. ID: %s, Status: %s", jar_job.job_id, jar_job.status) except AidatalakeException as e: logger.error("Example 1 FAILED: %s", e)Table 1 Parameter description Parameter
Mandatory (Yes/No)
Default Value
Description
endpoint_name
Yes
None
SparkJob endpoint name.
catalog_name
No
None
Default LakeFormation catalog name.
This parameter is optional if Spark.sql is executed within the job.
main_args
No
None
Arguments of the Main class.
job_agency
No
None
IAM agency for the job, which is used by users to access OBS or Lakeformation.
spark_config
No
None
Spark parameters.
driver_cpu
No
None
Number of millicores in the Driver pod.
driver_memory
No
None
Memory size of the Driver pod, in MB.
executor_cpu
No
None
Number of millicores in the Executor pod.
executor_memory
No
None
Memory size of the Executor pod, in MB.
executor_number
No
None
Number of Executor instances.
- spark_python_job
try: python_job = client.new_python_job_builder( "test-python-job", "obs://obs-bucket/sleep_1.py", ) \ .endpoint_name("endpoint_name") \ .catalog_name("catalog_name") \ .main_args(["500000"]) \ .job_agency("job_agency") \ .dependency_py_files(["obs://obs-bucket/utils.py"]) \ .driver_cpu(1000).driver_memory(4096) \ .executor_cpu(1000).executor_memory(4096) \ .executor_number(1) \ .build() python_job.submit_and_wait() logger.info("Python job completed. ID: %s, Status: %s", python_job.job_id, python_job.status) except AidatalakeException as e: logger.error("Example 2 FAILED: %s", e)Table 2 Parameter description Parameter
Mandatory (Yes/No)
Default Value
Description
endpoint_name
Yes
None
SparkJob endpoint name.
catalog_name
No
None
Default LakeFormation catalog name.
This parameter is optional if Spark.sql is executed within the job.
main_args
No
None
Arguments of the Main class.
job_agency
No
None
IAM agency for the job, which is used by users to access OBS or Lakeformation.
dependency_py_files
No
None
Dependent Python files.
driver_cpu
No
None
Number of millicores of the Driver pod.
driver_memory
No
None
Memory size of the Driver pod, in MB.
executor_cpu
No
None
Number of millicores of the Executor pod.
executor_memory
No
None
Memory size of the Executor pod, in MB.
executor_number
No
None
Number of Executor instances.
- spark_sql_scripting_job
try: sql_scripting_job = client.new_sql_scripting_job_builder( "test-sql-scripting-job", ) \ .sql_scripting_file("obs://obs-bucket/test_sql.sql") \ .sql_scripting_result_to_obs(True) \ .endpoint_name("endpoint_name") \ .catalog_name("catalog_name_sql") \ .job_agency("job_agency") \ .driver_cpu(1000).driver_memory(4096) \ .executor_cpu(1000).executor_memory(4096) \ .executor_number(1) \ .build() sql_scripting_job.submit_and_wait() logger.info("SQL Scripting job completed. ID: %s, Status: %s", sql_scripting_job.job_id, sql_scripting_job.status) # Read results from OBS result = sql_scripting_job.get_result() row_count = 0 for row in result: row_count += 1 if row_count <= 10: logger.info(" Row %d: %s", row_count, row) logger.info("Total rows: %d", row_count) if hasattr(result, 'close'): result.close() except AidatalakeException as e: logger.error("Example 3 FAILED: %s", e)Table 3 Parameter description Parameter
Mandatory (Yes/No)
Default Value
Description
sql_scripting_file
Yes
None
OBS path to the SQL scripting file.
endpoint_name
Yes
None
SparkJob endpoint name.
catalog_name
No
None
Default LakeFormation catalog name.
This parameter is optional if Spark.sql is executed within the job.
job_agency
No
None
IAM agency for the job, which is used by users to access OBS or Lakeformation.
driver_cpu
No
None
Number of millicores of the Driver pod.
driver_memory
No
None
Memory size of the Driver pod, in MB.
executor_cpu
No
None
Number of millicores of the Executor pod.
executor_memory
No
None
Memory size of the Executor pod, in MB.
executor_number
No
None
Number of Executor instances.
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