blob: c3c32698d4b578d9ef9b855e62b70b242a0f59e4 [file] [log] [blame]
"use strict";(self.webpackChunk=self.webpackChunk||[]).push([[5773],{3905:(e,t,n)=>{n.d(t,{Zo:()=>u,kt:()=>y});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 s(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?s(Object(n),!0).forEach((function(t){r(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):s(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function o(e,t){if(null==e)return{};var n,a,r=function(e,t){if(null==e)return{};var n,a,r={},s=Object.keys(e);for(a=0;a<s.length;a++)n=s[a],t.indexOf(n)>=0||(r[n]=e[n]);return r}(e,t);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(e);for(a=0;a<s.length;a++)n=s[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(r[n]=e[n])}return r}var l=a.createContext({}),p=function(e){var t=a.useContext(l),n=t;return e&&(n="function"==typeof e?e(t):i(i({},t),e)),n},u=function(e){var t=p(e.components);return a.createElement(l.Provider,{value:t},e.children)},d="mdxType",m={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},c=a.forwardRef((function(e,t){var n=e.components,r=e.mdxType,s=e.originalType,l=e.parentName,u=o(e,["components","mdxType","originalType","parentName"]),d=p(n),c=r,y=d["".concat(l,".").concat(c)]||d[c]||m[c]||s;return n?a.createElement(y,i(i({ref:t},u),{},{components:n})):a.createElement(y,i({ref:t},u))}));function y(e,t){var n=arguments,r=t&&t.mdxType;if("string"==typeof e||r){var s=n.length,i=new Array(s);i[0]=c;var o={};for(var l in t)hasOwnProperty.call(t,l)&&(o[l]=t[l]);o.originalType=e,o[d]="string"==typeof e?e:r,i[1]=o;for(var p=2;p<s;p++)i[p]=n[p];return a.createElement.apply(null,i)}return a.createElement.apply(null,n)}c.displayName="MDXCreateElement"},85162:(e,t,n)=>{n.d(t,{Z:()=>i});var a=n(67294),r=n(86010);const s={tabItem:"tabItem_Ymn6"};function i(e){var t=e.children,n=e.hidden,i=e.className;return a.createElement("div",{role:"tabpanel",className:(0,r.Z)(s.tabItem,i),hidden:n},t)}},74866:(e,t,n)=>{n.d(t,{Z:()=>E});var a=n(87462),r=n(67294),s=n(86010),i=n(12466),o=n(16550),l=n(91980),p=n(67392),u=n(50012);function d(e){return function(e){var t,n;return null!=(t=null==(n=r.Children.map(e,(function(e){if(!e||(0,r.isValidElement)(e)&&(t=e.props)&&"object"==typeof t&&"value"in t)return e;var t;throw new Error("Docusaurus error: Bad <Tabs> child <"+("string"==typeof e.type?e.type:e.type.name)+'>: all children of the <Tabs> component should be <TabItem>, and every <TabItem> should have a unique "value" prop.')})))?void 0:n.filter(Boolean))?t:[]}(e).map((function(e){var t=e.props;return{value:t.value,label:t.label,attributes:t.attributes,default:t.default}}))}function m(e){var t=e.values,n=e.children;return(0,r.useMemo)((function(){var e=null!=t?t:d(n);return function(e){var t=(0,p.l)(e,(function(e,t){return e.value===t.value}));if(t.length>0)throw new Error('Docusaurus error: Duplicate values "'+t.map((function(e){return e.value})).join(", ")+'" found in <Tabs>. Every value needs to be unique.')}(e),e}),[t,n])}function c(e){var t=e.value;return e.tabValues.some((function(e){return e.value===t}))}function y(e){var t=e.queryString,n=void 0!==t&&t,a=e.groupId,s=(0,o.k6)(),i=function(e){var t=e.queryString,n=void 0!==t&&t,a=e.groupId;if("string"==typeof n)return n;if(!1===n)return null;if(!0===n&&!a)throw new Error('Docusaurus error: The <Tabs> component groupId prop is required if queryString=true, because this value is used as the search param name. You can also provide an explicit value such as queryString="my-search-param".');return null!=a?a:null}({queryString:n,groupId:a});return[(0,l._X)(i),(0,r.useCallback)((function(e){if(i){var t=new URLSearchParams(s.location.search);t.set(i,e),s.replace(Object.assign({},s.location,{search:t.toString()}))}}),[i,s])]}function g(e){var t,n,a,s,i=e.defaultValue,o=e.queryString,l=void 0!==o&&o,p=e.groupId,d=m(e),g=(0,r.useState)((function(){return function(e){var t,n=e.defaultValue,a=e.tabValues;if(0===a.length)throw new Error("Docusaurus error: the <Tabs> component requires at least one <TabItem> children component");if(n){if(!c({value:n,tabValues:a}))throw new Error('Docusaurus error: The <Tabs> has a defaultValue "'+n+'" but none of its children has the corresponding value. Available values are: '+a.map((function(e){return e.value})).join(", ")+". If you intend to show no default tab, use defaultValue={null} instead.");return n}var r=null!=(t=a.find((function(e){return e.default})))?t:a[0];if(!r)throw new Error("Unexpected error: 0 tabValues");return r.value}({defaultValue:i,tabValues:d})})),N=g[0],h=g[1],f=y({queryString:l,groupId:p}),k=f[0],S=f[1],E=(t=function(e){return e?"docusaurus.tab."+e:null}({groupId:p}.groupId),n=(0,u.Nk)(t),a=n[0],s=n[1],[a,(0,r.useCallback)((function(e){t&&s.set(e)}),[t,s])]),b=E[0],C=E[1],v=function(){var e=null!=k?k:b;return c({value:e,tabValues:d})?e:null}();return(0,r.useLayoutEffect)((function(){v&&h(v)}),[v]),{selectedValue:N,selectValue:(0,r.useCallback)((function(e){if(!c({value:e,tabValues:d}))throw new Error("Can't select invalid tab value="+e);h(e),S(e),C(e)}),[S,C,d]),tabValues:d}}var N=n(72389);const h={tabList:"tabList__CuJ",tabItem:"tabItem_LNqP"};function f(e){var t=e.className,n=e.block,o=e.selectedValue,l=e.selectValue,p=e.tabValues,u=[],d=(0,i.o5)().blockElementScrollPositionUntilNextRender,m=function(e){var t=e.currentTarget,n=u.indexOf(t),a=p[n].value;a!==o&&(d(t),l(a))},c=function(e){var t,n=null;switch(e.key){case"Enter":m(e);break;case"ArrowRight":var a,r=u.indexOf(e.currentTarget)+1;n=null!=(a=u[r])?a:u[0];break;case"ArrowLeft":var s,i=u.indexOf(e.currentTarget)-1;n=null!=(s=u[i])?s:u[u.length-1]}null==(t=n)||t.focus()};return r.createElement("ul",{role:"tablist","aria-orientation":"horizontal",className:(0,s.Z)("tabs",{"tabs--block":n},t)},p.map((function(e){var t=e.value,n=e.label,i=e.attributes;return r.createElement("li",(0,a.Z)({role:"tab",tabIndex:o===t?0:-1,"aria-selected":o===t,key:t,ref:function(e){return u.push(e)},onKeyDown:c,onClick:m},i,{className:(0,s.Z)("tabs__item",h.tabItem,null==i?void 0:i.className,{"tabs__item--active":o===t})}),null!=n?n:t)})))}function k(e){var t=e.lazy,n=e.children,a=e.selectedValue,s=(Array.isArray(n)?n:[n]).filter(Boolean);if(t){var i=s.find((function(e){return e.props.value===a}));return i?(0,r.cloneElement)(i,{className:"margin-top--md"}):null}return r.createElement("div",{className:"margin-top--md"},s.map((function(e,t){return(0,r.cloneElement)(e,{key:t,hidden:e.props.value!==a})})))}function S(e){var t=g(e);return r.createElement("div",{className:(0,s.Z)("tabs-container",h.tabList)},r.createElement(f,(0,a.Z)({},e,t)),r.createElement(k,(0,a.Z)({},e,t)))}function E(e){var t=(0,N.Z)();return r.createElement(S,(0,a.Z)({key:String(t)},e))}},32747:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>m,contentTitle:()=>u,default:()=>N,frontMatter:()=>p,metadata:()=>d,toc:()=>c});var a=n(87462),r=n(63366),s=(n(67294),n(3905)),i=n(74866),o=n(85162),l=["components"],p={id:"nested-columns",title:"Nested columns",sidebar_label:"Nested columns"},u=void 0,d={unversionedId:"querying/nested-columns",id:"querying/nested-columns",title:"Nested columns",description:"\x3c!--",source:"@site/docs/latest/querying/nested-columns.md",sourceDirName:"querying",slug:"/querying/nested-columns",permalink:"/docs/latest/querying/nested-columns",draft:!1,tags:[],version:"current",frontMatter:{id:"nested-columns",title:"Nested columns",sidebar_label:"Nested columns"},sidebar:"docs",previous:{title:"Multi-value dimensions",permalink:"/docs/latest/querying/multi-value-dimensions"},next:{title:"Multitenancy",permalink:"/docs/latest/querying/multitenancy"}},m={},c=[{value:"Example nested data",id:"example-nested-data",level:2},{value:"Native batch ingestion",id:"native-batch-ingestion",level:2},{value:"Transform data during batch ingestion",id:"transform-data-during-batch-ingestion",level:3},{value:"SQL-based ingestion",id:"sql-based-ingestion",level:2},{value:"Streaming ingestion",id:"streaming-ingestion",level:2},{value:"Transform data during SQL-based ingestion",id:"transform-data-during-sql-based-ingestion",level:3},{value:"Ingest a JSON string as COMPLEX&lt;json&gt;",id:"ingest-a-json-string-as-complexjson",level:2},{value:"Querying nested columns",id:"querying-nested-columns",level:2},{value:"Displaying data types",id:"displaying-data-types",level:3},{value:"Example query: Display data types",id:"example-query-display-data-types",level:4},{value:"Retrieving JSON data",id:"retrieving-json-data",level:3},{value:"Example query: Retrieve JSON data",id:"example-query-retrieve-json-data",level:4},{value:"Extracting nested data elements",id:"extracting-nested-data-elements",level:3},{value:"Example query: Extract nested data elements",id:"example-query-extract-nested-data-elements",level:4},{value:"Extracting nested data elements as a suggested type",id:"extracting-nested-data-elements-as-a-suggested-type",level:3},{value:"Example query: Extract nested data elements as suggested types",id:"example-query-extract-nested-data-elements-as-suggested-types",level:4},{value:"Grouping, aggregating, and filtering",id:"grouping-aggregating-and-filtering",level:3},{value:"Example query: Grouping and filtering",id:"example-query-grouping-and-filtering",level:4},{value:"Transforming JSON object data",id:"transforming-json-object-data",level:3},{value:"Example query: Return results in a JSON object",id:"example-query-return-results-in-a-json-object",level:4},{value:"Example query: Combine multiple JSON inputs into a single JSON object value",id:"example-query-combine-multiple-json-inputs-into-a-single-json-object-value",level:4},{value:"Using other transform functions",id:"using-other-transform-functions",level:3},{value:"Example query: Parse and deserialize data",id:"example-query-parse-and-deserialize-data",level:4},{value:"Using helper operators",id:"using-helper-operators",level:3},{value:"Example query: Examine JSON object schema",id:"example-query-examine-json-object-schema",level:4},{value:"Known issues",id:"known-issues",level:2},{value:"Further reading",id:"further-reading",level:2}],y={toc:c},g="wrapper";function N(e){var t=e.components,p=(0,r.Z)(e,l);return(0,s.kt)(g,(0,a.Z)({},y,p,{components:t,mdxType:"MDXLayout"}),(0,s.kt)("p",null,"Apache Druid supports directly storing nested data structures in ",(0,s.kt)("inlineCode",{parentName:"p"},"COMPLEX<json>")," columns. ",(0,s.kt)("inlineCode",{parentName:"p"},"COMPLEX<json>")," columns store a copy of the structured data in JSON format and specialized internal columns and indexes for nested literal values","\u2014","STRING, LONG, and DOUBLE types. An optimized ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/querying/virtual-columns#nested-field-virtual-column"},"virtual column")," allows Druid to read and filter these values at speeds consistent with standard Druid LONG, DOUBLE, and STRING columns."),(0,s.kt)("p",null,"Druid ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-json-functions"},"SQL JSON functions")," allow you to extract, transform, and create ",(0,s.kt)("inlineCode",{parentName:"p"},"COMPLEX<json>")," values in SQL queries, using the specialized virtual columns where appropriate. You can use the ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/querying/math-expr#json-functions"},"JSON nested columns functions")," in ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/querying/"},"native queries")," using ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/querying/virtual-columns#expression-virtual-column"},"expression virtual columns"),", and in native ingestion with a ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/ingestion/ingestion-spec#transformspec"},(0,s.kt)("inlineCode",{parentName:"a"},"transformSpec")),"."),(0,s.kt)("p",null,"You can use the JSON functions in INSERT and REPLACE statements in SQL-based ingestion, or in a ",(0,s.kt)("inlineCode",{parentName:"p"},"transformSpec")," in native ingestion as an alternative to using a ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/ingestion/data-formats#flattenspec"},(0,s.kt)("inlineCode",{parentName:"a"},"flattenSpec")),' object to "flatten" nested data for ingestion.'),(0,s.kt)("p",null,"Druid supports directly ingesting nested data with the following formats: JSON, Parquet, Avro, ORC, Protobuf."),(0,s.kt)("h2",{id:"example-nested-data"},"Example nested data"),(0,s.kt)("p",null,"The examples in this topic use the JSON data in ",(0,s.kt)("a",{parentName:"p",href:"https://static.imply.io/data/nested_example_data.json"},(0,s.kt)("inlineCode",{parentName:"a"},"nested_example_data.json")),". The file contains a simple facsimile of an order tracking and shipping table."),(0,s.kt)("p",null,"When pretty-printed, a sample row in ",(0,s.kt)("inlineCode",{parentName:"p"},"nested_example_data")," looks like this:"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-json"},'{\n "time":"2022-6-14T10:32:08Z",\n "product":"Keyboard",\n "department":"Computers",\n "shipTo":{\n "firstName": "Sandra",\n "lastName": "Beatty",\n "address": {\n "street": "293 Grant Well",\n "city": "Loischester",\n "state": "FL",\n "country": "TV",\n "postalCode": "88845-0066"\n },\n "phoneNumbers": [\n {"type":"primary","number":"1-788-771-7028 x8627" },\n {"type":"secondary","number":"1-460-496-4884 x887"}\n ]\n },\n "details"{"color":"plum","price":"40.00"}\n}\n')),(0,s.kt)("h2",{id:"native-batch-ingestion"},"Native batch ingestion"),(0,s.kt)("p",null,"For native batch ingestion, you can use the ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-json-functions"},"SQL JSON functions")," to extract nested data as an alternative to using the ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/ingestion/data-formats#flattenspec"},(0,s.kt)("inlineCode",{parentName:"a"},"flattenSpec"))," input format."),(0,s.kt)("p",null,"To configure a dimension as a nested data type, specify the ",(0,s.kt)("inlineCode",{parentName:"p"},"json")," type for the dimension in the ",(0,s.kt)("inlineCode",{parentName:"p"},"dimensions")," list in the ",(0,s.kt)("inlineCode",{parentName:"p"},"dimensionsSpec")," property of your ingestion spec."),(0,s.kt)("p",null,"For example, the following ingestion spec instructs Druid to ingest ",(0,s.kt)("inlineCode",{parentName:"p"},"shipTo")," and ",(0,s.kt)("inlineCode",{parentName:"p"},"details")," as JSON-type nested dimensions:"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-json"},'{\n "type": "index_parallel",\n "spec": {\n "ioConfig": {\n "type": "index_parallel",\n "inputSource": {\n "type": "http",\n "uris": [\n "https://static.imply.io/data/nested_example_data.json"\n ]\n },\n "inputFormat": {\n "type": "json"\n }\n },\n "dataSchema": {\n "granularitySpec": {\n "segmentGranularity": "day",\n "queryGranularity": "none",\n "rollup": false\n },\n "dataSource": "nested_data_example",\n "timestampSpec": {\n "column": "time",\n "format": "auto"\n },\n "dimensionsSpec": {\n "dimensions": [\n "product",\n "department",\n {\n "type": "json",\n "name": "shipTo"\n },\n {\n "type": "json",\n "name": "details"\n }\n ]\n },\n "transformSpec": {}\n },\n "tuningConfig": {\n "type": "index_parallel",\n "partitionsSpec": {\n "type": "dynamic"\n }\n }\n }\n}\n')),(0,s.kt)("h3",{id:"transform-data-during-batch-ingestion"},"Transform data during batch ingestion"),(0,s.kt)("p",null,"You can use the ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-json-functions"},"SQL JSON functions")," to transform nested data and reference the transformed data in your ingestion spec."),(0,s.kt)("p",null,"To do this, define the output name and expression in the ",(0,s.kt)("inlineCode",{parentName:"p"},"transforms")," list in the ",(0,s.kt)("inlineCode",{parentName:"p"},"transformSpec")," object of your ingestion spec."),(0,s.kt)("p",null,"For example, the following ingestion spec extracts ",(0,s.kt)("inlineCode",{parentName:"p"},"firstName"),", ",(0,s.kt)("inlineCode",{parentName:"p"},"lastName")," and ",(0,s.kt)("inlineCode",{parentName:"p"},"address")," from ",(0,s.kt)("inlineCode",{parentName:"p"},"shipTo")," and creates a composite JSON object containing ",(0,s.kt)("inlineCode",{parentName:"p"},"product"),", ",(0,s.kt)("inlineCode",{parentName:"p"},"details")," and ",(0,s.kt)("inlineCode",{parentName:"p"},"department"),"."),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-json"},'{\n "type": "index_parallel",\n "spec": {\n "ioConfig": {\n "type": "index_parallel",\n "inputSource": {\n "type": "http",\n "uris": [\n "https://static.imply.io/data/nested_example_data.json"\n ]\n },\n "inputFormat": {\n "type": "json"\n }\n },\n "dataSchema": {\n "granularitySpec": {\n "segmentGranularity": "day",\n "queryGranularity": "none",\n "rollup": false\n },\n "dataSource": "nested_data_transform_example",\n "timestampSpec": {\n "column": "time",\n "format": "auto"\n },\n "dimensionsSpec": {\n "dimensions": [\n "firstName",\n "lastName",\n {\n "type": "json",\n "name": "address"\n },\n {\n "type": "json",\n "name": "productDetails"\n }\n ]\n },\n "transformSpec": {\n "transforms":[\n { "type":"expression", "name":"firstName", "expression":"json_value(shipTo, \'$.firstName\')"},\n { "type":"expression", "name":"lastName", "expression":"json_value(shipTo, \'$.lastName\')"},\n { "type":"expression", "name":"address", "expression":"json_query(shipTo, \'$.address\')"},\n { "type":"expression", "name":"productDetails", "expression":"json_object(\'product\', product, \'details\', details, \'department\', department)"}\n ]\n }\n },\n "tuningConfig": {\n "type": "index_parallel",\n "partitionsSpec": {\n "type": "dynamic"\n }\n }\n }\n}\n')),(0,s.kt)("h2",{id:"sql-based-ingestion"},"SQL-based ingestion"),(0,s.kt)("p",null,"To ingest nested data using SQL-based ingestion, specify ",(0,s.kt)("inlineCode",{parentName:"p"},"COMPLEX<json>")," as the value for ",(0,s.kt)("inlineCode",{parentName:"p"},"type")," when you define the row signature","\u2014",(0,s.kt)("inlineCode",{parentName:"p"},"shipTo")," and ",(0,s.kt)("inlineCode",{parentName:"p"},"details")," in the following example ingestion spec:"),(0,s.kt)("p",null,(0,s.kt)("img",{alt:"SQL-based ingestion",src:n(96917).Z,width:"2500",height:"1280"})),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-sql"},'REPLACE INTO msq_nested_data_example OVERWRITE ALL\nSELECT\n TIME_PARSE("time") as __time,\n product,\n department,\n shipTo,\n details\nFROM (\n SELECT * FROM\n TABLE(\n EXTERN(\n \'{"type":"http","uris":["https://static.imply.io/data/nested_example_data.json"]}\',\n \'{"type":"json"}\',\n \'[{"name":"time","type":"string"},{"name":"product","type":"string"},{"name":"department","type":"string"},{"name":"shipTo","type":"COMPLEX<json>"},{"name":"details","type":"COMPLEX<json>"}]\'\n )\n )\n)\nPARTITIONED BY ALL\n')),(0,s.kt)("h2",{id:"streaming-ingestion"},"Streaming ingestion"),(0,s.kt)("p",null,"You can ingest nested data into Druid using the ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/ingestion/#streaming"},"streaming method"),"\u2014","for example, from a Kafka topic."),(0,s.kt)("p",null,"When you ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/development/extensions-core/kafka-ingestion#define-a-supervisor-spec"},"define your supervisor spec"),", include a dimension with type ",(0,s.kt)("inlineCode",{parentName:"p"},"json")," for each nested column. For example, the following supervisor spec from the ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/tutorials/tutorial-kafka"},"Kafka ingestion tutorial")," contains dimensions for the nested columns ",(0,s.kt)("inlineCode",{parentName:"p"},"event"),", ",(0,s.kt)("inlineCode",{parentName:"p"},"agent"),", and ",(0,s.kt)("inlineCode",{parentName:"p"},"geo_ip")," in datasource ",(0,s.kt)("inlineCode",{parentName:"p"},"kttm-kafka"),"."),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-json"},'{\n "type": "kafka",\n "spec": {\n "ioConfig": {\n "type": "kafka",\n "consumerProperties": {\n "bootstrap.servers": "localhost:9092"\n },\n "topic": "kttm",\n "inputFormat": {\n "type": "json"\n },\n "useEarliestOffset": true\n },\n "tuningConfig": {\n "type": "kafka"\n },\n "dataSchema": {\n "dataSource": "kttm-kafka",\n "timestampSpec": {\n "column": "timestamp",\n "format": "iso"\n },\n "dimensionsSpec": {\n "dimensions": [\n "session",\n "number",\n "client_ip",\n "language",\n "adblock_list",\n "app_version",\n "path",\n "loaded_image",\n "referrer",\n "referrer_host",\n "server_ip",\n "screen",\n "window",\n {\n "type": "long",\n "name": "session_length"\n },\n "timezone",\n "timezone_offset",\n {\n "type": "json",\n "name": "event"\n },\n {\n "type": "json",\n "name": "agent"\n },\n {\n "type": "json",\n "name": "geo_ip"\n }\n ]\n },\n "granularitySpec": {\n "queryGranularity": "none",\n "rollup": false,\n "segmentGranularity": "day"\n }\n }\n }\n}\n')),(0,s.kt)("p",null,"The ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/tutorials/tutorial-kafka"},"Kafka tutorial")," guides you through the steps to load sample nested data into a Kafka topic, then ingest the data into Druid."),(0,s.kt)("h3",{id:"transform-data-during-sql-based-ingestion"},"Transform data during SQL-based ingestion"),(0,s.kt)("p",null,"You can use the ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-json-functions"},"SQL JSON functions")," to transform nested data in your ingestion query."),(0,s.kt)("p",null,"For example, the following ingestion query is the SQL-based version of the ",(0,s.kt)("a",{parentName:"p",href:"#transform-data-during-batch-ingestion"},"previous batch example"),"\u2014","it extracts ",(0,s.kt)("inlineCode",{parentName:"p"},"firstName"),", ",(0,s.kt)("inlineCode",{parentName:"p"},"lastName"),", and ",(0,s.kt)("inlineCode",{parentName:"p"},"address")," from ",(0,s.kt)("inlineCode",{parentName:"p"},"shipTo")," and creates a composite JSON object containing ",(0,s.kt)("inlineCode",{parentName:"p"},"product"),", ",(0,s.kt)("inlineCode",{parentName:"p"},"details"),", and ",(0,s.kt)("inlineCode",{parentName:"p"},"department"),"."),(0,s.kt)("p",null,(0,s.kt)("img",{alt:"SQL-based ingestion",src:n(38674).Z,width:"2500",height:"1280"})),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-sql"},'REPLACE INTO msq_nested_data_transform_example OVERWRITE ALL\nSELECT\n TIME_PARSE("time") as __time,\n JSON_VALUE(shipTo, \'$.firstName\') as firstName,\n JSON_VALUE(shipTo, \'$.lastName\') as lastName,\n JSON_QUERY(shipTo, \'$.address\') as address,\n JSON_OBJECT(\'product\':product,\'details\':details, \'department\':department) as productDetails\nFROM (\n SELECT * FROM\n TABLE(\n EXTERN(\n \'{"type":"http","uris":["https://static.imply.io/data/nested_example_data.json"]}\',\n \'{"type":"json"}\',\n \'[{"name":"time","type":"string"},{"name":"product","type":"string"},{"name":"department","type":"string"},{"name":"shipTo","type":"COMPLEX<json>"},{"name":"details","type":"COMPLEX<json>"}]\'\n )\n )\n)\nPARTITIONED BY ALL\n')),(0,s.kt)("h2",{id:"ingest-a-json-string-as-complexjson"},"Ingest a JSON string as COMPLEX<json",">"),(0,s.kt)("p",null,"If your source data contains serialized JSON strings, you can ingest the data as ",(0,s.kt)("inlineCode",{parentName:"p"},"COMPLEX<JSON>")," as follows:"),(0,s.kt)("ul",null,(0,s.kt)("li",{parentName:"ul"},"During native batch ingestion, call the ",(0,s.kt)("inlineCode",{parentName:"li"},"parse_json")," function in a ",(0,s.kt)("inlineCode",{parentName:"li"},"transform")," object in the ",(0,s.kt)("inlineCode",{parentName:"li"},"transformSpec"),"."),(0,s.kt)("li",{parentName:"ul"},"During SQL-based ingestion, use the PARSE_JSON keyword within your SELECT statement to transform the string values to JSON."),(0,s.kt)("li",{parentName:"ul"},"If you are concerned that your data may not contain valid JSON, you can use ",(0,s.kt)("inlineCode",{parentName:"li"},"try_parse_json")," for native batch or ",(0,s.kt)("inlineCode",{parentName:"li"},"TRY_PARSE_JSON")," for SQL-based ingestion. For cases where the column does not contain valid JSON, Druid inserts a null value.")),(0,s.kt)("p",null,"If you are using a text input format like ",(0,s.kt)("inlineCode",{parentName:"p"},"tsv"),", you need to use this method to ingest data into a ",(0,s.kt)("inlineCode",{parentName:"p"},"COMPLEX<json>")," column."),(0,s.kt)("p",null,"For example, consider the following deserialized row of the sample data set:"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre"},'{"time": "2022-06-13T10:10:35Z", "product": "Bike", "department":"Sports", "shipTo":"{\\"firstName\\": \\"Henry\\",\\"lastName\\": \\"Wuckert\\",\\"address\\": {\\"street\\": \\"5643 Jan Walk\\",\\"city\\": \\"Lake Bridget\\",\\"state\\": \\"HI\\",\\"country\\":\\"ME\\",\\"postalCode\\": \\"70204-2939\\"},\\"phoneNumbers\\": [{\\"type\\":\\"primary\\",\\"number\\":\\"593.475.0449 x86733\\" },{\\"type\\":\\"secondary\\",\\"number\\":\\"638-372-1210\\"}]}", "details":"{\\"color\\":\\"ivory\\", \\"price\\":955.00}"}\n')),(0,s.kt)("p",null,"The following examples demonstrate how to ingest the ",(0,s.kt)("inlineCode",{parentName:"p"},"shipTo")," and ",(0,s.kt)("inlineCode",{parentName:"p"},"details")," columns both as string type and as ",(0,s.kt)("inlineCode",{parentName:"p"},"COMPLEX<json>")," in the ",(0,s.kt)("inlineCode",{parentName:"p"},"shipTo_parsed")," and ",(0,s.kt)("inlineCode",{parentName:"p"},"details_parsed")," columns."),(0,s.kt)(i.Z,{mdxType:"Tabs"},(0,s.kt)(o.Z,{value:"1",label:"SQL",mdxType:"TabItem"},(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre"},'REPLACE INTO deserialized_example OVERWRITE ALL\nWITH source AS (SELECT * FROM TABLE(\n EXTERN(\n \'{"type":"inline","data":"{\\"time\\": \\"2022-06-13T10:10:35Z\\", \\"product\\": \\"Bike\\", \\"department\\":\\"Sports\\", \\"shipTo\\":\\"{\\\\\\"firstName\\\\\\": \\\\\\"Henry\\\\\\",\\\\\\"lastName\\\\\\": \\\\\\"Wuckert\\\\\\",\\\\\\"address\\\\\\": {\\\\\\"street\\\\\\": \\\\\\"5643 Jan Walk\\\\\\",\\\\\\"city\\\\\\": \\\\\\"Lake Bridget\\\\\\",\\\\\\"state\\\\\\": \\\\\\"HI\\\\\\",\\\\\\"country\\\\\\":\\\\\\"ME\\\\\\",\\\\\\"postalCode\\\\\\": \\\\\\"70204-2939\\\\\\"},\\\\\\"phoneNumbers\\\\\\": [{\\\\\\"type\\\\\\":\\\\\\"primary\\\\\\",\\\\\\"number\\\\\\":\\\\\\"593.475.0449 x86733\\\\\\" },{\\\\\\"type\\\\\\":\\\\\\"secondary\\\\\\",\\\\\\"number\\\\\\":\\\\\\"638-372-1210\\\\\\"}]}\\", \\"details\\":\\"{\\\\\\"color\\\\\\":\\\\\\"ivory\\\\\\", \\\\\\"price\\\\\\":955.00}\\"}\\n"}\',\n \'{"type":"json"}\',\n \'[{"name":"time","type":"string"},{"name":"product","type":"string"},{"name":"department","type":"string"},{"name":"shipTo","type":"string"},{"name":"details","type":"string"}]\'\n )\n))\nSELECT\n TIME_PARSE("time") AS __time,\n "product",\n "department",\n "shipTo",\n "details",\n PARSE_JSON("shipTo") as "shipTo_parsed",\n PARSE_JSON("details") as "details_parsed"\nFROM source\nPARTITIONED BY DAY\n'))),(0,s.kt)(o.Z,{value:"2",label:"Native batch",mdxType:"TabItem"},(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre"},'{\n "type": "index_parallel",\n "spec": {\n "ioConfig": {\n "type": "index_parallel",\n "inputSource": {\n "type": "inline",\n "data": "{\\"time\\": \\"2022-06-13T10:10:35Z\\", \\"product\\": \\"Bike\\", \\"department\\":\\"Sports\\", \\"shipTo\\":\\"{\\\\\\"firstName\\\\\\": \\\\\\"Henry\\\\\\",\\\\\\"lastName\\\\\\": \\\\\\"Wuckert\\\\\\",\\\\\\"address\\\\\\": {\\\\\\"street\\\\\\": \\\\\\"5643 Jan Walk\\\\\\",\\\\\\"city\\\\\\": \\\\\\"Lake Bridget\\\\\\",\\\\\\"state\\\\\\": \\\\\\"HI\\\\\\",\\\\\\"country\\\\\\":\\\\\\"ME\\\\\\",\\\\\\"postalCode\\\\\\": \\\\\\"70204-2939\\\\\\"},\\\\\\"phoneNumbers\\\\\\": [{\\\\\\"type\\\\\\":\\\\\\"primary\\\\\\",\\\\\\"number\\\\\\":\\\\\\"593.475.0449 x86733\\\\\\" },{\\\\\\"type\\\\\\":\\\\\\"secondary\\\\\\",\\\\\\"number\\\\\\":\\\\\\"638-372-1210\\\\\\"}]}\\", \\"details\\":\\"{\\\\\\"color\\\\\\":\\\\\\"ivory\\\\\\", \\\\\\"price\\\\\\":955.00}\\"}\\n"\n },\n "inputFormat": {\n "type": "json"\n }\n },\n "tuningConfig": {\n "type": "index_parallel",\n "partitionsSpec": {\n "type": "dynamic"\n }\n },\n "dataSchema": {\n "dataSource": "deserialized_example",\n "timestampSpec": {\n "column": "time",\n "format": "iso"\n },\n "transformSpec": {\n "transforms": [\n {\n "type": "expression",\n "name": "shipTo_parsed",\n "expression": "parse_json(shipTo)"\n },\n {\n "type": "expression",\n "name": "details_parsed",\n "expression": "parse_json(details)"\n }\n ]\n },\n "dimensionsSpec": {\n "dimensions": [\n "product",\n "department",\n "shipTo",\n "details",\n "shipTo_parsed",\n "details_parsed"\n ]\n },\n "granularitySpec": {\n "queryGranularity": "none",\n "rollup": false,\n "segmentGranularity": "day"\n }\n }\n }\n}\n')))),(0,s.kt)("h2",{id:"querying-nested-columns"},"Querying nested columns"),(0,s.kt)("p",null,"Once ingested, Druid stores the JSON-typed columns as native JSON objects and presents them as ",(0,s.kt)("inlineCode",{parentName:"p"},"COMPLEX<json>"),"."),(0,s.kt)("p",null,"See the ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-json-functions"},"Nested columns functions reference")," for information on the functions in the examples below."),(0,s.kt)("p",null,"Druid supports a small, simplified subset of the ",(0,s.kt)("a",{parentName:"p",href:"https://github.com/json-path/JsonPath/blob/master/README.md"},"JSONPath syntax")," operators, primarily limited to extracting individual values from nested data structures. See the ",(0,s.kt)("a",{parentName:"p",href:"/docs/latest/querying/sql-json-functions#jsonpath-syntax"},"SQL JSON functions")," page for details."),(0,s.kt)("h3",{id:"displaying-data-types"},"Displaying data types"),(0,s.kt)("p",null,"The following example illustrates how you can display the data types for your columns. Note that ",(0,s.kt)("inlineCode",{parentName:"p"},"details")," and ",(0,s.kt)("inlineCode",{parentName:"p"},"shipTo")," display as ",(0,s.kt)("inlineCode",{parentName:"p"},"COMPLEX<json>"),"."),(0,s.kt)("h4",{id:"example-query-display-data-types"},"Example query: Display data types"),(0,s.kt)("p",null,(0,s.kt)("img",{alt:"Display data types",src:n(47953).Z,width:"2500",height:"1280"})),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-sql"},"SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE\nFROM INFORMATION_SCHEMA.COLUMNS\nWHERE TABLE_NAME = 'nested_data_example'\n")),(0,s.kt)("p",null,"Example query results:"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-json"},'[["TABLE_NAME","COLUMN_NAME","DATA_TYPE"],["STRING","STRING","STRING"],["VARCHAR","VARCHAR","VARCHAR"],["nested_data_example","__time","TIMESTAMP"],["nested_data_example","department","VARCHAR"],["nested_data_example","details","COMPLEX<json>"],["nested_data_example","product","VARCHAR"],["nested_data_example","shipTo","COMPLEX<json>"]]\n')),(0,s.kt)("h3",{id:"retrieving-json-data"},"Retrieving JSON data"),(0,s.kt)("p",null,"You can retrieve JSON data directly from a table. Druid returns the results as a JSON object, so you can't use grouping, aggregation, or filtering operators."),(0,s.kt)("h4",{id:"example-query-retrieve-json-data"},"Example query: Retrieve JSON data"),(0,s.kt)("p",null,"The following example query extracts all data from ",(0,s.kt)("inlineCode",{parentName:"p"},"nested_data_example"),":"),(0,s.kt)("p",null,(0,s.kt)("img",{alt:"Retrieve JSON data",src:n(25865).Z,width:"2500",height:"1280"})),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-sql"},"SELECT * FROM nested_data_example\n")),(0,s.kt)("p",null,"Example query results:"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-json"},'[["__time","department","details","product","shipTo"],["LONG","STRING","COMPLEX<json>","STRING","COMPLEX<json>"],["TIMESTAMP","VARCHAR","OTHER","VARCHAR","OTHER"],["2022-06-13T07:52:29.000Z","Sports","{\\"color\\":\\"sky blue\\",\\"price\\":542.0}","Bike","{\\"firstName\\":\\"Russ\\",\\"lastName\\":\\"Cole\\",\\"address\\":{\\"street\\":\\"77173 Rusty Station\\",\\"city\\":\\"South Yeseniabury\\",\\"state\\":\\"WA\\",\\"country\\":\\"BL\\",\\"postalCode\\":\\"01893\\"},\\"phoneNumbers\\":[{\\"type\\":\\"primary\\",\\"number\\":\\"891-374-6188 x74568\\"},{\\"type\\":\\"secondary\\",\\"number\\":\\"1-248-998-4426 x33037\\"}]}"],["2022-06-13T10:10:35.000Z","Sports","{\\"color\\":\\"ivory\\",\\"price\\":955.0}","Bike","{\\"firstName\\":\\"Henry\\",\\"lastName\\":\\"Wuckert\\",\\"address\\":{\\"street\\":\\"5643 Jan Walk\\",\\"city\\":\\"Lake Bridget\\",\\"state\\":\\"HI\\",\\"country\\":\\"ME\\",\\"postalCode\\":\\"70204-2939\\"},\\"phoneNumbers\\":[{\\"type\\":\\"primary\\",\\"number\\":\\"593.475.0449 x86733\\"},{\\"type\\":\\"secondary\\",\\"number\\":\\"638-372-1210\\"}]}"],["2022-06-13T13:57:38.000Z","Grocery","{\\"price\\":8.0}","Sausages","{\\"firstName\\":\\"Forrest\\",\\"lastName\\":\\"Brekke\\",\\"address\\":{\\"street\\":\\"41548 Collier Divide\\",\\"city\\":\\"Wintheiserborough\\",\\"state\\":\\"WA\\",\\"country\\":\\"AD\\",\\"postalCode\\":\\"27577-6784\\"},\\"phoneNumbers\\":[{\\"type\\":\\"primary\\",\\"number\\":\\"(904) 890-0696 x581\\"},{\\"type\\":\\"secondary\\",\\"number\\":\\"676.895.6759\\"}]}"],["2022-06-13T21:37:06.000Z","Computers","{\\"color\\":\\"olive\\",\\"price\\":90.0}","Mouse","{\\"firstName\\":\\"Rickey\\",\\"lastName\\":\\"Rempel\\",\\"address\\":{\\"street\\":\\"6232 Green Glens\\",\\"city\\":\\"New Fermin\\",\\"state\\":\\"HI\\",\\"country\\":\\"CW\\",\\"postalCode\\":\\"98912-1195\\"},\\"phoneNumbers\\":[{\\"type\\":\\"primary\\",\\"number\\":\\"(689) 766-4272 x60778\\"},{\\"type\\":\\"secondary\\",\\"number\\":\\"375.662.4737 x24707\\"}]}"],["2022-06-14T10:32:08.000Z","Computers","{\\"color\\":\\"plum\\",\\"price\\":40.0}","Keyboard","{\\"firstName\\":\\"Sandra\\",\\"lastName\\":\\"Beatty\\",\\"address\\":{\\"street\\":\\"293 Grant Well\\",\\"city\\":\\"Loischester\\",\\"state\\":\\"FL\\",\\"country\\":\\"TV\\",\\"postalCode\\":\\"88845-0066\\"},\\"phoneNumbers\\":[{\\"type\\":\\"primary\\",\\"number\\":\\"1-788-771-7028 x8627\\"},{\\"type\\":\\"secondary\\",\\"number\\":\\"1-460-496-4884 x887\\"}]}"]]\n')),(0,s.kt)("h3",{id:"extracting-nested-data-elements"},"Extracting nested data elements"),(0,s.kt)("p",null,"The ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_VALUE")," function is specially optimized to provide native Druid level performance when processing nested literal values, as if they were flattened, traditional, Druid column types. It does this by reading from the specialized nested columns and indexes that are built and stored in JSON objects when Druid creates segments."),(0,s.kt)("p",null,"Some operations using ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_VALUE")," run faster than those using native Druid columns. For example, filtering numeric types uses the indexes built for nested numeric columns, which are not available for Druid DOUBLE, FLOAT, or LONG columns."),(0,s.kt)("p",null,(0,s.kt)("inlineCode",{parentName:"p"},"JSON_VALUE")," only returns literal types. Any paths that reference JSON objects or array types return null."),(0,s.kt)("admonition",{type:"info"},(0,s.kt)("p",{parentName:"admonition"}," To achieve the best possible performance, use the ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_VALUE")," function whenever you query JSON objects.")),(0,s.kt)("h4",{id:"example-query-extract-nested-data-elements"},"Example query: Extract nested data elements"),(0,s.kt)("p",null,"The following example query illustrates how to use ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_VALUE")," to extract specified elements from a ",(0,s.kt)("inlineCode",{parentName:"p"},"COMPLEX<json>")," object. Note that the returned values default to type VARCHAR."),(0,s.kt)("p",null,(0,s.kt)("img",{alt:"Extract nested data elements",src:n(77958).Z,width:"2500",height:"1280"})),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-sql"},"SELECT\n product,\n department,\n JSON_VALUE(shipTo, '$.address.country') as country,\n JSON_VALUE(shipTo, '$.phoneNumbers[0].number') as primaryPhone,\n JSON_VALUE(details, '$.price') as price\nFROM nested_data_example\n")),(0,s.kt)("p",null,"Example query results:"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-json"},'[["product","department","country","primaryPhone","price"],["STRING","STRING","STRING","STRING","STRING"],["VARCHAR","VARCHAR","VARCHAR","VARCHAR","VARCHAR"],["Bike","Sports","BL","891-374-6188 x74568","542.0"],["Bike","Sports","ME","593.475.0449 x86733","955.0"],["Sausages","Grocery","AD","(904) 890-0696 x581","8.0"],["Mouse","Computers","CW","(689) 766-4272 x60778","90.0"],["Keyboard","Computers","TV","1-788-771-7028 x8627","40.0"]]\n')),(0,s.kt)("h3",{id:"extracting-nested-data-elements-as-a-suggested-type"},"Extracting nested data elements as a suggested type"),(0,s.kt)("p",null,"You can use the ",(0,s.kt)("inlineCode",{parentName:"p"},"RETURNING")," keyword to provide type hints to the ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_VALUE")," function. This way the SQL planner produces the correct native Druid query, leading to expected results. This keyword allows you to specify a SQL type for the ",(0,s.kt)("inlineCode",{parentName:"p"},"path")," value."),(0,s.kt)("h4",{id:"example-query-extract-nested-data-elements-as-suggested-types"},"Example query: Extract nested data elements as suggested types"),(0,s.kt)("p",null,"The following example query illustrates how to use ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_VALUE")," and the ",(0,s.kt)("inlineCode",{parentName:"p"},"RETURNING")," keyword to extract an element of nested data and return it as specified types."),(0,s.kt)("p",null,(0,s.kt)("img",{alt:"Extract nested data elements as a suggested type",src:n(21062).Z,width:"2500",height:"1280"})),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-sql"},"SELECT\n product,\n department,\n JSON_VALUE(shipTo, '$.address.country') as country,\n JSON_VALUE(details, '$.price' RETURNING BIGINT) as price_int,\n JSON_VALUE(details, '$.price' RETURNING DECIMAL) as price_decimal,\n JSON_VALUE(details, '$.price' RETURNING VARCHAR) as price_varchar\nFROM nested_data_example\n")),(0,s.kt)("p",null,"Query results:"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-json"},'[["product","department","country","price_int","price_decimal","price_varchar"],["STRING","STRING","STRING","LONG","DOUBLE","STRING"],["VARCHAR","VARCHAR","VARCHAR","BIGINT","DECIMAL","VARCHAR"],["Bike","Sports","BL",542,542.0,"542.0"],["Bike","Sports","ME",955,955.0,"955.0"],["Sausages","Grocery","AD",8,8.0,"8.0"],["Mouse","Computers","CW",90,90.0,"90.0"],["Keyboard","Computers","TV",40,40.0,"40.0"]]\n')),(0,s.kt)("h3",{id:"grouping-aggregating-and-filtering"},"Grouping, aggregating, and filtering"),(0,s.kt)("p",null,"You can use ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_VALUE")," expressions in any context where you can use traditional Druid columns, such as grouping, aggregation, and filtering."),(0,s.kt)("h4",{id:"example-query-grouping-and-filtering"},"Example query: Grouping and filtering"),(0,s.kt)("p",null,"The following example query illustrates how to use SUM, WHERE, GROUP BY, and ORDER BY operators with ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_VALUE"),"."),(0,s.kt)("p",null,(0,s.kt)("img",{alt:"Group, aggregate, filter",src:n(80168).Z,width:"2500",height:"1280"})),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-sql"},"SELECT\n product,\n JSON_VALUE(shipTo, '$.address.country'),\n SUM(JSON_VALUE(details, '$.price' RETURNING BIGINT))\nFROM nested_data_example\nWHERE JSON_VALUE(shipTo, '$.address.country') in ('BL', 'CW')\nGROUP BY 1,2\nORDER BY 3 DESC\n")),(0,s.kt)("p",null,"Example query results:"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-json"},'[["product","EXPR$1","EXPR$2"],["STRING","STRING","LONG"],["VARCHAR","VARCHAR","BIGINT"],["Bike","BL",542],["Mouse","CW",90]]\n')),(0,s.kt)("h3",{id:"transforming-json-object-data"},"Transforming JSON object data"),(0,s.kt)("p",null,"In addition to ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_VALUE"),", Druid offers a number of operators that focus on transforming JSON object data:"),(0,s.kt)("ul",null,(0,s.kt)("li",{parentName:"ul"},(0,s.kt)("inlineCode",{parentName:"li"},"JSON_QUERY")),(0,s.kt)("li",{parentName:"ul"},(0,s.kt)("inlineCode",{parentName:"li"},"JSON_OBJECT")),(0,s.kt)("li",{parentName:"ul"},(0,s.kt)("inlineCode",{parentName:"li"},"PARSE_JSON")),(0,s.kt)("li",{parentName:"ul"},(0,s.kt)("inlineCode",{parentName:"li"},"TO_JSON_STRING"))),(0,s.kt)("p",null,"These functions are primarily intended for use with SQL-based ingestion to transform data during insert operations, but they also work in traditional Druid SQL queries. Because most of these functions output JSON objects, they have the same limitations when used in traditional Druid queries as interacting with the JSON objects directly."),(0,s.kt)("h4",{id:"example-query-return-results-in-a-json-object"},"Example query: Return results in a JSON object"),(0,s.kt)("p",null,"You can use the ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_QUERY")," function to extract a partial structure from any JSON input and return results in a JSON object. Unlike ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_VALUE")," it can extract objects and arrays."),(0,s.kt)("p",null,"The following example query illustrates the differences in output between ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_VALUE")," and ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_QUERY"),". The two output columns for ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_VALUE")," contain null values only because ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_VALUE")," only returns literal types."),(0,s.kt)("p",null,(0,s.kt)("img",{alt:"Return results in a JSON object",src:n(71441).Z,width:"2500",height:"1280"})),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-sql"},"SELECT\n JSON_VALUE(shipTo, '$.address'),\n JSON_QUERY(shipTo, '$.address'),\n JSON_VALUE(shipTo, '$.phoneNumbers'),\n JSON_QUERY(shipTo, '$.phoneNumbers')\nFROM nested_data_example\n")),(0,s.kt)("p",null,"Example query results:"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-json"},'[["EXPR$0","EXPR$1","EXPR$2","EXPR$3"],["STRING","COMPLEX<json>","STRING","COMPLEX<json>"],["VARCHAR","OTHER","VARCHAR","OTHER"],["","{\\"street\\":\\"77173 Rusty Station\\",\\"city\\":\\"South Yeseniabury\\",\\"state\\":\\"WA\\",\\"country\\":\\"BL\\",\\"postalCode\\":\\"01893\\"}","","[{\\"type\\":\\"primary\\",\\"number\\":\\"891-374-6188 x74568\\"},{\\"type\\":\\"secondary\\",\\"number\\":\\"1-248-998-4426 x33037\\"}]"],["","{\\"street\\":\\"5643 Jan Walk\\",\\"city\\":\\"Lake Bridget\\",\\"state\\":\\"HI\\",\\"country\\":\\"ME\\",\\"postalCode\\":\\"70204-2939\\"}","","[{\\"type\\":\\"primary\\",\\"number\\":\\"593.475.0449 x86733\\"},{\\"type\\":\\"secondary\\",\\"number\\":\\"638-372-1210\\"}]"],["","{\\"street\\":\\"41548 Collier Divide\\",\\"city\\":\\"Wintheiserborough\\",\\"state\\":\\"WA\\",\\"country\\":\\"AD\\",\\"postalCode\\":\\"27577-6784\\"}","","[{\\"type\\":\\"primary\\",\\"number\\":\\"(904) 890-0696 x581\\"},{\\"type\\":\\"secondary\\",\\"number\\":\\"676.895.6759\\"}]"],["","{\\"street\\":\\"6232 Green Glens\\",\\"city\\":\\"New Fermin\\",\\"state\\":\\"HI\\",\\"country\\":\\"CW\\",\\"postalCode\\":\\"98912-1195\\"}","","[{\\"type\\":\\"primary\\",\\"number\\":\\"(689) 766-4272 x60778\\"},{\\"type\\":\\"secondary\\",\\"number\\":\\"375.662.4737 x24707\\"}]"],["","{\\"street\\":\\"293 Grant Well\\",\\"city\\":\\"Loischester\\",\\"state\\":\\"FL\\",\\"country\\":\\"TV\\",\\"postalCode\\":\\"88845-0066\\"}","","[{\\"type\\":\\"primary\\",\\"number\\":\\"1-788-771-7028 x8627\\"},{\\"type\\":\\"secondary\\",\\"number\\":\\"1-460-496-4884 x887\\"}]"]]\n')),(0,s.kt)("h4",{id:"example-query-combine-multiple-json-inputs-into-a-single-json-object-value"},"Example query: Combine multiple JSON inputs into a single JSON object value"),(0,s.kt)("p",null," The following query illustrates how to use ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_OBJECT")," to combine nested data elements into a new object."),(0,s.kt)("p",null,(0,s.kt)("img",{alt:"Combine JSON inputs",src:n(6580).Z,width:"2500",height:"1280"})),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-sql"},"SELECT\n JSON_OBJECT(KEY 'shipTo' VALUE JSON_QUERY(shipTo, '$'), KEY 'details' VALUE JSON_QUERY(details, '$')) as combinedJson\nFROM nested_data_example\n")),(0,s.kt)("p",null,"Example query results:"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-json"},'[["combinedJson"],["COMPLEX<json>"],["OTHER"],["{\\"details\\":{\\"color\\":\\"sky blue\\",\\"price\\":542.0},\\"shipTo\\":{\\"firstName\\":\\"Russ\\",\\"lastName\\":\\"Cole\\",\\"address\\":{\\"street\\":\\"77173 Rusty Station\\",\\"city\\":\\"South Yeseniabury\\",\\"state\\":\\"WA\\",\\"country\\":\\"BL\\",\\"postalCode\\":\\"01893\\"},\\"phoneNumbers\\":[{\\"type\\":\\"primary\\",\\"number\\":\\"891-374-6188 x74568\\"},{\\"type\\":\\"secondary\\",\\"number\\":\\"1-248-998-4426 x33037\\"}]}}"],["{\\"details\\":{\\"color\\":\\"ivory\\",\\"price\\":955.0},\\"shipTo\\":{\\"firstName\\":\\"Henry\\",\\"lastName\\":\\"Wuckert\\",\\"address\\":{\\"street\\":\\"5643 Jan Walk\\",\\"city\\":\\"Lake Bridget\\",\\"state\\":\\"HI\\",\\"country\\":\\"ME\\",\\"postalCode\\":\\"70204-2939\\"},\\"phoneNumbers\\":[{\\"type\\":\\"primary\\",\\"number\\":\\"593.475.0449 x86733\\"},{\\"type\\":\\"secondary\\",\\"number\\":\\"638-372-1210\\"}]}}"],["{\\"details\\":{\\"price\\":8.0},\\"shipTo\\":{\\"firstName\\":\\"Forrest\\",\\"lastName\\":\\"Brekke\\",\\"address\\":{\\"street\\":\\"41548 Collier Divide\\",\\"city\\":\\"Wintheiserborough\\",\\"state\\":\\"WA\\",\\"country\\":\\"AD\\",\\"postalCode\\":\\"27577-6784\\"},\\"phoneNumbers\\":[{\\"type\\":\\"primary\\",\\"number\\":\\"(904) 890-0696 x581\\"},{\\"type\\":\\"secondary\\",\\"number\\":\\"676.895.6759\\"}]}}"],["{\\"details\\":{\\"color\\":\\"olive\\",\\"price\\":90.0},\\"shipTo\\":{\\"firstName\\":\\"Rickey\\",\\"lastName\\":\\"Rempel\\",\\"address\\":{\\"street\\":\\"6232 Green Glens\\",\\"city\\":\\"New Fermin\\",\\"state\\":\\"HI\\",\\"country\\":\\"CW\\",\\"postalCode\\":\\"98912-1195\\"},\\"phoneNumbers\\":[{\\"type\\":\\"primary\\",\\"number\\":\\"(689) 766-4272 x60778\\"},{\\"type\\":\\"secondary\\",\\"number\\":\\"375.662.4737 x24707\\"}]}}"],["{\\"details\\":{\\"color\\":\\"plum\\",\\"price\\":40.0},\\"shipTo\\":{\\"firstName\\":\\"Sandra\\",\\"lastName\\":\\"Beatty\\",\\"address\\":{\\"street\\":\\"293 Grant Well\\",\\"city\\":\\"Loischester\\",\\"state\\":\\"FL\\",\\"country\\":\\"TV\\",\\"postalCode\\":\\"88845-0066\\"},\\"phoneNumbers\\":[{\\"type\\":\\"primary\\",\\"number\\":\\"1-788-771-7028 x8627\\"},{\\"type\\":\\"secondary\\",\\"number\\":\\"1-460-496-4884 x887\\"}]}}"]]\n')),(0,s.kt)("h3",{id:"using-other-transform-functions"},"Using other transform functions"),(0,s.kt)("p",null,"Druid provides the following additional transform functions:"),(0,s.kt)("ul",null,(0,s.kt)("li",{parentName:"ul"},(0,s.kt)("inlineCode",{parentName:"li"},"PARSE_JSON"),": Deserializes a string value into a JSON object."),(0,s.kt)("li",{parentName:"ul"},(0,s.kt)("inlineCode",{parentName:"li"},"TO_JSON_STRING"),": Performs the operation of ",(0,s.kt)("inlineCode",{parentName:"li"},"TO_JSON")," and then serializes the value into a string.")),(0,s.kt)("h4",{id:"example-query-parse-and-deserialize-data"},"Example query: Parse and deserialize data"),(0,s.kt)("p",null," The following query illustrates how to use the transform functions to parse and deserialize data."),(0,s.kt)("p",null,(0,s.kt)("img",{alt:"Parse and deserialize data",src:n(95881).Z,width:"2500",height:"1280"})),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-sql"},'SELECT\n PARSE_JSON(\'{"x":"y"}\'),\n TO_JSON_STRING(\'{"x":"y"}\'),\n TO_JSON_STRING(PARSE_JSON(\'{"x":"y"}\'))\n')),(0,s.kt)("p",null,"Example query results:"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-json"},'[["EXPR$0","EXPR$2","EXPR$3"],["COMPLEX<json>","STRING","STRING"],["OTHER","VARCHAR","VARCHAR"],["{\\"x\\":\\"y\\"}","\\"{\\\\\\"x\\\\\\":\\\\\\"y\\\\\\"}\\"","{\\"x\\":\\"y\\"}"]]\n')),(0,s.kt)("h3",{id:"using-helper-operators"},"Using helper operators"),(0,s.kt)("p",null,"The ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_KEYS")," and ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_PATHS")," functions are helper operators that you can use to examine JSON object schema. Use them to plan your queries, for example to work out which paths to use in ",(0,s.kt)("inlineCode",{parentName:"p"},"JSON_VALUE"),"."),(0,s.kt)("h4",{id:"example-query-examine-json-object-schema"},"Example query: Examine JSON object schema"),(0,s.kt)("p",null," The following query illustrates how to use the helper operators to examine a nested data object."),(0,s.kt)("p",null,(0,s.kt)("img",{alt:"Examine JSON object schema",src:n(18667).Z,width:"2500",height:"1280"})),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-sql"},"SELECT\n ARRAY_CONCAT_AGG(DISTINCT JSON_KEYS(shipTo, '$.')),\n ARRAY_CONCAT_AGG(DISTINCT JSON_KEYS(shipTo, '$.address')),\n ARRAY_CONCAT_AGG(DISTINCT JSON_PATHS(shipTo))\nFROM nested_data_example\n")),(0,s.kt)("p",null,"Example query results:"),(0,s.kt)("pre",null,(0,s.kt)("code",{parentName:"pre",className:"language-json"},'[["EXPR$0","EXPR$1","EXPR$2","EXPR$3"],["COMPLEX<json>","COMPLEX<json>","STRING","STRING"],["OTHER","OTHER","VARCHAR","VARCHAR"],["{\\"x\\":\\"y\\"}","\\"{\\\\\\"x\\\\\\":\\\\\\"y\\\\\\"}\\"","\\"{\\\\\\"x\\\\\\":\\\\\\"y\\\\\\"}\\"","{\\"x\\":\\"y\\"}"]]\n')),(0,s.kt)("h2",{id:"known-issues"},"Known issues"),(0,s.kt)("p",null,"Before you start using the nested columns feature, consider the following known issues:"),(0,s.kt)("ul",null,(0,s.kt)("li",{parentName:"ul"},"Directly using ",(0,s.kt)("inlineCode",{parentName:"li"},"COMPLEX<json>")," columns and expressions is not well integrated into the Druid query engine. It can result in errors or undefined behavior when grouping and filtering, and when you use ",(0,s.kt)("inlineCode",{parentName:"li"},"COMPLEX<json>")," objects as inputs to aggregators. As a workaround, consider using ",(0,s.kt)("inlineCode",{parentName:"li"},"TO_JSON_STRING")," to coerce the values to strings before you perform these operations."),(0,s.kt)("li",{parentName:"ul"},"Directly using array-typed outputs from ",(0,s.kt)("inlineCode",{parentName:"li"},"JSON_KEYS")," and ",(0,s.kt)("inlineCode",{parentName:"li"},"JSON_PATHS")," is moderately supported by the Druid query engine. You can group on these outputs, and there are a number of array expressions that can operate on these values, such as ",(0,s.kt)("inlineCode",{parentName:"li"},"ARRAY_CONCAT_AGG"),". However, some operations are not well defined for use outside array-specific functions, such as filtering using ",(0,s.kt)("inlineCode",{parentName:"li"},"=")," or ",(0,s.kt)("inlineCode",{parentName:"li"},"IS NULL"),"."),(0,s.kt)("li",{parentName:"ul"},"Input validation for JSON SQL operators is currently incomplete, which sometimes results in undefined behavior or unhelpful error messages."),(0,s.kt)("li",{parentName:"ul"},"Ingesting data with a very complex nested structure is potentially an expensive operation and may require you to tune ingestion tasks and/or cluster parameters to account for increased memory usage or overall task run time. When you tune your ingestion configuration, treat each nested literal field inside an object as a flattened top-level Druid column.")),(0,s.kt)("h2",{id:"further-reading"},"Further reading"),(0,s.kt)("p",null,"For more information, see the following pages:"),(0,s.kt)("ul",null,(0,s.kt)("li",{parentName:"ul"},(0,s.kt)("a",{parentName:"li",href:"/docs/latest/querying/sql-json-functions"},"Nested columns functions reference")," for details of the functions used in the examples on this page."),(0,s.kt)("li",{parentName:"ul"},(0,s.kt)("a",{parentName:"li",href:"/docs/latest/multi-stage-query/"},"Multi-stage query architecture overview")," for information on how to set up and use this feature."),(0,s.kt)("li",{parentName:"ul"},(0,s.kt)("a",{parentName:"li",href:"/docs/latest/ingestion/ingestion-spec"},"Ingestion spec reference")," for information on native ingestion and ",(0,s.kt)("a",{parentName:"li",href:"/docs/latest/ingestion/ingestion-spec#transformspec"},(0,s.kt)("inlineCode",{parentName:"a"},"transformSpec")),"."),(0,s.kt)("li",{parentName:"ul"},(0,s.kt)("a",{parentName:"li",href:"/docs/latest/ingestion/data-formats"},"Data formats")," for information on ",(0,s.kt)("a",{parentName:"li",href:"/docs/latest/ingestion/data-formats#flattenspec"},(0,s.kt)("inlineCode",{parentName:"a"},"flattenSpec")),".")))}N.isMDXComponent=!0},6580:(e,t,n)=>{n.d(t,{Z:()=>a});const a=n.p+"assets/images/nested-combined-json-eb679cfee86dc9d1c5569ac094e766e1.png"},47953:(e,t,n)=>{n.d(t,{Z:()=>a});const a=n.p+"assets/images/nested-display-data-types-5dd733147ce48db2a498602be5cfc9ec.png"},18667:(e,t,n)=>{n.d(t,{Z:()=>a});const a=n.p+"assets/images/nested-examine-schema-ec0698cf3626fab096319382a506e1b6.png"},21062:(e,t,n)=>{n.d(t,{Z:()=>a});const a=n.p+"assets/images/nested-extract-as-type-10f1147de2c0b71bdc64f9b59b8fbd04.png"},77958:(e,t,n)=>{n.d(t,{Z:()=>a});const a=n.p+"assets/images/nested-extract-elements-1ee0f860a7a0771572c4a2f08e6f1e5e.png"},80168:(e,t,n)=>{n.d(t,{Z:()=>a});const a=n.p+"assets/images/nested-group-aggregate-ec3088ac449e81177b38ab9d9c39ec49.png"},38674:(e,t,n)=>{n.d(t,{Z:()=>a});const a=n.p+"assets/images/nested-msq-ingestion-transform-4f53f816ca16703df601f9c4f1aeee6d.png"},96917:(e,t,n)=>{n.d(t,{Z:()=>a});const a=n.p+"assets/images/nested-msq-ingestion-62d187c343c4323ecec7e32a684a61c1.png"},95881:(e,t,n)=>{n.d(t,{Z:()=>a});const a=n.p+"assets/images/nested-parse-deserialize-cc70017d4a08d1ea8d0d37f53091ee89.png"},25865:(e,t,n)=>{n.d(t,{Z:()=>a});const a=n.p+"assets/images/nested-retrieve-json-facaa1be9754d33d32e59c6f2c2d8f73.png"},71441:(e,t,n)=>{n.d(t,{Z:()=>a});const a=n.p+"assets/images/nested-return-json-a456b98c2e405203a1d96930cd7af001.png"}}]);