Submitting a Spark SQL Job
Description
This section describes how to submit a SQL job using AI DataLake SDK.
Prerequisites
- The Java 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
# Synchronous submission function submit(); # Asynchronous submission function asyncSubmit();
Sample Code
- Asynchronous SQL submission
public static void asyncSubmit(AIDataLakeSparkSqlClient sparkSqlClient) throws AIDataLakeException { String databaseName = "DATABASE_NAME"; String catalogName = "CATALOG_NAME"; String sqlStatement = "SELECT xxx"; Map<String, String> sparkConfig = new HashMap<>(); // spark conf starting with "spark." sparkConfig.put("spark.sql.shuffle.partitions", "200"); SparkSqlJob sqlJob = sparkSqlClient.newSparkSqlJobBuilder(sqlStatement) .catalogName(catalogName) .databaseName(databaseName) .sparkConfig(sparkConfig) .build(); sqlJob.asyncSubmit(); }
- Synchronous SQL submission
public static void submit(AIDataLakeSparkSqlClient sparkSqlClient) throws AIDataLakeException { String databaseName = "DATABASE_NAME"; String catalogName = "CATALOG_NAME"; String sqlStatement = "SELECT xxx"; Map<String, String> sparkConfig = new HashMap<>(); // spark conf starting with "spark." sparkConfig.put("spark.sql.shuffle.partitions", "200"); SparkSqlJob sqlJob = sparkSqlClient.newSparkSqlJobBuilder(sqlStatement) .catalogName(catalogName) .databaseName(databaseName) .sparkConfig(sparkConfig) .build(); sqlJob.submit(); }
- SQL query
public static void submit(AIDataLakeSparkSqlClient sparkSqlClient) throws AIDataLakeException { String databaseName = "DATABASE_NAME"; String catalogName = "CATALOG_NAME"; String sqlStatement = "SELECT xxx"; String statementId; Map<String, String> sparkConfig = new HashMap<>(); // spark conf starting with "spark." sparkConfig.put("spark.sql.shuffle.partitions", "200"); SparkSqlJob sqlJob = sparkSqlClient.newSparkSqlJobBuilder(sqlStatement) .catalogName(catalogName) .databaseName(databaseName) .sparkConfig(sparkConfig) .build(); sqlJob.submit(); statementId = sqlJob.getStatementId(); ResultSet resultSet = sparkSqlClient.getResultsWithStatementId(statementId).getResultSet(); while (resultSet.hasNext()) { Row row = resultSet.read(); List<Column> schema = row.getSchema(); List<Object> record = row.getRecord(); for (int i = 0; i < schema.size(); i++) { DataType.TypeName typeName = DataType.TypeName.fromName(schema.get(i).getType().getName()); switch (typeName) { case STRING: String strV = (String) record.get(i); break; case DATE: Date dtV = (Date) record.get(i); break; } } } }
Parameters
| Parameter | Mandatory (Yes/No) | Default Value | Description |
|---|---|---|---|
| databaseName | Yes | None | Default LakeFormation database name. |
| catalogName | Yes | None | Default LakeFormation catalog name. |
| sqlStatement | Yes | None | SQL statement to be submitted. |
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