These Aggregate Window Functions consume standard Druid Aggregators and outputs additional windowed aggregates called Averagers.
High level algorithm
Moving Average encapsulates the groupBy query (Or in case of no dimensions) in order to rely on the maturity of these query types.
It runs the query in two main phases:
- Runs an inner groupBy or query to compute Aggregators (i.e. daily count of events).
- Passes over aggregated results in Broker, in order to compute Averagers (i.e. moving 7 day average of the daily count).
Main enhancements provided by this extension:
- Functionality: Extending druid query functionality (i.e. initial introduction of Window Functions).
- Performance: Improving performance of such moving aggregations by eliminating multiple segment scans.
Further reading
Operations
Use pull-deps tool shipped with Druid to install this on all Druid broker and router nodes.
Enabling
After installation, to enable this extension, just add to druid.extensions.loadList
in broker and routers’ runtime.properties
file and then restart broker and router nodes.
druid.extensions.loadList=["druid-moving-average-query"]
There are currently no configuration properties specific to Moving Average.
Limitations
- movingAverage is missing support for the following groupBy properties:
subtotalsSpec
,virtualColumns
. - movingAverage is missing support for the following timeseries properties:
descending
. - movingAverage is missing support for SQL-compatible null handling (So setting druid.generic.useDefaultValueForNull in configuration will give an error).
- Most properties in the query spec derived from / timeseries, see documentation for these query types.
Averagers
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().
These are properties which are common to all Averagers:
property | description | required? |
---|---|---|
type | Averager type; See Averager types | yes |
name | Averager name | yes |
fieldName | Input name (An aggregation name) | yes |
buckets | Number of lookback buckets (time periods), including current one. Must be >0 | yes |
cycleSize | Cycle size; Used to calculate day-of-week option; See | no, defaults to 1 |
Averager types:
- :
- doubleMean
- doubleMeanNoNulls
- doubleSum
- doubleMax
- doubleMin
- longMean
- longMeanNoNulls
- longSum
- longMax
- longMin
Standard averagers
These averagers offer four functions:
- Mean (Average)
- MeanNoNulls (Ignores empty buckets).
- Sum
- Max
- Min
Ignoring nulls: 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.
Example of usage:
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).
I.e. when using these parameters:
- granularity: period=P1D (daily)
- buckets: 28
- cycleSize: 7
All examples are based on the Wikipedia dataset provided in the Druid .
Basic example
Calculating a 7-buckets moving average for Wikipedia edit deltas.
Query syntax:
"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
}
]
}
Result:
Calculating a 7-buckets moving average for Wikipedia edit deltas, plus a ratio between the current period and the moving average.
Query syntax:
{
"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",
"buckets": 7
}
],
"postAveragers" : [
{
"name": "ratioTrailing30MinChanges",
"type": "arithmetic",
"fn": "/",
"fields": [
{
"type": "fieldAccess",
"fieldName": "delta30Min"
},
"type": "fieldAccess",
"fieldName": "trailing30MinChanges"
}
]
}
]
}
Result:
Cycle size example
Calculating an average of every first 10-minutes of the last 3 hours:
{
"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
}