2018-12-13 14:47:20 -05:00
---
2019-08-21 00:48:59 -04:00
id: querying
2019-05-06 15:31:51 -04:00
title: "Native queries"
2019-08-21 00:48:59 -04:00
sidebar_label: "Making native queries"
2018-12-13 14:47:20 -05:00
---
2018-11-13 12:38:37 -05:00
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
2015-05-05 17:07:32 -04:00
2020-01-03 12:33:19 -05:00
> Apache Druid supports two query languages: [Druid SQL](sql.md) and native queries. Druid SQL
2019-08-21 00:48:59 -04:00
> queries are planned into native queries. This document describes the native query language.
2016-02-28 18:41:33 -05:00
2019-05-06 15:31:51 -04:00
Native queries in Druid are JSON objects and are typically issued to the Broker or Router processes. Queries can be
posted like this:
```bash
curl -X POST '< queryable_host > :< port > /druid/v2/?pretty' -H 'Content-Type:application/json' -H 'Accept:application/json' -d @< query_json_file >
```
2019-08-21 00:48:59 -04:00
Druid's native query language is JSON over HTTP, although many members of the community have contributed different
2019-05-15 17:49:50 -04:00
[client libraries ](/libraries.html ) in other languages to query Druid.
2015-05-05 17:07:32 -04:00
2018-10-12 17:29:14 -04:00
The Content-Type/Accept Headers can also take 'application/x-jackson-smile'.
2019-05-06 15:31:51 -04:00
```bash
curl -X POST '< queryable_host > :< port > /druid/v2/?pretty' -H 'Content-Type:application/json' -H 'Accept:application/x-jackson-smile' -d @< query_json_file >
```
2018-10-12 17:29:14 -04:00
Note: If Accept header is not provided, it defaults to value of 'Content-Type' header.
2019-08-21 00:48:59 -04:00
Druid's native query is relatively low level, mapping closely to how computations are performed internally. Druid queries
are designed to be lightweight and complete very quickly. This means that for more complex analysis, or to build
2015-12-31 19:32:51 -05:00
more complex visualizations, multiple Druid queries may be required.
2015-05-05 17:07:32 -04:00
2019-05-06 15:31:51 -04:00
Even though queries are typically made to Brokers or Routers, they can also be accepted by
2019-08-21 00:48:59 -04:00
[Historical ](../design/historical.md ) processes and by [Peons (task JVMs) ](../design/peons.md )) that are running
2019-05-06 15:31:51 -04:00
stream ingestion tasks. This may be valuable if you want to query results for specific segments that are served by
specific processes.
2019-08-21 00:48:59 -04:00
## Available queries
2015-05-05 17:07:32 -04:00
Druid has numerous query types for various use cases. Queries are composed of various JSON properties and Druid has different types of queries for different use cases. The documentation for the various query types describe all the JSON properties that can be set.
2019-08-21 00:48:59 -04:00
### Aggregation queries
2015-05-05 17:07:32 -04:00
2019-08-21 00:48:59 -04:00
* [Timeseries ](../querying/timeseriesquery.md )
* [TopN ](../querying/topnquery.md )
* [GroupBy ](../querying/groupbyquery.md )
2015-05-05 17:07:32 -04:00
2019-08-21 00:48:59 -04:00
### Metadata queries
2015-05-05 17:07:32 -04:00
2019-08-21 00:48:59 -04:00
* [TimeBoundary ](../querying/timeboundaryquery.md )
* [SegmentMetadata ](../querying/segmentmetadataquery.md )
* [DatasourceMetadata ](../querying/datasourcemetadataquery.md )
2015-05-05 17:07:32 -04:00
2019-08-21 00:48:59 -04:00
### Search queries
2015-05-05 17:07:32 -04:00
2019-08-21 00:48:59 -04:00
* [Search ](../querying/searchquery.md )
2015-05-05 17:07:32 -04:00
2019-08-21 00:48:59 -04:00
## Which query should I use?
2015-05-05 17:07:32 -04:00
Where possible, we recommend using [Timeseries]() and [TopN]() queries instead of [GroupBy](). GroupBy is the most flexible Druid query, but also has the poorest performance.
Timeseries are significantly faster than groupBy queries for aggregations that don't require grouping over dimensions. For grouping and sorting over a single dimension,
topN queries are much more optimized than groupBys.
2019-08-21 00:48:59 -04:00
## Query cancellation
2015-05-05 17:07:32 -04:00
Queries can be cancelled explicitly using their unique identifier. If the
query identifier is set at the time of query, or is otherwise known, the following
2019-01-30 22:41:07 -05:00
endpoint can be used on the Broker or Router to cancel the query.
2015-05-05 17:07:32 -04:00
```sh
DELETE /druid/v2/{queryId}
```
For example, if the query ID is `abc123` , the query can be cancelled as follows:
```sh
curl -X DELETE "http://host:port/druid/v2/abc123"
```
More useful query errors. (#3335)
Follow-up to #1773, which meant to add more useful query errors but
did not actually do so. Since that patch, any error other than
interrupt/cancel/timeout was reported as `{"error":"Unknown exception"}`.
With this patch, the error fields are:
- error, one of the specific strings "Query interrupted", "Query timeout",
"Query cancelled", or "Unknown exception" (same behavior as before).
- errorMessage, the message of the topmost non-QueryInterruptedException
in the causality chain.
- errorClass, the class of the topmost non-QueryInterruptedException
in the causality chain.
- host, the host that failed the query.
2016-08-09 04:14:52 -04:00
2019-08-21 00:48:59 -04:00
## Query errors
More useful query errors. (#3335)
Follow-up to #1773, which meant to add more useful query errors but
did not actually do so. Since that patch, any error other than
interrupt/cancel/timeout was reported as `{"error":"Unknown exception"}`.
With this patch, the error fields are:
- error, one of the specific strings "Query interrupted", "Query timeout",
"Query cancelled", or "Unknown exception" (same behavior as before).
- errorMessage, the message of the topmost non-QueryInterruptedException
in the causality chain.
- errorClass, the class of the topmost non-QueryInterruptedException
in the causality chain.
- host, the host that failed the query.
2016-08-09 04:14:52 -04:00
If a query fails, you will get an HTTP 500 response containing a JSON object with the following structure:
```json
{
"error" : "Query timeout",
"errorMessage" : "Timeout waiting for task.",
"errorClass" : "java.util.concurrent.TimeoutException",
"host" : "druid1.example.com:8083"
}
```
2020-03-10 05:57:16 -04:00
If a query request fails due to being limited by the [query scheduler laning configuration ](../configuration/index.md#broker ), an HTTP 429 response with the same JSON object schema as an error response, but with `errorMessage` of the form: "Total query capacity exceeded" or "Query capacity exceeded for lane 'low'".
More useful query errors. (#3335)
Follow-up to #1773, which meant to add more useful query errors but
did not actually do so. Since that patch, any error other than
interrupt/cancel/timeout was reported as `{"error":"Unknown exception"}`.
With this patch, the error fields are:
- error, one of the specific strings "Query interrupted", "Query timeout",
"Query cancelled", or "Unknown exception" (same behavior as before).
- errorMessage, the message of the topmost non-QueryInterruptedException
in the causality chain.
- errorClass, the class of the topmost non-QueryInterruptedException
in the causality chain.
- host, the host that failed the query.
2016-08-09 04:14:52 -04:00
The fields in the response are:
|field|description|
|-----|-----------|
|error|A well-defined error code (see below).|
|errorMessage|A free-form message with more information about the error. May be null.|
|errorClass|The class of the exception that caused this error. May be null.|
|host|The host on which this error occurred. May be null.|
Possible codes for the *error* field include:
|code|description|
|----|-----------|
|`Query timeout`|The query timed out.|
|`Query interrupted`|The query was interrupted, possibly due to JVM shutdown.|
|`Query cancelled`|The query was cancelled through the query cancellation API.|
2016-08-09 13:50:56 -04:00
|`Resource limit exceeded`|The query exceeded a configured resource limit (e.g. groupBy maxResults).|
2018-04-26 16:06:25 -04:00
|`Unauthorized request.`|The query was denied due to security policy. Either the user was not recognized, or the user was recognized but does not have access to the requested resource.|
2019-03-26 18:20:24 -04:00
|`Unsupported operation`|The query attempted to perform an unsupported operation. This may occur when using undocumented features or when using an incompletely implemented extension.|
More useful query errors. (#3335)
Follow-up to #1773, which meant to add more useful query errors but
did not actually do so. Since that patch, any error other than
interrupt/cancel/timeout was reported as `{"error":"Unknown exception"}`.
With this patch, the error fields are:
- error, one of the specific strings "Query interrupted", "Query timeout",
"Query cancelled", or "Unknown exception" (same behavior as before).
- errorMessage, the message of the topmost non-QueryInterruptedException
in the causality chain.
- errorClass, the class of the topmost non-QueryInterruptedException
in the causality chain.
- host, the host that failed the query.
2016-08-09 04:14:52 -04:00
|`Unknown exception`|Some other exception occurred. Check errorMessage and errorClass for details, although keep in mind that the contents of those fields are free-form and may change from release to release.|