2018-11-13 12:38:37 -05:00
<!--
~ 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.
-->
2015-05-05 17:07:32 -04:00
---
layout: doc_page
2018-12-12 23:42:12 -05:00
title: "Approximate Histogram aggregator"
2015-05-05 17:07:32 -04:00
---
2016-03-22 16:54:49 -04:00
# Approximate Histogram aggregator
Make sure to [include ](../../operations/including-extensions.html ) `druid-histogram` as an extension.
2015-08-10 15:17:03 -04:00
This aggregator is based on
[http://jmlr.org/papers/volume11/ben-haim10a/ben-haim10a.pdf ](http://jmlr.org/papers/volume11/ben-haim10a/ben-haim10a.pdf )
to compute approximate histograms, with the following modifications:
- some tradeoffs in accuracy were made in the interest of speed (see below)
- the sketch maintains the exact original data as long as the number of
distinct data points is fewer than the resolutions (number of centroids),
increasing accuracy when there are few data points, or when dealing with
2015-11-03 02:19:22 -05:00
discrete data points. You can find some of the details in [this post ](https://metamarkets.com/2013/histograms/ ).
2015-08-10 15:17:03 -04:00
Approximate histogram sketches are still experimental for a reason, and you
should understand the limitations of the current implementation before using
them. The approximation is heavily data-dependent, which makes it difficult to
give good general guidelines, so you should experiment and see what parameters
work well for your data.
Here are a few things to note before using them:
- As indicated in the original paper, there are no formal error bounds on the
approximation. In practice, the approximation gets worse if the distribution
is skewed.
- The algorithm is order-dependent, so results can vary for the same query, due
to variations in the order in which results are merged.
- In general, the algorithm only works well if the data that comes is randomly
distributed (i.e. if data points end up sorted in a column, approximation
will be horrible)
- We traded accuracy for aggregation speed, taking some shortcuts when adding
histograms together, which can lead to pathological cases if your data is
ordered in some way, or if your distribution has long tails. It should be
cheaper to increase the resolution of the sketch to get the accuracy you need.
That being said, those sketches can be useful to get a first order approximation
when averages are not good enough. Assuming most rows in your segment store
fewer data points than the resolution of histogram, you should be able to use
them for monitoring purposes and detect meaningful variations with a few
hundred centroids. To get good accuracy readings on 95th percentiles with
millions of rows of data, you may want to use several thousand centroids,
especially with long tails, since that's where the approximation will be worse.
### Creating approxiate histogram sketches at ingestion time
2015-08-19 16:20:37 -04:00
To use this feature, an "approxHistogram" or "approxHistogramFold" aggregator must be included at
indexing time. The ingestion aggregator can only apply to numeric values. If you use "approxHistogram"
then any input rows missing the value will be considered to have a value of 0, while with "approxHistogramFold"
such rows will be ignored.
To query for results, an "approxHistogramFold" aggregator must be included in the
2015-08-10 15:17:03 -04:00
query.
2015-05-05 17:07:32 -04:00
```json
{
2015-08-19 16:20:37 -04:00
"type" : "approxHistogram or approxHistogramFold (at ingestion time), approxHistogramFold (at query time)",
2015-05-05 17:07:32 -04:00
"name" : < output_name > ,
"fieldName" : < metric_name > ,
"resolution" : < integer > ,
"numBuckets" : < integer > ,
"lowerLimit" : < float > ,
"upperLimit" : < float >
}
```
2015-08-10 15:17:03 -04:00
|Property |Description |Default |
|-------------------------|------------------------------|----------------------------------|
|`resolution` |Number of centroids (data points) to store. The higher the resolution, the more accurate results are, but the slower the computation will be.|50|
|`numBuckets` |Number of output buckets for the resulting histogram. Bucket intervals are dynamic, based on the range of the underlying data. Use a post-aggregator to have finer control over the bucketing scheme|7|
2015-05-05 17:07:32 -04:00
|`lowerLimit`/`upperLimit`|Restrict the approximation to the given range. The values outside this range will be aggregated into two centroids. Counts of values outside this range are still maintained. |-INF/+INF|
### Approximate Histogram post-aggregators
2015-08-10 15:17:03 -04:00
Post-aggregators are used to transform opaque approximate histogram sketches
into bucketed histogram representations, as well as to compute various
distribution metrics such as quantiles, min, and max.
2015-05-05 17:07:32 -04:00
2015-08-10 15:17:03 -04:00
#### Equal buckets post-aggregator
2015-05-05 17:07:32 -04:00
2015-08-10 15:17:03 -04:00
Computes a visual representation of the approximate histogram with a given number of equal-sized bins.
Bucket intervals are based on the range of the underlying data.
2015-05-05 17:07:32 -04:00
```json
2015-08-10 15:17:03 -04:00
{
"type": "equalBuckets",
"name": "< output_name > ",
"fieldName": "< aggregator_name > ",
"numBuckets": < count >
}
2015-05-05 17:07:32 -04:00
```
2015-08-10 15:17:03 -04:00
#### Buckets post-aggregator
2015-05-05 17:07:32 -04:00
Computes a visual representation given an initial breakpoint, offset, and a bucket size.
2015-08-10 15:17:03 -04:00
Bucket size determines the width of the binning interval.
Offset determines the value on which those interval bins align.
2015-05-05 17:07:32 -04:00
```json
2015-08-10 15:17:03 -04:00
{
"type": "buckets",
"name": "< output_name > ",
"fieldName": "< aggregator_name > ",
"bucketSize": < bucket_size > ,
"offset": < offset >
}
2015-05-05 17:07:32 -04:00
```
2015-08-10 15:17:03 -04:00
#### Custom buckets post-aggregator
2015-05-05 17:07:32 -04:00
2015-08-10 15:17:03 -04:00
Computes a visual representation of the approximate histogram with bins laid out according to the given breaks.
2015-05-05 17:07:32 -04:00
```json
{ "type" : "customBuckets", "name" : < output_name > , "fieldName" : < aggregator_name > ,
"breaks" : [ < value > , < value > , ... ] }
```
#### min post-aggregator
Returns the minimum value of the underlying approximate histogram aggregator
```json
{ "type" : "min", "name" : < output_name > , "fieldName" : < aggregator_name > }
```
#### max post-aggregator
Returns the maximum value of the underlying approximate histogram aggregator
```json
{ "type" : "max", "name" : < output_name > , "fieldName" : < aggregator_name > }
```
#### quantile post-aggregator
Computes a single quantile based on the underlying approximate histogram aggregator
```json
{ "type" : "quantile", "name" : < output_name > , "fieldName" : < aggregator_name > ,
"probability" : < quantile > }
```
#### quantiles post-aggregator
Computes an array of quantiles based on the underlying approximate histogram aggregator
```json
{ "type" : "quantiles", "name" : < output_name > , "fieldName" : < aggregator_name > ,
"probabilities" : [ < quantile > , < quantile > , ... ] }
2015-08-10 15:17:03 -04:00
```