blob: d61bd936b4dda2d7601091e06c32c65b0a0abd37 [file] [log] [blame]
"use strict";(self.webpackChunk=self.webpackChunk||[]).push([[7118],{3905:(e,t,a)=>{a.d(t,{Zo:()=>u,kt:()=>y});var r=a(67294);function n(e,t,a){return t in e?Object.defineProperty(e,t,{value:a,enumerable:!0,configurable:!0,writable:!0}):e[t]=a,e}function l(e,t){var a=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),a.push.apply(a,r)}return a}function i(e){for(var t=1;t<arguments.length;t++){var a=null!=arguments[t]?arguments[t]:{};t%2?l(Object(a),!0).forEach((function(t){n(e,t,a[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(a)):l(Object(a)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(a,t))}))}return e}function o(e,t){if(null==e)return{};var a,r,n=function(e,t){if(null==e)return{};var a,r,n={},l=Object.keys(e);for(r=0;r<l.length;r++)a=l[r],t.indexOf(a)>=0||(n[a]=e[a]);return n}(e,t);if(Object.getOwnPropertySymbols){var l=Object.getOwnPropertySymbols(e);for(r=0;r<l.length;r++)a=l[r],t.indexOf(a)>=0||Object.prototype.propertyIsEnumerable.call(e,a)&&(n[a]=e[a])}return n}var s=r.createContext({}),d=function(e){var t=r.useContext(s),a=t;return e&&(a="function"==typeof e?e(t):i(i({},t),e)),a},u=function(e){var t=d(e.components);return r.createElement(s.Provider,{value:t},e.children)},p="mdxType",m={inlineCode:"code",wrapper:function(e){var t=e.children;return r.createElement(r.Fragment,{},t)}},c=r.forwardRef((function(e,t){var a=e.components,n=e.mdxType,l=e.originalType,s=e.parentName,u=o(e,["components","mdxType","originalType","parentName"]),p=d(a),c=n,y=p["".concat(s,".").concat(c)]||p[c]||m[c]||l;return a?r.createElement(y,i(i({ref:t},u),{},{components:a})):r.createElement(y,i({ref:t},u))}));function y(e,t){var a=arguments,n=t&&t.mdxType;if("string"==typeof e||n){var l=a.length,i=new Array(l);i[0]=c;var o={};for(var s in t)hasOwnProperty.call(t,s)&&(o[s]=t[s]);o.originalType=e,o[p]="string"==typeof e?e:n,i[1]=o;for(var d=2;d<l;d++)i[d]=a[d];return r.createElement.apply(null,i)}return r.createElement.apply(null,a)}c.displayName="MDXCreateElement"},78625:(e,t,a)=>{a.r(t),a.d(t,{assets:()=>u,contentTitle:()=>s,default:()=>y,frontMatter:()=>o,metadata:()=>d,toc:()=>p});var r=a(87462),n=a(63366),l=(a(67294),a(3905)),i=["components"],o={id:"sql-api",title:"Druid SQL API",sidebar_label:"Druid SQL API"},s=void 0,d={unversionedId:"querying/sql-api",id:"querying/sql-api",title:"Druid SQL API",description:"\x3c!--",source:"@site/docs/latest/querying/sql-api.md",sourceDirName:"querying",slug:"/querying/sql-api",permalink:"/docs/latest/querying/sql-api",draft:!1,tags:[],version:"current",frontMatter:{id:"sql-api",title:"Druid SQL API",sidebar_label:"Druid SQL API"}},u={},p=[{value:"Submit a query",id:"submit-a-query",level:2},{value:"Request body",id:"request-body",level:3},{value:"Responses",id:"responses",level:3},{value:"Result formats",id:"result-formats",level:4},{value:"Errors",id:"errors",level:4},{value:"Cancel a query",id:"cancel-a-query",level:2}],m={toc:p},c="wrapper";function y(e){var t=e.components,a=(0,n.Z)(e,i);return(0,l.kt)(c,(0,r.Z)({},m,a,{components:t,mdxType:"MDXLayout"}),(0,l.kt)("blockquote",null,(0,l.kt)("p",{parentName:"blockquote"},"Apache Druid supports two query languages: Druid SQL and ",(0,l.kt)("a",{parentName:"p",href:"/docs/latest/querying/"},"native queries"),".\nThis document describes the SQL language.")),(0,l.kt)("p",null,"You can submit and cancel ",(0,l.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql"},"Druid SQL")," queries using the Druid SQL API.\nThe Druid SQL API is available at ",(0,l.kt)("inlineCode",{parentName:"p"},"https://ROUTER:8888/druid/v2/sql"),", where ",(0,l.kt)("inlineCode",{parentName:"p"},"ROUTER")," is the IP address of the Druid Router."),(0,l.kt)("h2",{id:"submit-a-query"},"Submit a query"),(0,l.kt)("p",null,"To use the SQL API to make Druid SQL queries, send your query to the Router using the POST method:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre"},"POST https://ROUTER:8888/druid/v2/sql/\n")),(0,l.kt)("p",null,'Submit your query as the value of a "query" field in the JSON object within the request payload. For example:'),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-json"},'{"query" : "SELECT COUNT(*) FROM data_source WHERE foo = \'bar\'"}\n')),(0,l.kt)("h3",{id:"request-body"},"Request body"),(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},"Default"))),(0,l.kt)("tbody",{parentName:"table"},(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"query")),(0,l.kt)("td",{parentName:"tr",align:null},"SQL query string."),(0,l.kt)("td",{parentName:"tr",align:null},"none (required)")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"resultFormat")),(0,l.kt)("td",{parentName:"tr",align:null},"Format of query results. See ",(0,l.kt)("a",{parentName:"td",href:"#responses"},"Responses")," for details."),(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},'"object"'))),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"header")),(0,l.kt)("td",{parentName:"tr",align:null},"Whether or not to include a header row for the query result. See ",(0,l.kt)("a",{parentName:"td",href:"#responses"},"Responses")," for details."),(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"false"))),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"typesHeader")),(0,l.kt)("td",{parentName:"tr",align:null},"Whether or not to include type information in the header. Can only be set when ",(0,l.kt)("inlineCode",{parentName:"td"},"header")," is also ",(0,l.kt)("inlineCode",{parentName:"td"},"true"),". See ",(0,l.kt)("a",{parentName:"td",href:"#responses"},"Responses")," for details."),(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"false"))),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"sqlTypesHeader")),(0,l.kt)("td",{parentName:"tr",align:null},"Whether or not to include SQL type information in the header. Can only be set when ",(0,l.kt)("inlineCode",{parentName:"td"},"header")," is also ",(0,l.kt)("inlineCode",{parentName:"td"},"true"),". See ",(0,l.kt)("a",{parentName:"td",href:"#responses"},"Responses")," for details."),(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"false"))),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"context")),(0,l.kt)("td",{parentName:"tr",align:null},"JSON object containing ",(0,l.kt)("a",{parentName:"td",href:"/docs/latest/querying/sql-query-context"},"SQL query context parameters"),"."),(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"{}")," (empty)")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"parameters")),(0,l.kt)("td",{parentName:"tr",align:null},"List of query parameters for parameterized queries. Each parameter in the list should be a JSON object like ",(0,l.kt)("inlineCode",{parentName:"td"},'{"type": "VARCHAR", "value": "foo"}'),". The type should be a SQL type; see ",(0,l.kt)("a",{parentName:"td",href:"/docs/latest/querying/sql-data-types"},"Data types")," for a list of supported SQL types."),(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"[]")," (empty)")))),(0,l.kt)("p",null,"You can use ",(0,l.kt)("em",{parentName:"p"},"curl")," to send SQL queries from the command-line:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-bash"},'$ cat query.json\n{"query":"SELECT COUNT(*) AS TheCount FROM data_source"}\n\n$ curl -XPOST -H\'Content-Type: application/json\' http://ROUTER:8888/druid/v2/sql/ -d @query.json\n[{"TheCount":24433}]\n')),(0,l.kt)("p",null,"There are a variety of ",(0,l.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-query-context"},"SQL query context parameters"),' you can provide by adding a "context" map,\nlike:'),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-json"},'{\n "query" : "SELECT COUNT(*) FROM data_source WHERE foo = \'bar\' AND __time > TIMESTAMP \'2000-01-01 00:00:00\'",\n "context" : {\n "sqlTimeZone" : "America/Los_Angeles"\n }\n}\n')),(0,l.kt)("p",null,"Parameterized SQL queries are also supported:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-json"},'{\n "query" : "SELECT COUNT(*) FROM data_source WHERE foo = ? AND __time > ?",\n "parameters": [\n { "type": "VARCHAR", "value": "bar"},\n { "type": "TIMESTAMP", "value": "2000-01-01 00:00:00" }\n ]\n}\n')),(0,l.kt)("p",null,"Metadata is available over HTTP POST by querying ",(0,l.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-metadata-tables"},"metadata tables"),"."),(0,l.kt)("h3",{id:"responses"},"Responses"),(0,l.kt)("h4",{id:"result-formats"},"Result formats"),(0,l.kt)("p",null,'Druid SQL\'s HTTP POST API supports a variety of result formats. You can specify these by adding a "resultFormat"\nparameter, like:'),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-json"},'{\n "query" : "SELECT COUNT(*) FROM data_source WHERE foo = \'bar\' AND __time > TIMESTAMP \'2000-01-01 00:00:00\'",\n "resultFormat" : "array"\n}\n')),(0,l.kt)("p",null,"To request a header with information about column names, set ",(0,l.kt)("inlineCode",{parentName:"p"},"header")," to true in your request.\nWhen you set ",(0,l.kt)("inlineCode",{parentName:"p"},"header")," to true, you can optionally include ",(0,l.kt)("inlineCode",{parentName:"p"},"typesHeader")," and ",(0,l.kt)("inlineCode",{parentName:"p"},"sqlTypesHeader")," as well, which gives\nyou information about ",(0,l.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-data-types"},"Druid runtime and SQL types")," respectively. You can request all these headers\nwith a request like:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-json"},'{\n "query" : "SELECT COUNT(*) FROM data_source WHERE foo = \'bar\' AND __time > TIMESTAMP \'2000-01-01 00:00:00\'",\n "resultFormat" : "array",\n "header" : true,\n "typesHeader" : true,\n "sqlTypesHeader" : true\n}\n')),(0,l.kt)("p",null,"The following table shows supported result formats:"),(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},"Format"),(0,l.kt)("th",{parentName:"tr",align:null},"Description"),(0,l.kt)("th",{parentName:"tr",align:null},"Header description"),(0,l.kt)("th",{parentName:"tr",align:null},"Content-Type"))),(0,l.kt)("tbody",{parentName:"table"},(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"object")),(0,l.kt)("td",{parentName:"tr",align:null},"The default, a JSON array of JSON objects. Each object's field names match the columns returned by the SQL query, and are provided in the same order as the SQL query."),(0,l.kt)("td",{parentName:"tr",align:null},"If ",(0,l.kt)("inlineCode",{parentName:"td"},"header")," is true, the first row is an object where the fields are column names. Each field's value is either null (if ",(0,l.kt)("inlineCode",{parentName:"td"},"typesHeader")," and ",(0,l.kt)("inlineCode",{parentName:"td"},"sqlTypesHeader")," are false) or an object that contains the Druid type as ",(0,l.kt)("inlineCode",{parentName:"td"},"type")," (if ",(0,l.kt)("inlineCode",{parentName:"td"},"typesHeader")," is true) and the SQL type as ",(0,l.kt)("inlineCode",{parentName:"td"},"sqlType")," (if ",(0,l.kt)("inlineCode",{parentName:"td"},"sqlTypesHeader")," is true)."),(0,l.kt)("td",{parentName:"tr",align:null},"application/json")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"array")),(0,l.kt)("td",{parentName:"tr",align:null},"JSON array of JSON arrays. Each inner array has elements matching the columns returned by the SQL query, in order."),(0,l.kt)("td",{parentName:"tr",align:null},"If ",(0,l.kt)("inlineCode",{parentName:"td"},"header")," is true, the first row is an array of column names. If ",(0,l.kt)("inlineCode",{parentName:"td"},"typesHeader")," is true, the next row is an array of Druid types. If ",(0,l.kt)("inlineCode",{parentName:"td"},"sqlTypesHeader")," is true, the next row is an array of SQL types."),(0,l.kt)("td",{parentName:"tr",align:null},"application/json")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"objectLines")),(0,l.kt)("td",{parentName:"tr",align:null},"Like ",(0,l.kt)("inlineCode",{parentName:"td"},"object"),", but the JSON objects are separated by newlines instead of being wrapped in a JSON array. This can make it easier to parse the entire response set as a stream, if you do not have ready access to a streaming JSON parser. To make it possible to detect a truncated response, this format includes a trailer of one blank line."),(0,l.kt)("td",{parentName:"tr",align:null},"Same as ",(0,l.kt)("inlineCode",{parentName:"td"},"object"),"."),(0,l.kt)("td",{parentName:"tr",align:null},"text/plain")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"arrayLines")),(0,l.kt)("td",{parentName:"tr",align:null},"Like ",(0,l.kt)("inlineCode",{parentName:"td"},"array"),", but the JSON arrays are separated by newlines instead of being wrapped in a JSON array. This can make it easier to parse the entire response set as a stream, if you do not have ready access to a streaming JSON parser. To make it possible to detect a truncated response, this format includes a trailer of one blank line."),(0,l.kt)("td",{parentName:"tr",align:null},"Same as ",(0,l.kt)("inlineCode",{parentName:"td"},"array"),", except the rows are separated by newlines."),(0,l.kt)("td",{parentName:"tr",align:null},"text/plain")),(0,l.kt)("tr",{parentName:"tbody"},(0,l.kt)("td",{parentName:"tr",align:null},(0,l.kt)("inlineCode",{parentName:"td"},"csv")),(0,l.kt)("td",{parentName:"tr",align:null},"Comma-separated values, with one row per line. Individual field values may be escaped by being surrounded in double quotes. If double quotes appear in a field value, they will be escaped by replacing them with double-double-quotes like ",(0,l.kt)("inlineCode",{parentName:"td"},'""this""'),". To make it possible to detect a truncated response, this format includes a trailer of one blank line."),(0,l.kt)("td",{parentName:"tr",align:null},"Same as ",(0,l.kt)("inlineCode",{parentName:"td"},"array"),", except the lists are in CSV format."),(0,l.kt)("td",{parentName:"tr",align:null},"text/csv")))),(0,l.kt)("p",null,"If ",(0,l.kt)("inlineCode",{parentName:"p"},"typesHeader")," is set to true, ",(0,l.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-data-types"},"Druid type")," information is included in the response. Complex types,\nlike sketches, will be reported as ",(0,l.kt)("inlineCode",{parentName:"p"},"COMPLEX<typeName>")," if a particular complex type name is known for that field,\nor as ",(0,l.kt)("inlineCode",{parentName:"p"},"COMPLEX")," if the particular type name is unknown or mixed. If ",(0,l.kt)("inlineCode",{parentName:"p"},"sqlTypesHeader")," is set to true,\n",(0,l.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-data-types"},"SQL type")," information is included in the response. It is possible to set both ",(0,l.kt)("inlineCode",{parentName:"p"},"typesHeader")," and\n",(0,l.kt)("inlineCode",{parentName:"p"},"sqlTypesHeader")," at once. Both parameters require that ",(0,l.kt)("inlineCode",{parentName:"p"},"header")," is also set."),(0,l.kt)("p",null,"To aid in building clients that are compatible with older Druid versions, Druid returns the HTTP header\n",(0,l.kt)("inlineCode",{parentName:"p"},"X-Druid-SQL-Header-Included: yes")," if ",(0,l.kt)("inlineCode",{parentName:"p"},"header")," was set to true and if the version of Druid the client is connected to\nunderstands the ",(0,l.kt)("inlineCode",{parentName:"p"},"typesHeader")," and ",(0,l.kt)("inlineCode",{parentName:"p"},"sqlTypesHeader")," parameters. This HTTP response header is present irrespective of\nwhether ",(0,l.kt)("inlineCode",{parentName:"p"},"typesHeader")," or ",(0,l.kt)("inlineCode",{parentName:"p"},"sqlTypesHeader")," are set or not."),(0,l.kt)("p",null,"Druid returns the SQL query identifier in the ",(0,l.kt)("inlineCode",{parentName:"p"},"X-Druid-SQL-Query-Id")," HTTP header.\nThis query id will be assigned the value of ",(0,l.kt)("inlineCode",{parentName:"p"},"sqlQueryId")," from the ",(0,l.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-query-context"},"query context parameters"),"\nif specified, else Druid will generate a SQL query id for you."),(0,l.kt)("h4",{id:"errors"},"Errors"),(0,l.kt)("p",null,"Errors that occur before the response body is sent will be reported in JSON, with an HTTP 500 status code, in the\nsame format as ",(0,l.kt)("a",{parentName:"p",href:"/docs/latest/querying/#query-errors"},"native Druid query errors"),". If an error occurs while the response body is\nbeing sent, at that point it is too late to change the HTTP status code or report a JSON error, so the response will\nsimply end midstream and an error will be logged by the Druid server that was handling your request."),(0,l.kt)("p",null,"As a caller, it is important that you properly handle response truncation. This is easy for the ",(0,l.kt)("inlineCode",{parentName:"p"},"object")," and ",(0,l.kt)("inlineCode",{parentName:"p"},"array"),"\nformats, since truncated responses will be invalid JSON. For the line-oriented formats, you should check the\ntrailer they all include: one blank line at the end of the result set. If you detect a truncated response, either\nthrough a JSON parsing error or through a missing trailing newline, you should assume the response was not fully\ndelivered due to an error."),(0,l.kt)("h2",{id:"cancel-a-query"},"Cancel a query"),(0,l.kt)("p",null,"You can use the HTTP DELETE method to cancel a SQL query on either the Router or the Broker. When you cancel a query, Druid handles the cancellation in a best-effort manner. It marks the query canceled immediately and aborts the query execution as soon as possible. However, your query may run for a short time after your cancellation request."),(0,l.kt)("p",null,"Druid SQL's HTTP DELETE method uses the following syntax:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre"},"DELETE https://ROUTER:8888/druid/v2/sql/{sqlQueryId}\n")),(0,l.kt)("p",null,"The DELETE method requires the ",(0,l.kt)("inlineCode",{parentName:"p"},"sqlQueryId")," path parameter. To predict the query id you must set it in the query context. Druid does not enforce unique ",(0,l.kt)("inlineCode",{parentName:"p"},"sqlQueryId")," in the query context. If you issue a cancel request for a ",(0,l.kt)("inlineCode",{parentName:"p"},"sqlQueryId")," active in more than one query context, Druid cancels all requests that use the query id."),(0,l.kt)("p",null,"For example if you issue the following query:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-bash"},'curl --request POST \'https://ROUTER:8888/druid/v2/sql\' \\\n--header \'Content-Type: application/json\' \\\n--data-raw \'{"query" : "SELECT sleep(CASE WHEN sum_added > 0 THEN 1 ELSE 0 END) FROM wikiticker WHERE sum_added > 0 LIMIT 15",\n"context" : {"sqlQueryId" : "myQuery01"}}\'\n')),(0,l.kt)("p",null,"You can cancel the query using the query id ",(0,l.kt)("inlineCode",{parentName:"p"},"myQuery01")," as follows:"),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-bash"},"curl --request DELETE 'https://ROUTER:8888/druid/v2/sql/myQuery01' \\\n")),(0,l.kt)("p",null,"Cancellation requests require READ permission on all resources used in the sql query. "),(0,l.kt)("p",null,"Druid returns an HTTP 202 response for successful deletion requests."),(0,l.kt)("p",null,"Druid returns an HTTP 404 response in the following cases:"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("inlineCode",{parentName:"li"},"sqlQueryId")," is incorrect."),(0,l.kt)("li",{parentName:"ul"},"The query completes before your cancellation request is processed.")),(0,l.kt)("p",null,"Druid returns an HTTP 403 response for authorization failure."))}y.isMDXComponent=!0}}]);