| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8" /> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| <meta name="description" content="Apache Druid"> |
| <meta name="keywords" content="druid,kafka,database,analytics,streaming,real-time,real time,apache,open source"> |
| <meta name="author" content="Apache Software Foundation"> |
| |
| <title>Druid | </title> |
| |
| <link rel="alternate" type="application/atom+xml" href="/feed"> |
| <link rel="shortcut icon" href="/img/favicon.png"> |
| |
| <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous"> |
| |
| <link href='//fonts.googleapis.com/css?family=Open+Sans+Condensed:300,700,300italic|Open+Sans:300italic,400italic,600italic,400,300,600,700' rel='stylesheet' type='text/css'> |
| |
| <link rel="stylesheet" href="/css/bootstrap-pure.css?v=1.1"> |
| <link rel="stylesheet" href="/css/base.css?v=1.1"> |
| <link rel="stylesheet" href="/css/header.css?v=1.1"> |
| <link rel="stylesheet" href="/css/footer.css?v=1.1"> |
| <link rel="stylesheet" href="/css/syntax.css?v=1.1"> |
| <link rel="stylesheet" href="/css/docs.css?v=1.1"> |
| |
| <script> |
| (function() { |
| var cx = '000162378814775985090:molvbm0vggm'; |
| var gcse = document.createElement('script'); |
| gcse.type = 'text/javascript'; |
| gcse.async = true; |
| gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + |
| '//cse.google.com/cse.js?cx=' + cx; |
| var s = document.getElementsByTagName('script')[0]; |
| s.parentNode.insertBefore(gcse, s); |
| })(); |
| </script> |
| |
| |
| </head> |
| |
| <body> |
| <!-- Start page_header include --> |
| <script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> |
| |
| <div class="top-navigator"> |
| <div class="container"> |
| <div class="left-cont"> |
| <a class="logo" href="/"><span class="druid-logo"></span></a> |
| </div> |
| <div class="right-cont"> |
| <ul class="links"> |
| <li class=""><a href="/technology">Technology</a></li> |
| <li class=""><a href="/use-cases">Use Cases</a></li> |
| <li class=""><a href="/druid-powered">Powered By</a></li> |
| <li class=""><a href="/docs/latest/design/">Docs</a></li> |
| <li class=""><a href="/community/">Community</a></li> |
| <li class="header-dropdown"> |
| <a>Apache</a> |
| <div class="header-dropdown-menu"> |
| <a href="https://www.apache.org/" target="_blank">Foundation</a> |
| <a href="https://www.apache.org/events/current-event" target="_blank">Events</a> |
| <a href="https://www.apache.org/licenses/" target="_blank">License</a> |
| <a href="https://www.apache.org/foundation/thanks.html" target="_blank">Thanks</a> |
| <a href="https://www.apache.org/security/" target="_blank">Security</a> |
| <a href="https://www.apache.org/foundation/sponsorship.html" target="_blank">Sponsorship</a> |
| </div> |
| </li> |
| <li class=" button-link"><a href="/downloads.html">Download</a></li> |
| </ul> |
| </div> |
| </div> |
| <div class="action-button menu-icon"> |
| <span class="fa fa-bars"></span> MENU |
| </div> |
| <div class="action-button menu-icon-close"> |
| <span class="fa fa-times"></span> MENU |
| </div> |
| </div> |
| |
| <script type="text/javascript"> |
| var $menu = $('.right-cont'); |
| var $menuIcon = $('.menu-icon'); |
| var $menuIconClose = $('.menu-icon-close'); |
| |
| function showMenu() { |
| $menu.fadeIn(100); |
| $menuIcon.fadeOut(100); |
| $menuIconClose.fadeIn(100); |
| } |
| |
| $menuIcon.click(showMenu); |
| |
| function hideMenu() { |
| $menu.fadeOut(100); |
| $menuIconClose.fadeOut(100); |
| $menuIcon.fadeIn(100); |
| } |
| |
| $menuIconClose.click(hideMenu); |
| |
| $(window).resize(function() { |
| if ($(window).width() >= 840) { |
| $menu.fadeIn(100); |
| $menuIcon.fadeOut(100); |
| $menuIconClose.fadeOut(100); |
| } |
| else { |
| $menu.fadeOut(100); |
| $menuIcon.fadeIn(100); |
| $menuIconClose.fadeOut(100); |
| } |
| }); |
| </script> |
| |
| <!-- Stop page_header include --> |
| |
| |
| <div class="container doc-container"> |
| |
| |
| |
| |
| <p> Looking for the <a href="/docs/0.23.0/">latest stable documentation</a>?</p> |
| |
| |
| <div class="row"> |
| <div class="col-md-9 doc-content"> |
| <p> |
| <a class="btn btn-default btn-xs visible-xs-inline-block visible-sm-inline-block" href="#toc">Table of Contents</a> |
| </p> |
| <!-- |
| ~ Licensed to the Apache Software Foundation (ASF) under one |
| ~ or more contributor license agreements. See the NOTICE file |
| ~ distributed with this work for additional information |
| ~ regarding copyright ownership. The ASF licenses this file |
| ~ to you under the Apache License, Version 2.0 (the |
| ~ "License"); you may not use this file except in compliance |
| ~ with the License. You may obtain a copy of the License at |
| ~ |
| ~ http://www.apache.org/licenses/LICENSE-2.0 |
| ~ |
| ~ Unless required by applicable law or agreed to in writing, |
| ~ software distributed under the License is distributed on an |
| ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| ~ KIND, either express or implied. See the License for the |
| ~ specific language governing permissions and limitations |
| ~ under the License. |
| --> |
| |
| <h1 id="moving-average-queries">Moving Average Queries</h1> |
| |
| <h2 id="overview">Overview</h2> |
| |
| <p><strong>Moving Average Query</strong> is an extension which provides support for <a href="https://en.wikipedia.org/wiki/Moving_average">Moving Average</a> and other Aggregate <a href="https://en.wikibooks.org/wiki/Structured_Query_Language/Window_functions">Window Functions</a> in Druid queries.</p> |
| |
| <p>These Aggregate Window Functions consume standard Druid Aggregators and outputs additional windowed aggregates called <a href="#averagers">Averagers</a>.</p> |
| |
| <h4 id="high-level-algorithm">High level algorithm</h4> |
| |
| <p>Moving Average encapsulates the <a href="../../querying/groupbyquery.html">groupBy query</a> (Or <a href="../../querying/timeseriesquery.html">timeseries</a> in case of no dimensions) in order to rely on the maturity of these query types.</p> |
| |
| <p>It runs the query in two main phases: |
| 1. Runs an inner <a href="../../querying/groupbyquery.html">groupBy</a> or <a href="../../querying/timeseriesquery.html">timeseries</a> query to compute Aggregators (i.e. daily count of events). |
| 2. Passes over aggregated results in Broker, in order to compute Averagers (i.e. moving 7 day average of the daily count).</p> |
| |
| <h4 id="main-enhancements-provided-by-this-extension">Main enhancements provided by this extension:</h4> |
| |
| <ol> |
| <li>Functionality: Extending druid query functionality (i.e. initial introduction of Window Functions).</li> |
| <li>Performance: Improving performance of such moving aggregations by eliminating multiple segment scans.</li> |
| </ol> |
| |
| <h4 id="further-reading">Further reading</h4> |
| |
| <p><a href="https://en.wikipedia.org/wiki/Moving_average">Moving Average</a></p> |
| |
| <p><a href="https://en.wikibooks.org/wiki/Structured_Query_Language/Window_functions">Window Functions</a></p> |
| |
| <p><a href="https://cloud.google.com/bigquery/docs/reference/standard-sql/analytic-function-concepts">Analytic Functions</a></p> |
| |
| <h2 id="operations">Operations</h2> |
| |
| <p>To use this extension, make sure to <a href="../../operations/including-extensions.html">load</a> <code>druid-moving-average-query</code> only to the Broker.</p> |
| |
| <h2 id="configuration">Configuration</h2> |
| |
| <p>There are currently no configuration properties specific to Moving Average.</p> |
| |
| <h2 id="limitations">Limitations</h2> |
| |
| <ul> |
| <li>movingAverage is missing support for the following groupBy properties: <code>subtotalsSpec</code>, <code>virtualColumns</code>.</li> |
| <li>movingAverage is missing support for the following timeseries properties: <code>descending</code>.</li> |
| <li>movingAverage is missing support for <a href="https://github.com/apache/incubator-druid/issues/4349">SQL-compatible null handling</a> (So setting druid.generic.useDefaultValueForNull in configuration will give an error). </li> |
| </ul> |
| |
| <h2 id="query-spec">Query spec:</h2> |
| |
| <ul> |
| <li>Most properties in the query spec derived from <a href="../../querying/groupbyquery.html">groupBy query</a> / <a href="../../querying/timeseriesquery.html">timeseries</a>, see documentation for these query types.</li> |
| </ul> |
| |
| <table><thead> |
| <tr> |
| <th>property</th> |
| <th>description</th> |
| <th>required?</th> |
| </tr> |
| </thead><tbody> |
| <tr> |
| <td>queryType</td> |
| <td>This String should always be "movingAverage"; this is the first thing Druid looks at to figure out how to interpret the query.</td> |
| <td>yes</td> |
| </tr> |
| <tr> |
| <td>dataSource</td> |
| <td>A String or Object defining the data source to query, very similar to a table in a relational database. See <a href="../../querying/datasource.html">DataSource</a> for more information.</td> |
| <td>yes</td> |
| </tr> |
| <tr> |
| <td>dimensions</td> |
| <td>A JSON list of <a href="../../querying/dimensionspecs.html">DimensionSpec</a> (Notice that property is optional)</td> |
| <td>no</td> |
| </tr> |
| <tr> |
| <td>limitSpec</td> |
| <td>See <a href="../../querying/limitspec.html">LimitSpec</a></td> |
| <td>no</td> |
| </tr> |
| <tr> |
| <td>having</td> |
| <td>See <a href="../../querying/having.html">Having</a></td> |
| <td>no</td> |
| </tr> |
| <tr> |
| <td>granularity</td> |
| <td>A period granilarity; See <a href="../../querying/granularities.html#period-granularities">Period Granularities</a></td> |
| <td>yes</td> |
| </tr> |
| <tr> |
| <td>filter</td> |
| <td>See <a href="../../querying/filters.html">Filters</a></td> |
| <td>no</td> |
| </tr> |
| <tr> |
| <td>aggregations</td> |
| <td>Aggregations forms the input to Averagers; See <a href="../../querying/aggregations.html">Aggregations</a></td> |
| <td>yes</td> |
| </tr> |
| <tr> |
| <td>postAggregations</td> |
| <td>Supports only aggregations as input; See <a href="../../querying/post-aggregations.html">Post Aggregations</a></td> |
| <td>no</td> |
| </tr> |
| <tr> |
| <td>intervals</td> |
| <td>A JSON Object representing ISO-8601 Intervals. This defines the time ranges to run the query over.</td> |
| <td>yes</td> |
| </tr> |
| <tr> |
| <td>context</td> |
| <td>An additional JSON Object which can be used to specify certain flags.</td> |
| <td>no</td> |
| </tr> |
| <tr> |
| <td>averagers</td> |
| <td>Defines the moving average function; See <a href="#averagers">Averagers</a></td> |
| <td>yes</td> |
| </tr> |
| <tr> |
| <td>postAveragers</td> |
| <td>Support input of both averagers and aggregations; Syntax is identical to postAggregations (See <a href="../../querying/post-aggregations.html">Post Aggregations</a>)</td> |
| <td>no</td> |
| </tr> |
| </tbody></table> |
| |
| <h2 id="averagers">Averagers</h2> |
| |
| <p>Averagers are used to define the Moving-Average function. Averagers are not limited to an average - they can also provide other types of window functions such as MAX()/MIN().</p> |
| |
| <h3 id="properties">Properties</h3> |
| |
| <p>These are properties which are common to all Averagers:</p> |
| |
| <table><thead> |
| <tr> |
| <th>property</th> |
| <th>description</th> |
| <th>required?</th> |
| </tr> |
| </thead><tbody> |
| <tr> |
| <td>type</td> |
| <td>Averager type; See <a href="#averager-types">Averager types</a></td> |
| <td>yes</td> |
| </tr> |
| <tr> |
| <td>name</td> |
| <td>Averager name</td> |
| <td>yes</td> |
| </tr> |
| <tr> |
| <td>fieldName</td> |
| <td>Input name (An aggregation name)</td> |
| <td>yes</td> |
| </tr> |
| <tr> |
| <td>buckets</td> |
| <td>Number of lookback buckets (time periods), including current one. Must be >0</td> |
| <td>yes</td> |
| </tr> |
| <tr> |
| <td>cycleSize</td> |
| <td>Cycle size; Used to calculate day-of-week option; See <a href="#cycle-size-day-of-week">Cycle size (Day of Week)</a></td> |
| <td>no, defaults to 1</td> |
| </tr> |
| </tbody></table> |
| |
| <h3 id="averager-types">Averager types:</h3> |
| |
| <ul> |
| <li><a href="#standard-averagers">Standard averagers</a>: |
| |
| <ul> |
| <li>doubleMean</li> |
| <li>doubleMeanNoNulls</li> |
| <li>doubleMax</li> |
| <li>doubleMin</li> |
| <li>longMean</li> |
| <li>longMeanNoNulls</li> |
| <li>longMax</li> |
| <li>longMin</li> |
| </ul></li> |
| </ul> |
| |
| <h4 id="standard-averagers">Standard averagers</h4> |
| |
| <p>These averagers offer four functions: |
| * Mean (Average) |
| * MeanNoNulls (Ignores empty buckets). |
| * Max |
| * Min</p> |
| |
| <p><strong>Ignoring nulls</strong>: |
| Using a MeanNoNulls averager is useful when the interval starts at the dataset beginning time. |
| In that case, the first records will ignore missing buckets and average won't be artificially low. |
| However, this also means that empty days in a sparse dataset will also be ignored.</p> |
| |
| <p>Example of usage: |
| <code>json |
| { "type" : "doubleMean", "name" : <output_name>, "fieldName": <input_name> } |
| </code></p> |
| |
| <h3 id="cycle-size-day-of-week">Cycle size (Day of Week)</h3> |
| |
| <p>This optional parameter is used to calculate over a single bucket within each cycle instead of all buckets. |
| A prime example would be weekly buckets, resulting in a Day of Week calculation. (Other examples: Month of year, Hour of day).</p> |
| |
| <p>I.e. when using these parameters: |
| * <em>granularity</em>: period=P1D (daily) |
| * <em>buckets</em>: 28 |
| * <em>cycleSize</em>: 7</p> |
| |
| <p>Within each output record, the averager will compute the result over the following buckets: current (#0), #7, #14, #21. |
| Whereas without specifying cycleSize it would have computed over all 28 buckets.</p> |
| |
| <h2 id="examples">Examples</h2> |
| |
| <p>All examples are based on the Wikipedia dataset provided in the Druid <a href="../../tutorials/index.html">tutorials</a>.</p> |
| |
| <h3 id="basic-example">Basic example</h3> |
| |
| <p>Calculating a 7-buckets moving average for Wikipedia edit deltas.</p> |
| |
| <p>Query syntax: |
| <code>json |
| { |
| "queryType": "movingAverage", |
| "dataSource": "wikipedia", |
| "granularity": { |
| "type": "period", |
| "period": "PT30M" |
| }, |
| "intervals": [ |
| "2015-09-12T00:00:00Z/2015-09-13T00:00:00Z" |
| ], |
| "aggregations": [ |
| { |
| "name": "delta30Min", |
| "fieldName": "delta", |
| "type": "longSum" |
| } |
| ], |
| "averagers": [ |
| { |
| "name": "trailing30MinChanges", |
| "fieldName": "delta30Min", |
| "type": "longMean", |
| "buckets": 7 |
| } |
| ] |
| } |
| </code></p> |
| |
| <p>Result: |
| <code>json |
| [ { |
| "version" : "v1", |
| "timestamp" : "2015-09-12T00:30:00.000Z", |
| "event" : { |
| "delta30Min" : 30490, |
| "trailing30MinChanges" : 4355.714285714285 |
| } |
| }, { |
| "version" : "v1", |
| "timestamp" : "2015-09-12T01:00:00.000Z", |
| "event" : { |
| "delta30Min" : 96526, |
| "trailing30MinChanges" : 18145.14285714286 |
| } |
| }, { |
| ... |
| ... |
| ... |
| }, { |
| "version" : "v1", |
| "timestamp" : "2015-09-12T23:00:00.000Z", |
| "event" : { |
| "delta30Min" : 119100, |
| "trailing30MinChanges" : 198697.2857142857 |
| } |
| }, { |
| "version" : "v1", |
| "timestamp" : "2015-09-12T23:30:00.000Z", |
| "event" : { |
| "delta30Min" : 177882, |
| "trailing30MinChanges" : 193890.0 |
| } |
| } |
| </code></p> |
| |
| <h3 id="post-averager-example">Post averager example</h3> |
| |
| <p>Calculating a 7-buckets moving average for Wikipedia edit deltas, plus a ratio between the current period and the moving average.</p> |
| |
| <p>Query syntax: |
| <code>json |
| { |
| "queryType": "movingAverage", |
| "dataSource": "wikipedia", |
| "granularity": { |
| "type": "period", |
| "period": "PT30M" |
| }, |
| "intervals": [ |
| "2015-09-12T22:00:00Z/2015-09-13T00:00:00Z" |
| ], |
| "aggregations": [ |
| { |
| "name": "delta30Min", |
| "fieldName": "delta", |
| "type": "longSum" |
| } |
| ], |
| "averagers": [ |
| { |
| "name": "trailing30MinChanges", |
| "fieldName": "delta30Min", |
| "type": "longMean", |
| "buckets": 7 |
| } |
| ], |
| "postAveragers" : [ |
| { |
| "name": "ratioTrailing30MinChanges", |
| "type": "arithmetic", |
| "fn": "/", |
| "fields": [ |
| { |
| "type": "fieldAccess", |
| "fieldName": "delta30Min" |
| }, |
| { |
| "type": "fieldAccess", |
| "fieldName": "trailing30MinChanges" |
| } |
| ] |
| } |
| ] |
| } |
| </code></p> |
| |
| <p>Result: |
| <code>json |
| [ { |
| "version" : "v1", |
| "timestamp" : "2015-09-12T22:00:00.000Z", |
| "event" : { |
| "delta30Min" : 144269, |
| "trailing30MinChanges" : 204088.14285714287, |
| "ratioTrailing30MinChanges" : 0.7068955500319539 |
| } |
| }, { |
| "version" : "v1", |
| "timestamp" : "2015-09-12T22:30:00.000Z", |
| "event" : { |
| "delta30Min" : 242860, |
| "trailing30MinChanges" : 214031.57142857142, |
| "ratioTrailing30MinChanges" : 1.134692411867141 |
| } |
| }, { |
| "version" : "v1", |
| "timestamp" : "2015-09-12T23:00:00.000Z", |
| "event" : { |
| "delta30Min" : 119100, |
| "trailing30MinChanges" : 198697.2857142857, |
| "ratioTrailing30MinChanges" : 0.5994042624782422 |
| } |
| }, { |
| "version" : "v1", |
| "timestamp" : "2015-09-12T23:30:00.000Z", |
| "event" : { |
| "delta30Min" : 177882, |
| "trailing30MinChanges" : 193890.0, |
| "ratioTrailing30MinChanges" : 0.9174377224199288 |
| } |
| } ] |
| </code></p> |
| |
| <h3 id="cycle-size-example">Cycle size example</h3> |
| |
| <p>Calculating an average of every first 10-minutes of the last 3 hours:</p> |
| |
| <p>Query syntax: |
| <code>json |
| { |
| "queryType": "movingAverage", |
| "dataSource": "wikipedia", |
| "granularity": { |
| "type": "period", |
| "period": "PT10M" |
| }, |
| "intervals": [ |
| "2015-09-12T00:00:00Z/2015-09-13T00:00:00Z" |
| ], |
| "aggregations": [ |
| { |
| "name": "delta10Min", |
| "fieldName": "delta", |
| "type": "doubleSum" |
| } |
| ], |
| "averagers": [ |
| { |
| "name": "trailing10MinPerHourChanges", |
| "fieldName": "delta10Min", |
| "type": "doubleMeanNoNulls", |
| "buckets": 18, |
| "cycleSize": 6 |
| } |
| ] |
| } |
| </code></p> |
| |
| </div> |
| <div class="col-md-3"> |
| <div class="searchbox"> |
| <gcse:searchbox-only></gcse:searchbox-only> |
| </div> |
| <div id="toc" class="nav toc hidden-print"> |
| </div> |
| </div> |
| </div> |
| </div> |
| |
| <!-- Start page_footer include --> |
| <footer class="druid-footer"> |
| <div class="container"> |
| <div class="text-center"> |
| <p> |
| <a href="/technology">Technology</a> ·  |
| <a href="/use-cases">Use Cases</a> ·  |
| <a href="/druid-powered">Powered by Druid</a> ·  |
| <a href="/docs/latest/">Docs</a> ·  |
| <a href="/community/">Community</a> ·  |
| <a href="/downloads.html">Download</a> ·  |
| <a href="/faq">FAQ</a> |
| </p> |
| </div> |
| <div class="text-center"> |
| <a title="Join the user group" href="https://groups.google.com/forum/#!forum/druid-user" target="_blank"><span class="fa fa-comments"></span></a> ·  |
| <a title="Follow Druid" href="https://twitter.com/druidio" target="_blank"><span class="fab fa-twitter"></span></a> ·  |
| <a title="GitHub" href="https://github.com/apache/druid" target="_blank"><span class="fab fa-github"></span></a> |
| </div> |
| <div class="text-center license"> |
| Copyright © 2020 <a href="https://www.apache.org/" target="_blank">Apache Software Foundation</a>.<br> |
| Except where otherwise noted, licensed under <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">CC BY-SA 4.0</a>.<br> |
| Apache Druid, Druid, and the Druid logo are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries. |
| </div> |
| </div> |
| </footer> |
| |
| <script async src="https://www.googletagmanager.com/gtag/js?id=UA-131010415-1"></script> |
| <script> |
| window.dataLayer = window.dataLayer || []; |
| function gtag(){dataLayer.push(arguments);} |
| gtag('js', new Date()); |
| gtag('config', 'UA-131010415-1'); |
| </script> |
| <script> |
| function trackDownload(type, url) { |
| ga('send', 'event', 'download', type, url); |
| } |
| </script> |
| <script src="//code.jquery.com/jquery.min.js"></script> |
| <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script> |
| <script src="/assets/js/druid.js"></script> |
| <!-- stop page_footer include --> |
| |
| |
| <script> |
| $(function() { |
| $(".toc").load("/docs/0.15.0-incubating/toc.html"); |
| |
| // There is no way to tell when .gsc-input will be async loaded into the page so just try to set a placeholder until it works |
| var tries = 0; |
| var timer = setInterval(function() { |
| tries++; |
| if (tries > 300) clearInterval(timer); |
| var searchInput = $('input.gsc-input'); |
| if (searchInput.length) { |
| searchInput.attr('placeholder', 'Search'); |
| clearInterval(timer); |
| } |
| }, 100); |
| }); |
| </script> |
| </body> |
| </html> |