blob: b84b035e94a524e2a56bb87ead43dd055567f738 [file] [log] [blame]
"use strict";(self.webpackChunk=self.webpackChunk||[]).push([[1081],{3905:(e,t,n)=>{n.d(t,{Zo:()=>m,kt:()=>c});var a=n(67294);function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function l(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function i(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?l(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):l(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function s(e,t){if(null==e)return{};var n,a,r=function(e,t){if(null==e)return{};var n,a,r={},l=Object.keys(e);for(a=0;a<l.length;a++)n=l[a],t.indexOf(n)>=0||(r[n]=e[n]);return r}(e,t);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);for(a=0;a<l.length;a++)n=l[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}var o=a.createContext({}),u=function(e){var t=a.useContext(o),n=t;return e&&(n="function"==typeof e?e(t):i(i({},t),e)),n},m=function(e){var t=u(e.components);return a.createElement(o.Provider,{value:t},e.children)},p="mdxType",d={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},g=a.forwardRef((function(e,t){var n=e.components,r=e.mdxType,l=e.originalType,o=e.parentName,m=s(e,["components","mdxType","originalType","parentName"]),p=u(n),g=r,c=p["".concat(o,".").concat(g)]||p[g]||d[g]||l;return n?a.createElement(c,i(i({ref:t},m),{},{components:n})):a.createElement(c,i({ref:t},m))}));function c(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var l=n.length,i=new Array(l);i[0]=g;var s={};for(var o in t)hasOwnProperty.call(t,o)&&(s[o]=t[o]);s.originalType=e,s[p]="string"==typeof e?e:r,i[1]=s;for(var u=2;u<l;u++)i[u]=n[u];return a.createElement.apply(null,i)}return a.createElement.apply(null,n)}g.displayName="MDXCreateElement"},2009:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>m,contentTitle:()=>o,default:()=>c,frontMatter:()=>s,metadata:()=>u,toc:()=>p});var a=n(87462),r=n(63366),l=(n(67294),n(3905)),i=["components"],s={id:"timeseriesquery",title:"Timeseries queries",sidebar_label:"Timeseries"},o=void 0,u={unversionedId:"querying/timeseriesquery",id:"querying/timeseriesquery",title:"Timeseries queries",description:"\x3c!--",source:"@site/docs/28.0.0/querying/timeseriesquery.md",sourceDirName:"querying",slug:"/querying/timeseriesquery",permalink:"/docs/28.0.0/querying/timeseriesquery",draft:!1,tags:[],version:"current",frontMatter:{id:"timeseriesquery",title:"Timeseries queries",sidebar_label:"Timeseries"},sidebar:"docs",previous:{title:"Query context",permalink:"/docs/28.0.0/querying/query-context"},next:{title:"TopN",permalink:"/docs/28.0.0/querying/topnquery"}},m={},p=[{value:"Grand totals",id:"grand-totals",level:2},{value:"Zero-filling",id:"zero-filling",level:2}],d={toc:p},g="wrapper";function c(e){var t=e.components,n=(0,r.Z)(e,i);return(0,l.kt)(g,(0,a.Z)({},d,n,{components:t,mdxType:"MDXLayout"}),(0,l.kt)("admonition",{type:"info"},(0,l.kt)("p",{parentName:"admonition"}," Apache Druid supports two query languages: ",(0,l.kt)("a",{parentName:"p",href:"/docs/28.0.0/querying/sql"},"Druid SQL")," and ",(0,l.kt)("a",{parentName:"p",href:"/docs/28.0.0/querying/"},"native queries"),".\nThis document describes a query\ntype in the native language. For information about when Druid SQL will use this query type, refer to the\n",(0,l.kt)("a",{parentName:"p",href:"/docs/28.0.0/querying/sql-translation#query-types"},"SQL documentation"),".")),(0,l.kt)("p",null,"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."),(0,l.kt)("p",null,"An example timeseries query object is shown below:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-json"},'{\n "queryType": "timeseries",\n "dataSource": "sample_datasource",\n "granularity": "day",\n "descending": "true",\n "filter": {\n "type": "and",\n "fields": [\n { "type": "selector", "dimension": "sample_dimension1", "value": "sample_value1" },\n { "type": "or",\n "fields": [\n { "type": "selector", "dimension": "sample_dimension2", "value": "sample_value2" },\n { "type": "selector", "dimension": "sample_dimension3", "value": "sample_value3" }\n ]\n }\n ]\n },\n "aggregations": [\n { "type": "longSum", "name": "sample_name1", "fieldName": "sample_fieldName1" },\n { "type": "doubleSum", "name": "sample_name2", "fieldName": "sample_fieldName2" }\n ],\n "postAggregations": [\n { "type": "arithmetic",\n "name": "sample_divide",\n "fn": "/",\n "fields": [\n { "type": "fieldAccess", "name": "postAgg__sample_name1", "fieldName": "sample_name1" },\n { "type": "fieldAccess", "name": "postAgg__sample_name2", "fieldName": "sample_name2" }\n ]\n }\n ],\n "intervals": [ "2012-01-01T00:00:00.000/2012-01-03T00:00:00.000" ]\n}\n')),(0,l.kt)("p",null,"There are 7 main parts to a timeseries query:"),(0,l.kt)("table",null,(0,l.kt)("thead",{parentName:"table"},(0,l.kt)("tr",{parentName:"thead"},(0,l.kt)("th",{parentName:"tr",align:null},"property"),(0,l.kt)("th",{parentName:"tr",align:null},"description"),(0,l.kt)("th",{parentName:"tr",align:null},"required?"))),(0,l.kt)("tbody",{parentName:"table"},(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},"queryType"),(0,l.kt)("td",{parentName:"tr",align:null},'This String should always be "timeseries"; this is the first thing Apache Druid looks at to figure out how to interpret the query'),(0,l.kt)("td",{parentName:"tr",align:null},"yes")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},"dataSource"),(0,l.kt)("td",{parentName:"tr",align:null},"A String or Object defining the data source to query, very similar to a table in a relational database. See ",(0,l.kt)("a",{parentName:"td",href:"/docs/28.0.0/querying/datasource"},"DataSource")," for more information."),(0,l.kt)("td",{parentName:"tr",align:null},"yes")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},"descending"),(0,l.kt)("td",{parentName:"tr",align:null},"Whether to make descending ordered result. Default is ",(0,l.kt)("inlineCode",{parentName:"td"},"false"),"(ascending)."),(0,l.kt)("td",{parentName:"tr",align:null},"no")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},"intervals"),(0,l.kt)("td",{parentName:"tr",align:null},"A JSON Object representing ISO-8601 Intervals. This defines the time ranges to run the query over."),(0,l.kt)("td",{parentName:"tr",align:null},"yes")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},"granularity"),(0,l.kt)("td",{parentName:"tr",align:null},"Defines the granularity to bucket query results. See ",(0,l.kt)("a",{parentName:"td",href:"/docs/28.0.0/querying/granularities"},"Granularities")),(0,l.kt)("td",{parentName:"tr",align:null},"yes")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},"filter"),(0,l.kt)("td",{parentName:"tr",align:null},"See ",(0,l.kt)("a",{parentName:"td",href:"/docs/28.0.0/querying/filters"},"Filters")),(0,l.kt)("td",{parentName:"tr",align:null},"no")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},"virtualColumns"),(0,l.kt)("td",{parentName:"tr",align:null},"A JSON list of ",(0,l.kt)("a",{parentName:"td",href:"/docs/28.0.0/querying/virtual-columns"},"virtual columns"),". You can reference the virtual columns in ",(0,l.kt)("inlineCode",{parentName:"td"},"aggregations")," or ",(0,l.kt)("inlineCode",{parentName:"td"},"postAggregations"),"."),(0,l.kt)("td",{parentName:"tr",align:null},"no (default none)")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},"aggregations"),(0,l.kt)("td",{parentName:"tr",align:null},"See ",(0,l.kt)("a",{parentName:"td",href:"/docs/28.0.0/querying/aggregations"},"Aggregations")),(0,l.kt)("td",{parentName:"tr",align:null},"no")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},"postAggregations"),(0,l.kt)("td",{parentName:"tr",align:null},"See ",(0,l.kt)("a",{parentName:"td",href:"/docs/28.0.0/querying/post-aggregations"},"Post Aggregations")),(0,l.kt)("td",{parentName:"tr",align:null},"no")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},"limit"),(0,l.kt)("td",{parentName:"tr",align:null},"An integer that limits the number of results. The default is unlimited."),(0,l.kt)("td",{parentName:"tr",align:null},"no")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},"context"),(0,l.kt)("td",{parentName:"tr",align:null},"Can be used to modify query behavior, including ",(0,l.kt)("a",{parentName:"td",href:"#grand-totals"},"grand totals")," and ",(0,l.kt)("a",{parentName:"td",href:"#zero-filling"},"zero-filling"),". See also ",(0,l.kt)("a",{parentName:"td",href:"/docs/28.0.0/querying/query-context"},"Context")," for parameters that apply to all query types."),(0,l.kt)("td",{parentName:"tr",align:null},"no")))),(0,l.kt)("p",null,'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:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-json"},'[\n {\n "timestamp": "2012-01-01T00:00:00.000Z",\n "result": { "sample_name1": <some_value>, "sample_name2": <some_value>, "sample_divide": <some_value> }\n },\n {\n "timestamp": "2012-01-02T00:00:00.000Z",\n "result": { "sample_name1": <some_value>, "sample_name2": <some_value>, "sample_divide": <some_value> }\n }\n]\n')),(0,l.kt)("h2",{id:"grand-totals"},"Grand totals"),(0,l.kt)("p",null,'Druid can include an extra "grand totals" row as the last row of a timeseries result set. To enable this, add\n',(0,l.kt)("inlineCode",{parentName:"p"},'"grandTotal" : true')," to your query context. For example:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-json"},'{\n "queryType": "timeseries",\n "dataSource": "sample_datasource",\n "intervals": [ "2012-01-01T00:00:00.000/2012-01-03T00:00:00.000" ],\n "granularity": "day",\n "aggregations": [\n { "type": "longSum", "name": "sample_name1", "fieldName": "sample_fieldName1" },\n { "type": "doubleSum", "name": "sample_name2", "fieldName": "sample_fieldName2" }\n ],\n "context": {\n "grandTotal": true\n }\n}\n')),(0,l.kt)("p",null,'The grand totals row will appear as the last row in the result array, and will have no timestamp. It will be the last\nrow even if the query is run in "descending" mode. Post-aggregations in the grand totals row will be computed based\nupon the grand total aggregations.'),(0,l.kt)("h2",{id:"zero-filling"},"Zero-filling"),(0,l.kt)("p",null,'Timeseries queries normally fill empty interior time buckets with zeroes. For example, if you issue a "day" granularity\ntimeseries query for the interval 2012-01-01/2012-01-04, and no data exists for 2012-01-02, you will receive:'),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-json"},'[\n {\n "timestamp": "2012-01-01T00:00:00.000Z",\n "result": { "sample_name1": <some_value> }\n },\n {\n "timestamp": "2012-01-02T00:00:00.000Z",\n "result": { "sample_name1": 0 }\n },\n {\n "timestamp": "2012-01-03T00:00:00.000Z",\n "result": { "sample_name1": <some_value> }\n }\n]\n')),(0,l.kt)("p",null,"Time buckets that lie completely outside the data interval are not zero-filled."),(0,l.kt)("p",null,'You can disable all zero-filling with the context flag "skipEmptyBuckets". In this mode, the data point for 2012-01-02\nwould be omitted from the results.'),(0,l.kt)("p",null,"A query with this context flag set would look like:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-json"},'{\n "queryType": "timeseries",\n "dataSource": "sample_datasource",\n "granularity": "day",\n "aggregations": [\n { "type": "longSum", "name": "sample_name1", "fieldName": "sample_fieldName1" }\n ],\n "intervals": [ "2012-01-01T00:00:00.000/2012-01-04T00:00:00.000" ],\n "context" : {\n "skipEmptyBuckets": "true"\n }\n}\n')))}c.isMDXComponent=!0}}]);