blob: 21e0dec47246eb7be3299e8c43c6fbbd4e39d44e [file] [log] [blame]
"use strict";(self.webpackChunk=self.webpackChunk||[]).push([[6502],{15680:(e,t,a)=>{a.d(t,{xA:()=>p,yg:()=>m});var r=a(96540);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 i(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 o(e){for(var t=1;t<arguments.length;t++){var a=null!=arguments[t]?arguments[t]:{};t%2?i(Object(a),!0).forEach((function(t){n(e,t,a[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(a)):i(Object(a)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(a,t))}))}return e}function s(e,t){if(null==e)return{};var a,r,n=function(e,t){if(null==e)return{};var a,r,n={},i=Object.keys(e);for(r=0;r<i.length;r++)a=i[r],t.indexOf(a)>=0||(n[a]=e[a]);return n}(e,t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(e);for(r=0;r<i.length;r++)a=i[r],t.indexOf(a)>=0||Object.prototype.propertyIsEnumerable.call(e,a)&&(n[a]=e[a])}return n}var l=r.createContext({}),c=function(e){var t=r.useContext(l),a=t;return e&&(a="function"==typeof e?e(t):o(o({},t),e)),a},p=function(e){var t=c(e.components);return r.createElement(l.Provider,{value:t},e.children)},u="mdxType",d={inlineCode:"code",wrapper:function(e){var t=e.children;return r.createElement(r.Fragment,{},t)}},g=r.forwardRef((function(e,t){var a=e.components,n=e.mdxType,i=e.originalType,l=e.parentName,p=s(e,["components","mdxType","originalType","parentName"]),u=c(a),g=n,m=u["".concat(l,".").concat(g)]||u[g]||d[g]||i;return a?r.createElement(m,o(o({ref:t},p),{},{components:a})):r.createElement(m,o({ref:t},p))}));function m(e,t){var a=arguments,n=t&&t.mdxType;if("string"==typeof e||n){var i=a.length,o=new Array(i);o[0]=g;var s={};for(var l in t)hasOwnProperty.call(t,l)&&(s[l]=t[l]);s.originalType=e,s[u]="string"==typeof e?e:n,o[1]=s;for(var c=2;c<i;c++)o[c]=a[c];return r.createElement.apply(null,o)}return r.createElement.apply(null,a)}g.displayName="MDXCreateElement"},5983:(e,t,a)=>{a.r(t),a.d(t,{assets:()=>p,contentTitle:()=>l,default:()=>m,frontMatter:()=>s,metadata:()=>c,toc:()=>u});var r=a(58168),n=a(98587),i=(a(96540),a(15680)),o=["components"],s={id:"javascript",title:"JavaScript programming guide",sidebar_label:"JavaScript functionality"},l=void 0,c={unversionedId:"development/javascript",id:"development/javascript",title:"JavaScript programming guide",description:"\x3c!--",source:"@site/docs/latest/development/javascript.md",sourceDirName:"development",slug:"/development/javascript",permalink:"/docs/latest/development/javascript",draft:!1,tags:[],version:"current",frontMatter:{id:"javascript",title:"JavaScript programming guide",sidebar_label:"JavaScript functionality"},sidebar:"docs",previous:{title:"Creating extensions",permalink:"/docs/latest/development/modules"},next:{title:"Build from source",permalink:"/docs/latest/development/build"}},p={},u=[{value:"Examples",id:"examples",level:2},{value:"Security",id:"security",level:2},{value:"Global variables",id:"global-variables",level:2},{value:"Performance",id:"performance",level:2},{value:"JavaScript vs. Native Extensions",id:"javascript-vs-native-extensions",level:2}],d={toc:u},g="wrapper";function m(e){var t=e.components,a=(0,n.A)(e,o);return(0,i.yg)(g,(0,r.A)({},d,a,{components:t,mdxType:"MDXLayout"}),(0,i.yg)("p",null,"This page discusses how to use JavaScript to extend Apache Druid."),(0,i.yg)("h2",{id:"examples"},"Examples"),(0,i.yg)("p",null,"JavaScript can be used to extend Druid in a variety of ways:"),(0,i.yg)("ul",null,(0,i.yg)("li",{parentName:"ul"},(0,i.yg)("a",{parentName:"li",href:"/docs/latest/querying/aggregations#javascript-aggregator"},"Aggregators")),(0,i.yg)("li",{parentName:"ul"},(0,i.yg)("a",{parentName:"li",href:"/docs/latest/querying/dimensionspecs#javascript-extraction-function"},"Extraction functions")),(0,i.yg)("li",{parentName:"ul"},(0,i.yg)("a",{parentName:"li",href:"/docs/latest/querying/filters#javascript-filter"},"Filters")),(0,i.yg)("li",{parentName:"ul"},(0,i.yg)("a",{parentName:"li",href:"/docs/latest/querying/post-aggregations#javascript-post-aggregator"},"Post-aggregators")),(0,i.yg)("li",{parentName:"ul"},(0,i.yg)("a",{parentName:"li",href:"/docs/latest/ingestion/data-formats#javascript-parsespec"},"Input parsers")),(0,i.yg)("li",{parentName:"ul"},(0,i.yg)("a",{parentName:"li",href:"/docs/latest/design/router#javascript"},"Router strategy")),(0,i.yg)("li",{parentName:"ul"},(0,i.yg)("a",{parentName:"li",href:"/docs/latest/configuration/#javascript-worker-select-strategy"},"Worker select strategy"))),(0,i.yg)("p",null,"JavaScript can be injected dynamically at runtime, making it convenient to rapidly prototype new functionality\nwithout needing to write and deploy Druid extensions."),(0,i.yg)("p",null,"Druid uses the Mozilla Rhino engine at optimization level 9 to compile and execute JavaScript."),(0,i.yg)("h2",{id:"security"},"Security"),(0,i.yg)("p",null,"Druid does not execute JavaScript functions in a sandbox, so they have full access to the machine. So JavaScript\nfunctions allow users to execute arbitrary code inside druid process. So, by default, JavaScript is disabled.\nHowever, on dev/staging environments or secured production environments you can enable those by setting\nthe ",(0,i.yg)("a",{parentName:"p",href:"/docs/latest/configuration/#javascript"},"configuration property"),"\n",(0,i.yg)("inlineCode",{parentName:"p"},"druid.javascript.enabled = true"),"."),(0,i.yg)("h2",{id:"global-variables"},"Global variables"),(0,i.yg)("p",null,"Avoid using global variables. Druid may share the global scope between multiple threads, which can lead to\nunpredictable results if global variables are used."),(0,i.yg)("h2",{id:"performance"},"Performance"),(0,i.yg)("p",null,"Simple JavaScript functions typically have a slight performance penalty to native speed. More complex JavaScript\nfunctions can have steeper performance penalties. Druid compiles JavaScript functions once on each data process per query."),(0,i.yg)("p",null,"You may need to pay special attention to garbage collection when making heavy use of JavaScript functions, especially\ngarbage collection of the compiled classes themselves. Be sure to use a garbage collector configuration that supports\ntimely collection of unused classes (this is generally easier on JDK8 with the Metaspace than it is on JDK7)."),(0,i.yg)("h2",{id:"javascript-vs-native-extensions"},"JavaScript vs. Native Extensions"),(0,i.yg)("p",null,"Generally we recommend using JavaScript when security is not an issue, and when speed of development is more important\nthan performance or memory use. If security is an issue, or if performance and memory use are of the utmost importance,\nwe recommend developing a native Druid extension."),(0,i.yg)("p",null,"In addition, native Druid extensions are more flexible than JavaScript functions. There are some kinds of extensions\n(like sketches) that must be written as native Druid extensions due to their need for custom data formats."))}m.isMDXComponent=!0}}]);