development/extensions-contrib/moving-average-query

Overview

Moving Average Query is an extension which provides support for Moving Average and other Aggregate Window Functions in Druid queries.

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 timeseries in case of no dimensions) in order to rely on the maturity of these query types.

It runs the query in two main phases:

  1. Runs an inner groupBy or timeseries 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).

Main enhancements provided by this extension:

  1. Functionality: Extending druid query functionality (i.e. initial introduction of Window Functions).
  2. Performance: Improving performance of such moving aggregations by eliminating multiple segment scans.

Further reading

Moving Average

Window Functions

Analytic Functions

Operations

To use this extension, make sure to load druid-moving-average-query only to the Broker.

Configuration

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).

Query spec:

  • Most properties in the query spec derived from groupBy query / timeseries, see documentation for these query types.
propertydescriptionrequired?
queryTypeThis String should always be “movingAverage”; this is the first thing Druid looks at to figure out how to interpret the query.yes
dataSourceA String or Object defining the data source to query, very similar to a table in a relational database. See DataSource for more information.yes
dimensionsA JSON list of DimensionSpec (Notice that property is optional)no
limitSpecSee LimitSpecno
havingSee Havingno
granularityA period granularity; See Period Granularitiesyes
filterSee Filtersno
aggregationsAggregations forms the input to Averagers; See Aggregationsyes
postAggregationsSupports only aggregations as input; See Post Aggregationsno
intervalsA JSON Object representing ISO-8601 Intervals. This defines the time ranges to run the query over.yes
contextAn additional JSON Object which can be used to specify certain flags.no
averagersDefines the moving average function; See Averagersyes
postAveragersSupport input of both averagers and aggregations; Syntax is identical to postAggregations (See Post Aggregations)no

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().

Properties

These are properties which are common to all Averagers:

propertydescriptionrequired?
typeAverager type; See Averager typesyes
nameAverager nameyes
fieldNameInput name (An aggregation name)yes
bucketsNumber of lookback buckets (time periods), including current one. Must be >0yes
cycleSizeCycle size; Used to calculate day-of-week option; See Cycle size (Day of Week)no, defaults to 1

Averager types:

  • Standard averagers:
    • 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:

  1. { "type" : "doubleMean", "name" : <output_name>, "fieldName": <input_name> }

Cycle size (Day of Week)

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

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.

Examples

All examples are based on the Wikipedia dataset provided in the Druid tutorials.

Basic example

Calculating a 7-buckets moving average for Wikipedia edit deltas.

Query syntax:

  1. {
  2. "queryType": "movingAverage",
  3. "dataSource": "wikipedia",
  4. "granularity": {
  5. "type": "period",
  6. "period": "PT30M"
  7. },
  8. "intervals": [
  9. "2015-09-12T00:00:00Z/2015-09-13T00:00:00Z"
  10. ],
  11. "aggregations": [
  12. {
  13. "name": "delta30Min",
  14. "fieldName": "delta",
  15. "type": "longSum"
  16. }
  17. ],
  18. "averagers": [
  19. {
  20. "name": "trailing30MinChanges",
  21. "fieldName": "delta30Min",
  22. "type": "longMean",
  23. "buckets": 7
  24. }
  25. ]
  26. }

Result:

  1. [ {
  2. "version" : "v1",
  3. "timestamp" : "2015-09-12T00:30:00.000Z",
  4. "event" : {
  5. "delta30Min" : 30490,
  6. "trailing30MinChanges" : 4355.714285714285
  7. }
  8. }, {
  9. "version" : "v1",
  10. "timestamp" : "2015-09-12T01:00:00.000Z",
  11. "event" : {
  12. "delta30Min" : 96526,
  13. "trailing30MinChanges" : 18145.14285714286
  14. }
  15. }, {
  16. ...
  17. ...
  18. ...
  19. }, {
  20. "version" : "v1",
  21. "timestamp" : "2015-09-12T23:00:00.000Z",
  22. "event" : {
  23. "delta30Min" : 119100,
  24. "trailing30MinChanges" : 198697.2857142857
  25. }
  26. }, {
  27. "version" : "v1",
  28. "timestamp" : "2015-09-12T23:30:00.000Z",
  29. "event" : {
  30. "delta30Min" : 177882,
  31. "trailing30MinChanges" : 193890.0
  32. }
  33. }

Post averager example

Calculating a 7-buckets moving average for Wikipedia edit deltas, plus a ratio between the current period and the moving average.

Query syntax:

  1. {
  2. "queryType": "movingAverage",
  3. "dataSource": "wikipedia",
  4. "granularity": {
  5. "type": "period",
  6. "period": "PT30M"
  7. },
  8. "intervals": [
  9. "2015-09-12T22:00:00Z/2015-09-13T00:00:00Z"
  10. ],
  11. "aggregations": [
  12. {
  13. "name": "delta30Min",
  14. "fieldName": "delta",
  15. "type": "longSum"
  16. }
  17. ],
  18. "averagers": [
  19. {
  20. "name": "trailing30MinChanges",
  21. "fieldName": "delta30Min",
  22. "type": "longMean",
  23. "buckets": 7
  24. }
  25. ],
  26. "postAveragers" : [
  27. {
  28. "name": "ratioTrailing30MinChanges",
  29. "type": "arithmetic",
  30. "fn": "/",
  31. "fields": [
  32. {
  33. "type": "fieldAccess",
  34. "fieldName": "delta30Min"
  35. },
  36. {
  37. "type": "fieldAccess",
  38. "fieldName": "trailing30MinChanges"
  39. }
  40. ]
  41. }
  42. ]
  43. }

Result:

  1. [ {
  2. "version" : "v1",
  3. "timestamp" : "2015-09-12T22:00:00.000Z",
  4. "event" : {
  5. "delta30Min" : 144269,
  6. "trailing30MinChanges" : 204088.14285714287,
  7. "ratioTrailing30MinChanges" : 0.7068955500319539
  8. }
  9. }, {
  10. "version" : "v1",
  11. "timestamp" : "2015-09-12T22:30:00.000Z",
  12. "event" : {
  13. "delta30Min" : 242860,
  14. "trailing30MinChanges" : 214031.57142857142,
  15. "ratioTrailing30MinChanges" : 1.134692411867141
  16. }
  17. }, {
  18. "version" : "v1",
  19. "timestamp" : "2015-09-12T23:00:00.000Z",
  20. "event" : {
  21. "delta30Min" : 119100,
  22. "trailing30MinChanges" : 198697.2857142857,
  23. "ratioTrailing30MinChanges" : 0.5994042624782422
  24. }
  25. }, {
  26. "version" : "v1",
  27. "timestamp" : "2015-09-12T23:30:00.000Z",
  28. "event" : {
  29. "delta30Min" : 177882,
  30. "trailing30MinChanges" : 193890.0,
  31. "ratioTrailing30MinChanges" : 0.9174377224199288
  32. }
  33. } ]

Cycle size example

Calculating an average of every first 10-minutes of the last 3 hours:

Query syntax:

  1. {
  2. "queryType": "movingAverage",
  3. "dataSource": "wikipedia",
  4. "granularity": {
  5. "type": "period",
  6. "period": "PT10M"
  7. },
  8. "intervals": [
  9. "2015-09-12T00:00:00Z/2015-09-13T00:00:00Z"
  10. ],
  11. "aggregations": [
  12. {
  13. "name": "delta10Min",
  14. "fieldName": "delta",
  15. "type": "doubleSum"
  16. }
  17. ],
  18. "averagers": [
  19. {
  20. "name": "trailing10MinPerHourChanges",
  21. "fieldName": "delta10Min",
  22. "type": "doubleMeanNoNulls",
  23. "buckets": 18,
  24. "cycleSize": 6
  25. }
  26. ]
  27. }