blob: edcd0cdb5f14333954e35fb7613c21b59b8125c3 [file] [log] [blame]
<!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 | Aggregations</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.19.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="aggregations">Aggregations</h1>
<p>Aggregations can be provided at ingestion time as part of the ingestion spec as a way of summarizing data before it enters Apache Druid (incubating).
Aggregations can also be specified as part of many queries at query time.</p>
<p>Available aggregations are:</p>
<h3 id="count-aggregator">Count aggregator</h3>
<p><code>count</code> computes the count of Druid rows that match the filters.</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span> <span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;count&quot;</span><span class="p">,</span> <span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span> <span class="p">}</span>
</code></pre></div>
<p>Please note the count aggregator counts the number of Druid rows, which does not always reflect the number of raw events ingested.
This is because Druid can be configured to roll up data at ingestion time. To
count the number of ingested rows of data, include a count aggregator at ingestion time, and a longSum aggregator at
query time.</p>
<h3 id="sum-aggregators">Sum aggregators</h3>
<h4 id="longsum-aggregator"><code>longSum</code> aggregator</h4>
<p>computes the sum of values as a 64-bit, signed integer</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span> <span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;longSum&quot;</span><span class="p">,</span> <span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span> <span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span> <span class="p">}</span>
</code></pre></div>
<p><code>name</code> – output name for the summed value
<code>fieldName</code> – name of the metric column to sum over</p>
<h4 id="doublesum-aggregator"><code>doubleSum</code> aggregator</h4>
<p>Computes and stores the sum of values as 64-bit floating point value. Similar to <code>longSum</code></p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span> <span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;doubleSum&quot;</span><span class="p">,</span> <span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span> <span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span> <span class="p">}</span>
</code></pre></div>
<h4 id="floatsum-aggregator"><code>floatSum</code> aggregator</h4>
<p>Computes and stores the sum of values as 32-bit floating point value. Similar to <code>longSum</code> and <code>doubleSum</code></p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span> <span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;floatSum&quot;</span><span class="p">,</span> <span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span> <span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span> <span class="p">}</span>
</code></pre></div>
<h3 id="min-max-aggregators">Min / Max aggregators</h3>
<h4 id="doublemin-aggregator"><code>doubleMin</code> aggregator</h4>
<p><code>doubleMin</code> computes the minimum of all metric values and Double.POSITIVE_INFINITY</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span> <span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;doubleMin&quot;</span><span class="p">,</span> <span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span> <span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span> <span class="p">}</span>
</code></pre></div>
<h4 id="doublemax-aggregator"><code>doubleMax</code> aggregator</h4>
<p><code>doubleMax</code> computes the maximum of all metric values and Double.NEGATIVE_INFINITY</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span> <span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;doubleMax&quot;</span><span class="p">,</span> <span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span> <span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span> <span class="p">}</span>
</code></pre></div>
<h4 id="floatmin-aggregator"><code>floatMin</code> aggregator</h4>
<p><code>floatMin</code> computes the minimum of all metric values and Float.POSITIVE_INFINITY</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span> <span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;floatMin&quot;</span><span class="p">,</span> <span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span> <span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span> <span class="p">}</span>
</code></pre></div>
<h4 id="floatmax-aggregator"><code>floatMax</code> aggregator</h4>
<p><code>floatMax</code> computes the maximum of all metric values and Float.NEGATIVE_INFINITY</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span> <span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;floatMax&quot;</span><span class="p">,</span> <span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span> <span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span> <span class="p">}</span>
</code></pre></div>
<h4 id="longmin-aggregator"><code>longMin</code> aggregator</h4>
<p><code>longMin</code> computes the minimum of all metric values and Long.MAX_VALUE</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span> <span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;longMin&quot;</span><span class="p">,</span> <span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span> <span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span> <span class="p">}</span>
</code></pre></div>
<h4 id="longmax-aggregator"><code>longMax</code> aggregator</h4>
<p><code>longMax</code> computes the maximum of all metric values and Long.MIN_VALUE</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span> <span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;longMax&quot;</span><span class="p">,</span> <span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span> <span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span> <span class="p">}</span>
</code></pre></div>
<h3 id="first-last-aggregator">First / Last aggregator</h3>
<p>(Double/Float/Long) First and Last aggregator cannot be used in ingestion spec, and should only be specified as part of queries.</p>
<p>Note that queries with first/last aggregators on a segment created with rollup enabled will return the rolled up value, and not the last value within the raw ingested data.</p>
<h4 id="doublefirst-aggregator"><code>doubleFirst</code> aggregator</h4>
<p><code>doubleFirst</code> computes the metric value with the minimum timestamp or 0 if no row exist</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span>
<span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;doubleFirst&quot;</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span>
<span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span>
<span class="p">}</span>
</code></pre></div>
<h4 id="doublelast-aggregator"><code>doubleLast</code> aggregator</h4>
<p><code>doubleLast</code> computes the metric value with the maximum timestamp or 0 if no row exist</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span>
<span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;doubleLast&quot;</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span>
<span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span>
<span class="p">}</span>
</code></pre></div>
<h4 id="floatfirst-aggregator"><code>floatFirst</code> aggregator</h4>
<p><code>floatFirst</code> computes the metric value with the minimum timestamp or 0 if no row exist</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span>
<span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;floatFirst&quot;</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span>
<span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span>
<span class="p">}</span>
</code></pre></div>
<h4 id="floatlast-aggregator"><code>floatLast</code> aggregator</h4>
<p><code>floatLast</code> computes the metric value with the maximum timestamp or 0 if no row exist</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span>
<span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;floatLast&quot;</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span>
<span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span>
<span class="p">}</span>
</code></pre></div>
<h4 id="longfirst-aggregator"><code>longFirst</code> aggregator</h4>
<p><code>longFirst</code> computes the metric value with the minimum timestamp or 0 if no row exist</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span>
<span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;longFirst&quot;</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span>
<span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span>
<span class="p">}</span>
</code></pre></div>
<h4 id="longlast-aggregator"><code>longLast</code> aggregator</h4>
<p><code>longLast</code> computes the metric value with the maximum timestamp or 0 if no row exist</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span>
<span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;longLast&quot;</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span>
<span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span><span class="p">,</span>
<span class="p">}</span>
</code></pre></div>
<h4 id="stringfirst-aggregator"><code>stringFirst</code> aggregator</h4>
<p><code>stringFirst</code> computes the metric value with the minimum timestamp or <code>null</code> if no row exist</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span>
<span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;stringFirst&quot;</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span>
<span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span><span class="p">,</span>
<span class="nt">&quot;maxStringBytes&quot;</span> <span class="p">:</span> <span class="err">&lt;integer&gt;</span> <span class="err">#</span> <span class="err">(optional</span><span class="p">,</span> <span class="err">defaults</span> <span class="err">to</span> <span class="err">1024),</span>
<span class="nt">&quot;filterNullValues&quot;</span> <span class="p">:</span> <span class="err">&lt;boolean&gt;</span> <span class="err">#</span> <span class="err">(optional</span><span class="p">,</span> <span class="err">defaults</span> <span class="err">to</span> <span class="err">false)</span>
<span class="p">}</span>
</code></pre></div>
<h4 id="stringlast-aggregator"><code>stringLast</code> aggregator</h4>
<p><code>stringLast</code> computes the metric value with the maximum timestamp or <code>null</code> if no row exist</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span>
<span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;stringLast&quot;</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span> <span class="p">:</span> <span class="err">&lt;output_name&gt;</span><span class="p">,</span>
<span class="nt">&quot;fieldName&quot;</span> <span class="p">:</span> <span class="err">&lt;metric_name&gt;</span><span class="p">,</span>
<span class="nt">&quot;maxStringBytes&quot;</span> <span class="p">:</span> <span class="err">&lt;integer&gt;</span> <span class="err">#</span> <span class="err">(optional</span><span class="p">,</span> <span class="err">defaults</span> <span class="err">to</span> <span class="err">1024),</span>
<span class="nt">&quot;filterNullValues&quot;</span> <span class="p">:</span> <span class="err">&lt;boolean&gt;</span> <span class="err">#</span> <span class="err">(optional</span><span class="p">,</span> <span class="err">defaults</span> <span class="err">to</span> <span class="err">false)</span>
<span class="p">}</span>
</code></pre></div>
<h3 id="javascript-aggregator">JavaScript aggregator</h3>
<p>Computes an arbitrary JavaScript function over a set of columns (both metrics and dimensions are allowed). Your
JavaScript functions are expected to return floating-point values.</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span> <span class="nt">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;javascript&quot;</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;&lt;output_name&gt;&quot;</span><span class="p">,</span>
<span class="nt">&quot;fieldNames&quot;</span> <span class="p">:</span> <span class="p">[</span> <span class="err">&lt;column</span><span class="mi">1</span><span class="err">&gt;</span><span class="p">,</span> <span class="err">&lt;column</span><span class="mi">2</span><span class="err">&gt;</span><span class="p">,</span> <span class="err">...</span> <span class="p">],</span>
<span class="nt">&quot;fnAggregate&quot;</span> <span class="p">:</span> <span class="s2">&quot;function(current, column1, column2, ...) {</span>
<span class="s2"> &lt;updates partial aggregate (current) based on the current row values&gt;</span>
<span class="s2"> return &lt;updated partial aggregate&gt;</span>
<span class="s2"> }&quot;</span><span class="p">,</span>
<span class="nt">&quot;fnCombine&quot;</span> <span class="p">:</span> <span class="s2">&quot;function(partialA, partialB) { return &lt;combined partial results&gt;; }&quot;</span><span class="p">,</span>
<span class="nt">&quot;fnReset&quot;</span> <span class="p">:</span> <span class="s2">&quot;function() { return &lt;initial value&gt;; }&quot;</span>
<span class="p">}</span>
</code></pre></div>
<p><strong>Example</strong></p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span>
<span class="nt">&quot;type&quot;</span><span class="p">:</span> <span class="s2">&quot;javascript&quot;</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;sum(log(x)*y) + 10&quot;</span><span class="p">,</span>
<span class="nt">&quot;fieldNames&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;x&quot;</span><span class="p">,</span> <span class="s2">&quot;y&quot;</span><span class="p">],</span>
<span class="nt">&quot;fnAggregate&quot;</span> <span class="p">:</span> <span class="s2">&quot;function(current, a, b) { return current + (Math.log(a) * b); }&quot;</span><span class="p">,</span>
<span class="nt">&quot;fnCombine&quot;</span> <span class="p">:</span> <span class="s2">&quot;function(partialA, partialB) { return partialA + partialB; }&quot;</span><span class="p">,</span>
<span class="nt">&quot;fnReset&quot;</span> <span class="p">:</span> <span class="s2">&quot;function() { return 10; }&quot;</span>
<span class="p">}</span>
</code></pre></div>
<div class="note info">
JavaScript-based functionality is disabled by default. Please refer to the Druid <a href="../development/javascript.html">JavaScript programming guide</a> for guidelines about using Druid's JavaScript functionality, including instructions on how to enable it.
</div>
<p><a name="approx" /></p>
<h2 id="approximate-aggregations">Approximate Aggregations</h2>
<h3 id="count-distinct">Count distinct</h3>
<h4 id="datasketches-theta-sketch">DataSketches Theta Sketch</h4>
<p>The <a href="../development/extensions-core/datasketches-theta.html">DataSketches Theta Sketch</a> extension-provided aggregator gives distinct count estimates with support for set union, intersection, and difference post-aggregators, using Theta sketches from the <a href="http://datasketches.github.io/">datasketches</a> library.</p>
<h4 id="datasketches-hll-sketch">DataSketches HLL Sketch</h4>
<p>The <a href="../development/extensions-core/datasketches-hll.html">DataSketches HLL Sketch</a> extension-provided aggregator gives distinct count estimates using the HyperLogLog algorithm.</p>
<p>Compared to the Theta sketch, the HLL sketch does not support set operations and has slightly slower update and merge speed, but requires significantly less space.</p>
<h4 id="cardinality-hyperunique">Cardinality, hyperUnique</h4>
<div class="note info">
For new use cases, we recommend evaluating <a href="../development/extensions-core/datasketches-theta.html">DataSketches Theta Sketch</a> or <a href="../development/extensions-core/datasketches-hll.html">DataSketches HLL Sketch</a> instead.
The DataSketches aggregators are generally able to offer more flexibility and better accuracy than the classic Druid `cardinality` and `hyperUnique` aggregators.
</div>
<p>The <a href="../querying/hll-old.html">Cardinality and HyperUnique</a> aggregators are older aggregator implementations available by default in Druid that also provide distinct count estimates using the HyperLogLog algorithm. The newer DataSketches Theta and HLL extension-provided aggregators described above have superior accuracy and performance and are recommended instead. </p>
<p>The DataSketches team has published a <a href="https://datasketches.github.io/docs/HLL/HllSketchVsDruidHyperLogLogCollector.html">comparison study</a> between Druid&#39;s original HLL algorithm and the DataSketches HLL algorithm. Based on the demonstrated advantages of the DataSketches implementation, we are recommending using them in preference to Druid&#39;s original HLL-based aggregators.
However, to ensure backwards compatibility, we will continue to support the classic aggregators.</p>
<p>Please note that <code>hyperUnique</code> aggregators are not mutually compatible with Datasketches HLL or Theta sketches.</p>
<h5 id="multi-column-handling">Multi-column handling</h5>
<p>Note the DataSketches Theta and HLL aggregators currently only support single-column inputs. If you were previously using the Cardinality aggregator with multiple-column inputs, equivalent operations using Theta or HLL sketches are described below:</p>
<ul>
<li>Multi-column <code>byValue</code> Cardinality can be replaced with a union of Theta sketches on the individual input columns</li>
<li>Multi-column <code>byRow</code> Cardinality can be replaced with a Theta or HLL sketch on a single <a href="../querying/virtual-columns.html">virtual column</a> that combines the individual input columns.</li>
</ul>
<h3 id="histograms-and-quantiles">Histograms and quantiles</h3>
<h4 id="datasketches-quantiles-sketch">DataSketches Quantiles Sketch</h4>
<p>The <a href="../development/extensions-core/datasketches-quantiles.html">DataSketches Quantiles Sketch</a> extension-provided aggregator provides quantile estimates and histogram approximations using the numeric quantiles DoublesSketch from the <a href="http://datasketches.github.io/">datasketches</a> library.</p>
<p>We recommend this aggregator in general for quantiles/histogram use cases, as it provides formal error bounds and has distribution-independent accuracy.</p>
<h4 id="moments-sketch-experimental">Moments Sketch (Experimental)</h4>
<p>The <a href="../development/extensions-contrib/momentsketch-quantiles.html">Moments Sketch</a> extension-provided aggregator is an experimental aggregator that provides quantile estimates using the <a href="https://github.com/stanford-futuredata/momentsketch">Moments Sketch</a>.</p>
<p>The Moments Sketch aggregator is provided as an experimental option. It is optimized for merging speed and it can have higher aggregation performance compared to the DataSketches quantiles aggregator. However, the accuracy of the Moments Sketch is distribution-dependent, so users will need to empirically verify that the aggregator is suitable for their input data.</p>
<p>As a general guideline for experimentation, the <a href="https://arxiv.org/pdf/1803.01969.pdf">Moments Sketch paper</a> points out that this algorithm works better on inputs with high entropy. In particular, the algorithm is not a good fit when the input data consists of a small number of clustered discrete values.</p>
<h4 id="fixed-buckets-histogram">Fixed Buckets Histogram</h4>
<p>Druid also provides a <a href="../development/extensions-core/approximate-histograms.html#fixed-buckets-histogram">simple histogram implementation</a> that uses a fixed range and fixed number of buckets with support for quantile estimation, backed by an array of bucket count values.</p>
<p>The fixed buckets histogram can perform well when the distribution of the input data allows a small number of buckets to be used.</p>
<p>We do not recommend the fixed buckets histogram for general use, as its usefulness is extremely data dependent. However, it is made available for users that have already identified use cases where a fixed buckets histogram is suitable.</p>
<h4 id="approximate-histogram-deprecated">Approximate Histogram (deprecated)</h4>
<div class="note caution">
The Approximate Histogram aggregator is deprecated.
There are a number of other quantile estimation algorithms that offer better performance, accuracy, and memory footprint.
We recommend using <a href="../development/extensions-core/datasketches-quantiles.html">DataSketches Quantiles</a> instead.
</div>
<p>The <a href="../development/extensions-core/approximate-histograms.html">Approximate Histogram</a> extension-provided aggregator also provides quantile estimates and histogram approximations, based on <a href="http://jmlr.org/papers/volume11/ben-haim10a/ben-haim10a.pdf">http://jmlr.org/papers/volume11/ben-haim10a/ben-haim10a.pdf</a>.</p>
<p>The algorithm used by this deprecated aggregator is highly distribution-dependent and its output is subject to serious distortions when the input does not fit within the algorithm&#39;s limitations.</p>
<p>A <a href="https://datasketches.github.io/docs/Quantiles/DruidApproxHistogramStudy.html">study published by the DataSketches team</a> demonstrates some of the known failure modes of this algorithm:
- The algorithm&#39;s quantile calculations can fail to provide results for a large range of rank values (all ranks less than 0.89 in the example used in the study), returning all zeroes instead.
- The algorithm can completely fail to record spikes in the tail ends of the distribution
- In general, the histogram produced by the algorithm can deviate significantly from the true histogram, with no bounds on the errors.</p>
<p>It is not possible to determine a priori how well this aggregator will behave for a given input stream, nor does the aggregator provide any indication that serious distortions are present in the output.</p>
<p>For these reasons, we have deprecated this aggregator and recommend using the DataSketches Quantiles aggregator instead for new and existing use cases, although we will continue to support Approximate Histogram for backwards compatibility.</p>
<h2 id="miscellaneous-aggregations">Miscellaneous Aggregations</h2>
<h3 id="filtered-aggregator">Filtered Aggregator</h3>
<p>A filtered aggregator wraps any given aggregator, but only aggregates the values for which the given dimension filter matches.</p>
<p>This makes it possible to compute the results of a filtered and an unfiltered aggregation simultaneously, without having to issue multiple queries, and use both results as part of post-aggregations.</p>
<p><em>Note:</em> If only the filtered results are required, consider putting the filter on the query itself, which will be much faster since it does not require scanning all the data.</p>
<div class="highlight"><pre><code class="language-json" data-lang="json"><span></span><span class="p">{</span>
<span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;filtered&quot;</span><span class="p">,</span>
<span class="nt">&quot;filter&quot;</span> <span class="p">:</span> <span class="p">{</span>
<span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;selector&quot;</span><span class="p">,</span>
<span class="nt">&quot;dimension&quot;</span> <span class="p">:</span> <span class="err">&lt;dimension&gt;</span><span class="p">,</span>
<span class="nt">&quot;value&quot;</span> <span class="p">:</span> <span class="err">&lt;dimension</span> <span class="err">value&gt;</span>
<span class="p">}</span>
<span class="s2">&quot;aggregator&quot;</span> <span class="p">:</span> <span class="err">&lt;aggregation&gt;</span>
<span class="p">}</span>
</code></pre></div>
</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>&ensp;·&ensp;
<a href="/use-cases">Use Cases</a>&ensp;·&ensp;
<a href="/druid-powered">Powered by Druid</a>&ensp;·&ensp;
<a href="/docs/latest/">Docs</a>&ensp;·&ensp;
<a href="/community/">Community</a>&ensp;·&ensp;
<a href="/downloads.html">Download</a>&ensp;·&ensp;
<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>&ensp;·&ensp;
<a title="Follow Druid" href="https://twitter.com/druidio" target="_blank"><span class="fab fa-twitter"></span></a>&ensp;·&ensp;
<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>