Introduction
IBM Cloud SQL Query is a cloud-native service that lets you use ANSI SQL to analyze rectangular data stored in IBM Cloud Object Storage. It is hosted on IBM Cloud and includes a publicly accessible REST API. The V2 API documentation intends to help you get going with the SQL Query API, and it offers resources on how to operationalize it.
The SQL Query V2 REST interface is deprecated, therefore use the V3 API of Data Engine, which is backward compatible.
API endpoint
https://api.sql-query.cloud.ibm.com/
Running an SQL query
You can use the SQL Query service REST V2 API to run queries and retrieve information about their status. This is especially helpful when writing code that automatically queries data.
SQL Query provides the following REST APIs:
- POST request to submit a new SQL job
- GET request to receive details about a specific SQL job
- GET request to receive a list of submitted SQL jobs
Before you can call any of the above REST APIs, create an IAM bearer token and have your SQL Query instance Cloud Resource Name (CRN) available. You find the CRN in the SQL Query instance dashboard that provides a copy button to get it into your clipboard.
Creating an IAM bearer token
The recommended method to retrieve an IAM token programmatically is to create an API key for your IBM Cloud identity and then use the IAM token API to exchange that key for a token. Each token is valid only for one hour, and after a token expires you have to create a new one if you want to continue using the API.
You can create a token in IBM Cloud or by using the IBM Cloud command line interface (CLI).
To create a token in the IBM Cloud:
- Log in to IBM Cloud and select Manage > Security > Platform API Keys.
- Create an API key for your own personal identity, copy the key value, and save it in a secure place. After you leave the page, you will no longer be able to access this value.
- With your API key, set up Postman or another REST API tool and run the command to the right.
- Use the value of the
access_token
property for your SQL Query API calls. Set theaccess_token
value as the authorization header parameter for requests to the Watson Data APIs. The format isAuthorization: Bearer <access_token_value_here>
. For example:
Authorization: Bearer eyJraWQiOiIyMDE3MDgwOS0wMDowMDowMCIsImFsZyI6IlJTMjU2In0...
To create a token by using the IBM Cloud CLI:
Follow the steps to install the CLI, log in to IBM Cloud, and get the token described here.
Curl command with API key to retrieve token
curl "https://iam.cloud.ibm.com/identity/token" -d "apikey=YOUR_API_KEY_HERE&grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey" -H "Content-Type: application/x-www-form-urlencoded" -H "Authorization: Basic Yng6Yng="
Response
{
"access_token": "eyJraWQiOiIyMDE3MDgwOS0wMDowMDowMCIsImFsZyI6...",
"refresh_token": "zmRTQFKhASUdF76Av6IUzi9dtB7ip8F2XV5fNgoRQ0mbQgD5XCeWkQhjlJ1dZi8K...",
"token_type": "Bearer",
"expires_in": 3600,
"expiration": 1505865282
}
Error handling
This API uses standard HTTP response codes to indicate if a method completed successfully:
- A 200 or 201 type response indicates success.
- A 400 type response indicates an error in the specified parameters.
- A 401 type response indicates an authorization error.
- A 404 type response indicates that a resource related to this request was not found.
Rate limiting
Rate limits for API POST requests are enforced for each instance. If the number of POST requests for an instance reaches the request limit, no further requests are accepted until one of the SQL query jobs that are running for that instance finishes.
An HTTP status code of 429 indicates that the rate limit has been exceeded.
The number of requests that are allowed depends on your plan.
Resources
- Getting started tutorial
- How to run basic queries
- How to use the SQL Query REST API
- How to operationalize SQL code and call it from an application
- How to connect to IBM Cloud Object Storage from the command Line
- Sample Python client for interacting with the SQL Query APIs
- Example using cloud functions with SQL Query APIs
Methods
Run an SQL job
Runs an SQL job and stores the result either in IBM Cloud Object Storage or IBM® Db2® on Cloud. The FROM
clause references rectangular data that is stored in Parquet, CSV, ORC, AVRO or JSON format in IBM Cloud Object Storage. For more information, see the SQL Query overview documentation.
POST /sql_jobs
Request
Query Parameters
The cloud resource name (CRN) of the SQL query service instance.
SQL job specification
{
"statement": "SELECT firstname FROM cos://us-geo/sql/employees.parquet STORED AS PARQUET WHERE city = 'London' INTO cos://us-geo/target-bucket/q1-results"
}
This is the SQL statement to be submitted. The table names specified in the FROM clause must correspond to objects in one or more IBM Cloud Object Storage instances or to table definitions specified in the database catalog. The INTO clause of the query indicates the endpoint, bucket, and (optionally) subfolder in IBM Cloud Object Storage or a Db2 table URI, in which the query result is to be stored. Alternatively, a Database Definition Language statement for table or index management can be specified. Within the specified target, each result is stored in a separate subfolder with a name that indicates the job ID. The job ID of a query is returned by the GET endpoint.
Possible values: length ≥ 1
This field provides an alternative way to specify the target URI for a query. It is supported to preserve backward compatibility and will be removed in a future API version. Use the INTO clause of the SQL query to specify the target URI instead.
Possible values: length ≥ 1
curl -XPOST --url "https://api.sql-query.cloud.ibm.com/v2/sql_jobs?instance_crn=YOUR_SQL_QUERY_CRN" -H "Accept: application/json" -H "Authorization: Bearer YOUR_BEARER_TOKEN" -H "Content-Type: application/json" -d '{"statement":"SELECT firstname FROM cos://us-geo/sql/employees.parquet STORED AS PARQUET WHERE EMPLOYEEID=5 INTO cos://us-geo/target-bucket/q1-results" }'
Response
Abridged information about an SQL job, including its identifier and processing status.
Identifier for an SQL job.
Example:
637a0b7d-069d-453f-a418-a35d4db3ea64
Execution status of an SQL job.
Possible values: [
queued
,running
,completed
,failed
]ID of the user who submitted an SQL job.
Example:
test_user@my.org
Timestamp indicating when an SQL job was accepted by the service.
Boolean indicating when an SQL job has an improvement hint.
Status Code
Successful submission. Returns information about the SQL job, including its status and an identifier for future reference.
The request provided an invalid job specification or other invalid data. Returns details about the validation problem.
The request did not specify a valid bearer token for authentication.
You are not authorized to perform this action for the specified service instance.
The request requires an unsupported output format. SQL Query produces JSON output with UTF-8 encoding. A request cannot be processed if its header specifies that this format is not accepted.
This instance is currently running its maximum number of SQL jobs. A new job can be accepted only after at least one of the currently running jobs completes.
An internal error occurred while processing the request.
{ "job_id": "7ebed7f7-00dc-44a2-acfa-5bdb53889648", "status": "queued" }
Get information about recent SQL jobs
Returns information about recently submitted SQL jobs.
GET /sql_jobs
Request
Query Parameters
The cloud resource name (CRN) of the SQL query service instance.
curl -XGET --url "https://api.sql-query.cloud.ibm.com/v2/sql_jobs?instance_crn=YOUR_SQL_QUERY_CRN" -H "Accept: application/json" -H "Authorization: Bearer YOUR_BEARER_TOKEN"
Response
List of information about SQL jobs.
The SQL jobs.
Status Code
Information about recently submitted SQL jobs. The list might be empty.
The request specified invalid data, for example, incorrect headers. Returns details about the validation problem.
The request did not specify a valid bearer token for authentication.
You are not authorized to perform this action for the specified service instance.
The request requires an unsupported output format. SQL Query produces JSON output with UTF-8 encoding. A request cannot be processed if its header specifies that this format is not accepted.
An internal error occurred while processing the request.
{ "jobs": [ { "job_id": "7ebed7f7-00dc-44a2-acfa-5bdb53889648", "status": "completed", "submit_time": "2018-08-14T08:45:54.012Z", "user_id": "user1@ibm.com" }, { "job_id": "ffde4c5a-1cc2-448b-b377-43573818e5d8", "status": "completed", "submit_time": "2018-08-14T08:47:33.350Z", "user_id": "user1@ibm.com" } ] }
Get information about a specific SQL job
Returns information about the specified SQL job.
GET /sql_jobs/{job_id}
Request
Path Parameters
ID of the SQL job for which information is to be retrieved. This ID is returned when an SQL job is submitted, and when information about recently submitted SQL jobs is requested.
Query Parameters
The cloud resource name (CRN) of the SQL query service instance.
curl -XGET --url "https://api.sql-query.cloud.ibm.com/v2/sql_jobs/YOUR_JOB_ID?instance_crn=YOUR_SQL_QUERY_CRN" -H "Accept: application/json" -H "Authorization: Bearer YOUR_BEARER_TOKEN"
Response
Full information about an SQL job, including output or error information.
Identifier for an SQL job.
Example:
637a0b7d-069d-453f-a418-a35d4db3ea64
Execution status of an SQL job.
Possible values: [
queued
,running
,completed
,failed
]ID of the user who submitted an SQL job.
Example:
test_user@my.org
Timestamp indicating when an SQL job was accepted by the service.
The SQL query that the job processes.
The service plan id of the instance.
Format of the query result.
Example:
csv
A URI that indicates where the query result is stored. This URI can be used as input for another SQL query. The result comprises all objects that have a name with this URI as its prefix.
Example:
cos://s3.dal.us.cloud-object-storage.appdomain.cloud/target-bucket/q1-results/jobid=411323a4-04de-440a-9e41-011d31052f54
Timestamp indicating when the job finished processing.
Number of rows returned.
Number of rows read.
Number of bytes read.
Number of objects skipped using index management.
Number of objects qualified using index management.
An error that was encountered while processing the job.
Detailed information about the error.
Suggests possible optimizations for a query.
Status Code
Status of the specified SQL job.
The request specified invalid data, for example, incorrect headers. Returns details about the validation problem.
The request did not specify a valid bearer token for authentication.
You are not authorized to perform this action for the specified service instance.
No information was found for the specified job. Note that the system periodically deletes information about completed or failed jobs.
The request requires an unsupported output format. SQL Query produces JSON output with UTF-8 encoding. A request cannot be processed if its header specifies that this format is not accepted.
An internal error occurred while processing the request.
{ "job_id": "7ebed7f7-00dc-44a2-acfa-5bdb53889648", "status": "completed", "statement": "SELECT e.firstname employee, e.city FROM cos://us-geo/sql/employees.parquet STORED AS PARQUET e", "plan_id": "e03a38d0-5ec1-41c5-b3b3-5e081dc19c8c", "submit_time": "2018-08-14T08:45:54.012Z", "resultset_location": "cos://s3.us.cloud-object-storage.appdomain.cloud/result/test/jobid=7ebed7f7-00dc-44a2-acfa-5bdb53889648", "resultset_format": "parquet", "rows_returned": 9, "rows_read": 9, "bytes_read": 4928, "end_time": "2018-08-14T08:46:01.516Z", "user_id": "user1@ibm.com" }
List catalog tables
Retrieve a list of the first 100 tables that are defined for the given instance in the catalog.
GET /tables
Request
Query Parameters
The cloud resource name (CRN) of the SQL query service instance.
A table name pattern for filtering the tables that should be listed. The pattern follows Hive syntax conventions and can include asterisks as wildcards and vertical bars to separate alternatives.
A table type for filtering the tables that should be listed, can be "table" or "view".
curl -XGET --url "https://api.sql-query.cloud.ibm.com/v2/tables?instance_crn=YOUR_SQL_QUERY_CRN" -H "Accept: application/json" -H "Authorization: Bearer YOUR_BEARER_TOKEN"
Response
List of catalog tables.
The table names.
Metadata about the returned tables.
- tables_metadata
The name of a catalog table.
Example:
customer_address
The type of a catalog table (for example, "TABLE" or "VIEW").
Example:
table
Status Code
Names of defined catalog tables. The list might be empty.
The request specified invalid data, for example, incorrect headers. Returns details about the validation problem.
The request did not specify a valid bearer token for authentication.
You are not authorized to perform this action for the specified service instance.
The request requires an unsupported output format. SQL Query produces JSON output with UTF-8 encoding. A request cannot be processed if its header specifies that this format is not accepted.
The client has submitted too many requests for catalog information. Retry the request after a short wait time.
An internal error occurred while processing the request.
{ "tables": [ "employees", "customers", "products", "orders" ] }
Get information about a specific catalog table
Returns information about the specified catalog table.
GET /tables/{table_name}
Request
Path Parameters
Name of the catalog table for which information is to be retrieved. Table names are case-insensitive and must only contain alphabetic and numeral characters, and underscore (_).
Query Parameters
The cloud resource name (CRN) of the SQL query service instance.
curl -XGET --url "https://api.sql-query.cloud.ibm.com/v2/tables/YOUR_TABLE_NAME?instance_crn=YOUR_SQL_QUERY_CRN" -H "Accept: application/json" -H "Authorization: Bearer YOUR_BEARER_TOKEN"
Response
Detailed information about a catalog table.
The name of a catalog table.
Example:
customer_address
The type of a catalog table (for example, "TABLE" or "VIEW").
Example:
table
The columns of the table.
- columns
The name of the column.
The data type of the column.
Example:
string
Whether the column may contain NULL values.
Status Code
Information about the specified table.
The request specified invalid data, for example, incorrect headers. Returns details about the validation problem.
The request did not specify a valid bearer token for authentication.
You are not authorized to perform this action for the specified service instance.
The specified table is not in the catalog
The request requires an unsupported output format. SQL Query produces JSON output with UTF-8 encoding. A request cannot be processed if its header specifies that this format is not accepted.
The client has submitted too many requests for catalog information. Retry the request after a short wait time.
An internal error occurred while processing the request.
{ "columns": [ { "name": "employeeID", "type": "integer", "nullable": true }, { "name": "lastName", "type": "string", "nullable": true }, { "name": "firstName", "type": "string", "nullable": true }, { "name": "birthDate", "type": "timestamp", "nullable": true }, { "name": "hireDate", "type": "timestamp", "nullable": true }, { "name": "city", "type": "string", "nullable": true } ] }