Updated on 2022-12-14 GMT+08:00

Submitting a SQL Statement

Function

This API is used to submit and execute a SQL statement in an MRS cluster.

URI

  • Format

    POST /v2/{project_id}/clusters/{cluster_id}/sql-execution

  • Parameter description
    Table 1 URI parameters

    Parameter

    Mandatory

    Type

    Description

    project_id

    Yes

    String

    The project ID. For details about how to obtain the project ID, see Obtaining a Project ID.

    cluster_id

    Yes

    String

    The cluster ID. For details about how to obtain the cluster ID, see Obtaining a Cluster ID.

Request Parameters

Table 2 Request parameters

Parameter

Mandatory

Type

Description

sql_type

Yes

String

The SQL type. Currently, only the SQL of the presto type is supported.

NOTE:
  • The SQL of the presto type can be submitted and executed only in clusters containing the Presto component.
  • This parameter is available only for MRS 2.0.6.1, 2.1.0.7, 3.1.2, or later clusters.

sql_content

Yes

String

The SQL statement to be executed.

NOTE:

Currently, only a single SQL statement can be executed at a time, and the statement cannot contain a semicolon (;).

database

No

String

The database where the SQL statement to be executed is located. The default value is default.

archive_path

No

String

The directory for storing the dumped SQL execution results.

NOTE:

Only the SELECT statement dumps query results. Currently, the query results can be dumped only to OBS.

Response Parameters

Status code: 200

Table 3 Response parameters

Parameter

Type

Description

id

String

The execution ID of a SQL statement. The ID is generated only when the SELECT, SHOW, or DESC statement is executed. For other operations, the ID is empty.

message

String

The error message.

statement

String

The SQL statement to be executed.

status

String

The execution status of a SQL statement. Possible values:

  • QUEUED
  • WAITING_FOR_RESOURCES
  • PLANNING
  • STARTING
  • RUNNING
  • FINISHING
  • FINISHED
  • FAILED

result_location

String

The path for archiving the final results of the SQL query statement.

NOTE:

Only the SELECT statement dumps the SQL execution results to result_location.

content

Array<Array<String>>

The execution result of the SQL statement.

NOTE:

Only non-SELECT statements return results in content. If the SQL statement does not return results, content is empty.

Status code: 400

Table 4 Response body parameters

Parameter

Type

Description

error_code

String

The error code.

error_msg

String

The error message.

Example Request

Example request for submitting a SQL statement

POST https://{endpoint}/v2/{project_id}/clusters/{cluster_id}/sql-execution

{
  "sql_type" : "presto",
  "sql_content" : "show tables",
  "database" : "default",
  "archive_path" : "obs://my-bucket/path"
}

Example Response

Status code: 200

The SQL statement is submitted successfully.

{
  "id" : "20190909_011820_00151_xxxxx",
  "statement" : "show tables",
  "status" : "FINISHED",
  "result_location" : " obs://my_bucket/uuid_date/xxxx.csv",
  "content" : [ [ "t1", null ], [ null, "t2" ], [ null, "t3" ] ]
}

Status code: 400

Failed to submit the SQL statement.

{
  "error_code" : "MRS.0011",
   "message": "Failed to submit SQL to the executor. The cluster ID is xxxx"
}

Status Codes

See Status Codes.

Error Codes

See Error Codes.