Timeseries queries

These types of queries take a timeseries query object and return an array of JSON objects where each object represents a value asked for by the timeseries query.

An example timeseries query object is shown below:

  1. {
  2. "queryType": "timeseries",
  3. "dataSource": "sample_datasource",
  4. "granularity": "day",
  5. "descending": "true",
  6. "filter": {
  7. "type": "and",
  8. "fields": [
  9. { "type": "selector", "dimension": "sample_dimension1", "value": "sample_value1" },
  10. { "type": "or",
  11. "fields": [
  12. { "type": "selector", "dimension": "sample_dimension2", "value": "sample_value2" },
  13. { "type": "selector", "dimension": "sample_dimension3", "value": "sample_value3" }
  14. ]
  15. }
  16. ]
  17. },
  18. "aggregations": [
  19. { "type": "longSum", "name": "sample_name1", "fieldName": "sample_fieldName1" },
  20. { "type": "doubleSum", "name": "sample_name2", "fieldName": "sample_fieldName2" }
  21. ],
  22. "postAggregations": [
  23. { "type": "arithmetic",
  24. "name": "sample_divide",
  25. "fn": "/",
  26. "fields": [
  27. { "type": "fieldAccess", "name": "postAgg__sample_name1", "fieldName": "sample_name1" },
  28. { "type": "fieldAccess", "name": "postAgg__sample_name2", "fieldName": "sample_name2" }
  29. ]
  30. }
  31. ],
  32. "intervals": [ "2012-01-01T00:00:00.000/2012-01-03T00:00:00.000" ]
  33. }

There are 7 main parts to a timeseries query:

propertydescriptionrequired?
queryTypeThis String should always be “timeseries”; this is the first thing Apache Druid looks at to figure out how to interpret the queryyes
dataSourceA String or Object defining the data source to query, very similar to a table in a relational database. See DataSource for more information.yes
descendingWhether to make descending ordered result. Default is false(ascending).no
intervalsA JSON Object representing ISO-8601 Intervals. This defines the time ranges to run the query over.yes
granularityDefines the granularity to bucket query results. See Granularitiesyes
filterSee Filtersno
aggregationsSee Aggregationsno
postAggregationsSee Post Aggregationsno
limitAn integer that limits the number of results. The default is unlimited.no
contextCan be used to modify query behavior, including grand totals and zero-filling. See also Context for parameters that apply to all query types.no

To pull it all together, the above query would return 2 data points, one for each day between 2012-01-01 and 2012-01-03, from the “sample_datasource” table. Each data point would be the (long) sum of sample_fieldName1, the (double) sum of sample_fieldName2 and the (double) result of sample_fieldName1 divided by sample_fieldName2 for the filter set. The output looks like this:

  1. [
  2. {
  3. "timestamp": "2012-01-01T00:00:00.000Z",
  4. "result": { "sample_name1": <some_value>, "sample_name2": <some_value>, "sample_divide": <some_value> }
  5. },
  6. {
  7. "timestamp": "2012-01-02T00:00:00.000Z",
  8. "result": { "sample_name1": <some_value>, "sample_name2": <some_value>, "sample_divide": <some_value> }
  9. }
  10. ]

Grand totals

Druid can include an extra “grand totals” row as the last row of a timeseries result set. To enable this, add "grandTotal" : true to your query context. For example:

  1. {
  2. "queryType": "timeseries",
  3. "dataSource": "sample_datasource",
  4. "intervals": [ "2012-01-01T00:00:00.000/2012-01-03T00:00:00.000" ],
  5. "granularity": "day",
  6. "aggregations": [
  7. { "type": "longSum", "name": "sample_name1", "fieldName": "sample_fieldName1" },
  8. { "type": "doubleSum", "name": "sample_name2", "fieldName": "sample_fieldName2" }
  9. ],
  10. "context": {
  11. "grandTotal": true
  12. }
  13. }

The grand totals row will appear as the last row in the result array, and will have no timestamp. It will be the last row even if the query is run in “descending” mode. Post-aggregations in the grand totals row will be computed based upon the grand total aggregations.

Zero-filling

Timeseries queries normally fill empty interior time buckets with zeroes. For example, if you issue a “day” granularity timeseries query for the interval 2012-01-01/2012-01-04, and no data exists for 2012-01-02, you will receive:

  1. [
  2. {
  3. "timestamp": "2012-01-01T00:00:00.000Z",
  4. "result": { "sample_name1": <some_value> }
  5. },
  6. {
  7. "timestamp": "2012-01-02T00:00:00.000Z",
  8. "result": { "sample_name1": 0 }
  9. },
  10. {
  11. "timestamp": "2012-01-03T00:00:00.000Z",
  12. "result": { "sample_name1": <some_value> }
  13. }
  14. ]

Time buckets that lie completely outside the data interval are not zero-filled.

You can disable all zero-filling with the context flag “skipEmptyBuckets”. In this mode, the data point for 2012-01-02 would be omitted from the results.

A query with this context flag set would look like:

  1. {
  2. "queryType": "timeseries",
  3. "dataSource": "sample_datasource",
  4. "granularity": "day",
  5. "aggregations": [
  6. { "type": "longSum", "name": "sample_name1", "fieldName": "sample_fieldName1" }
  7. ],
  8. "intervals": [ "2012-01-01T00:00:00.000/2012-01-04T00:00:00.000" ],
  9. "context" : {
  10. "skipEmptyBuckets": "true"
  11. }
  12. }