Query data by API
After you have created one or more tables and ingested data, you can use the Query API to run queries against your data. For information on how to write SQL queries in Imply Polaris, see Querying data.
This topic shows how to query a table in Polaris using Druid SQL.
Prerequisites
This topic assumes you have an OAuth token with the ReadDataSources
role.
In the examples below, the token value is stored in the variable named IMPLY_TOKEN
.
See Authenticate API requests if you need to configure an OAuth-based client and obtain an access token.
Visit User roles reference for more information on roles and their permissions.
Get project ID
All queries to Polaris sent via API must specify the project ID.
To obtain the project ID, make a GET
request to the projects
endpoint.
Sample request
The following example shows how to obtain project information from your Polaris environment:
curl --location --request GET 'https://api.imply.io/v1/projects' \
--header "Authorization: Bearer $IMPLY_TOKEN"
import requests
url = "https://api.imply.io/v1/projects"
headers = {
'Authorization': 'Bearer {token}'.format(token=IMPLY_TOKEN)
}
response = requests.get(url, headers=headers)
print(response.text)
Sample response
The following example shows a successful response for project information.
The project ID is displayed in the uid
field of the JSON response.
[
{
"metadata":{
"uid":"677b1203-fe4c-4d63-b01c-90254e104jcs",
"name":"default"
},
"spec":{
"plan":"a.02",
"desiredState":"RUNNING"
},
"status":{
"maxBytes":200000000000,
"currentBytes":240081263,
"state":"RUNNING"
}
}
]
Query using Druid SQL
To submit a Druid SQL query via the API, send a POST
request along with a JSON body containing your query.
Replace {project_id}
with your project ID.
Change the SQL statement in the query
field to modify the query.
Sample request
The following example shows how to submit a query using the API:
curl --location --request POST 'https://api.imply.io/v1/projects/{project_id}/query/sql' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header "Authorization: Bearer $IMPLY_TOKEN" \
--data-raw '{
"query": "SELECT continent, COUNT(*) AS counts FROM \"Koalas to the Max\" GROUP BY 1 ORDER BY counts DESC"
}'
import requests
import json
url = "https://api.imply.io/v1/projects/{project_id}/query/sql"
payload = json.dumps({
"query": "SELECT continent, COUNT(*) AS counts FROM \"Koalas to the Max\" GROUP BY 1 ORDER BY counts DESC"
})
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer {token}'.format(token=IMPLY_TOKEN)
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
Sample response
The following example shows a successful response containing the query result:
[
{
"continent": "North America",
"counts": 241671
},
{
"continent": "Europe",
"counts": 169433
},
{
"continent": "Oceania",
"counts": 35905
},
{
"continent": "Asia",
"counts": 29902
},
{
"continent": "South America",
"counts": 25336
},
{
"continent": "Africa",
"counts": 2263
},
{
"continent": "",
"counts": 922
}
]
Learn more
See the following topics for more information:
- Query API for details on the Polaris Query API.
- Druid SQL documentation for reference on Druid SQL queries.
- Query using JDBC for querying data in Polaris using JDBC.