blob: 97fa530d06a7bd29cc24e2e8545d71b283aada47 [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 | Cardinality/HyperUnique aggregators</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.16.1-incubating/">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="cardinality-hyperunique-aggregators">Cardinality/HyperUnique aggregators</h1>
<h2 id="cardinality-aggregator">Cardinality aggregator</h2>
<p>Computes the cardinality of a set of Apache Druid (incubating) dimensions, using HyperLogLog to estimate the cardinality. Please note that this
aggregator will be much slower than indexing a column with the hyperUnique aggregator. This aggregator also runs over a dimension column, which
means the string dimension cannot be removed from the dataset to improve rollup. In general, we strongly recommend using the hyperUnique aggregator
instead of the cardinality aggregator if you do not care about the individual values of a dimension.</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;cardinality&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;fields&quot;</span><span class="p">:</span> <span class="p">[</span> <span class="err">&lt;dimension</span><span class="mi">1</span><span class="err">&gt;</span><span class="p">,</span> <span class="err">&lt;dimension</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;byRow&quot;</span><span class="p">:</span> <span class="err">&lt;</span><span class="kc">false</span> <span class="err">|</span> <span class="kc">true</span><span class="err">&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="nt">&quot;round&quot;</span><span class="p">:</span> <span class="err">&lt;</span><span class="kc">false</span> <span class="err">|</span> <span class="kc">true</span><span class="err">&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>
<p>Each individual element of the &quot;fields&quot; list can be a String or <a href="../querying/dimensionspecs.html">DimensionSpec</a>. A String dimension in the fields list is equivalent to a DefaultDimensionSpec (no transformations).</p>
<p>The HyperLogLog algorithm generates decimal estimates with some error. &quot;round&quot; can be set to true to round off estimated
values to whole numbers. Note that even with rounding, the cardinality is still an estimate. The &quot;round&quot; field only
affects query-time behavior, and is ignored at ingestion-time.</p>
<h3 id="cardinality-by-value">Cardinality by value</h3>
<p>When setting <code>byRow</code> to <code>false</code> (the default) it computes the cardinality of the set composed of the union of all dimension values for all the given dimensions.</p>
<ul>
<li>For a single dimension, this is equivalent to</li>
</ul>
<div class="highlight"><pre><code class="language-sql" data-lang="sql"><span></span><span class="k">SELECT</span> <span class="k">COUNT</span><span class="p">(</span><span class="k">DISTINCT</span><span class="p">(</span><span class="n">dimension</span><span class="p">))</span> <span class="k">FROM</span> <span class="o">&lt;</span><span class="n">datasource</span><span class="o">&gt;</span>
</code></pre></div>
<ul>
<li>For multiple dimensions, this is equivalent to something akin to</li>
</ul>
<div class="highlight"><pre><code class="language-sql" data-lang="sql"><span></span><span class="k">SELECT</span> <span class="k">COUNT</span><span class="p">(</span><span class="k">DISTINCT</span><span class="p">(</span><span class="n">value</span><span class="p">))</span> <span class="k">FROM</span> <span class="p">(</span>
<span class="k">SELECT</span> <span class="n">dim_1</span> <span class="k">as</span> <span class="n">value</span> <span class="k">FROM</span> <span class="o">&lt;</span><span class="n">datasource</span><span class="o">&gt;</span>
<span class="k">UNION</span>
<span class="k">SELECT</span> <span class="n">dim_2</span> <span class="k">as</span> <span class="n">value</span> <span class="k">FROM</span> <span class="o">&lt;</span><span class="n">datasource</span><span class="o">&gt;</span>
<span class="k">UNION</span>
<span class="k">SELECT</span> <span class="n">dim_3</span> <span class="k">as</span> <span class="n">value</span> <span class="k">FROM</span> <span class="o">&lt;</span><span class="n">datasource</span><span class="o">&gt;</span>
<span class="p">)</span>
</code></pre></div>
<h3 id="cardinality-by-row">Cardinality by row</h3>
<p>When setting <code>byRow</code> to <code>true</code> it computes the cardinality by row, i.e. the cardinality of distinct dimension combinations.
This is equivalent to something akin to</p>
<div class="highlight"><pre><code class="language-sql" data-lang="sql"><span></span><span class="k">SELECT</span> <span class="k">COUNT</span><span class="p">(</span><span class="o">*</span><span class="p">)</span> <span class="k">FROM</span> <span class="p">(</span> <span class="k">SELECT</span> <span class="n">DIM1</span><span class="p">,</span> <span class="n">DIM2</span><span class="p">,</span> <span class="n">DIM3</span> <span class="k">FROM</span> <span class="o">&lt;</span><span class="n">datasource</span><span class="o">&gt;</span> <span class="k">GROUP</span> <span class="k">BY</span> <span class="n">DIM1</span><span class="p">,</span> <span class="n">DIM2</span><span class="p">,</span> <span class="n">DIM3</span> <span class="p">)</span>
</code></pre></div>
<p><strong>Example</strong></p>
<p>Determine the number of distinct countries people are living in or have come from.</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;cardinality&quot;</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;distinct_countries&quot;</span><span class="p">,</span>
<span class="nt">&quot;fields&quot;</span><span class="p">:</span> <span class="p">[</span> <span class="s2">&quot;country_of_origin&quot;</span><span class="p">,</span> <span class="s2">&quot;country_of_residence&quot;</span> <span class="p">]</span>
<span class="p">}</span>
</code></pre></div>
<p>Determine the number of distinct people (i.e. combinations of first and last name).</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;cardinality&quot;</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;distinct_people&quot;</span><span class="p">,</span>
<span class="nt">&quot;fields&quot;</span><span class="p">:</span> <span class="p">[</span> <span class="s2">&quot;first_name&quot;</span><span class="p">,</span> <span class="s2">&quot;last_name&quot;</span> <span class="p">],</span>
<span class="nt">&quot;byRow&quot;</span> <span class="p">:</span> <span class="kc">true</span>
<span class="p">}</span>
</code></pre></div>
<p>Determine the number of distinct starting characters of last names</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;cardinality&quot;</span><span class="p">,</span>
<span class="nt">&quot;name&quot;</span><span class="p">:</span> <span class="s2">&quot;distinct_last_name_first_char&quot;</span><span class="p">,</span>
<span class="nt">&quot;fields&quot;</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span>
<span class="nt">&quot;type&quot;</span> <span class="p">:</span> <span class="s2">&quot;extraction&quot;</span><span class="p">,</span>
<span class="nt">&quot;dimension&quot;</span> <span class="p">:</span> <span class="s2">&quot;last_name&quot;</span><span class="p">,</span>
<span class="nt">&quot;outputName&quot;</span> <span class="p">:</span> <span class="s2">&quot;last_name_first_char&quot;</span><span class="p">,</span>
<span class="nt">&quot;extractionFn&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;substring&quot;</span><span class="p">,</span> <span class="nt">&quot;index&quot;</span> <span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="nt">&quot;length&quot;</span> <span class="p">:</span> <span class="mi">1</span> <span class="p">}</span>
<span class="p">}</span>
<span class="p">],</span>
<span class="nt">&quot;byRow&quot;</span> <span class="p">:</span> <span class="kc">true</span>
<span class="p">}</span>
</code></pre></div>
<h2 id="hyperunique-aggregator">HyperUnique aggregator</h2>
<p>Uses <a href="http://algo.inria.fr/flajolet/Publications/FlFuGaMe07.pdf">HyperLogLog</a> to compute the estimated cardinality of a dimension that has been aggregated as a &quot;hyperUnique&quot; metric at indexing time.</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;hyperUnique&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;isInputHyperUnique&quot;</span> <span class="p">:</span> <span class="kc">false</span><span class="p">,</span>
<span class="nt">&quot;round&quot;</span> <span class="p">:</span> <span class="kc">false</span>
<span class="p">}</span>
</code></pre></div>
<p>&quot;isInputHyperUnique&quot; can be set to true to index pre-computed HLL (Base64 encoded output from druid-hll is expected).
The &quot;isInputHyperUnique&quot; field only affects ingestion-time behavior, and is ignored at query-time.</p>
<p>The HyperLogLog algorithm generates decimal estimates with some error. &quot;round&quot; can be set to true to round off estimated
values to whole numbers. Note that even with rounding, the cardinality is still an estimate. The &quot;round&quot; field only
affects query-time behavior, and is ignored at ingestion-time.</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>&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="Download via Apache" href="https://www.apache.org/dyn/closer.cgi?path=/incubator/druid/0.16.1-incubating/apache-druid-0.16.1-incubating-bin.tar.gz" target="_blank"><span class="fas fa-feather"></span></a>&ensp;·&ensp;
<a title="GitHub" href="https://github.com/apache/incubator-druid" target="_blank"><span class="fab fa-github"></span></a>
</div>
<div class="text-center license">
Copyright © 2019 <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.14.2-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>