Search in sources :

Example 11 with HyperLogLogCollector

use of io.druid.hll.HyperLogLogCollector in project druid by druid-io.

the class CardinalityBufferAggregator method aggregate.

@Override
public void aggregate(ByteBuffer buf, int position) {
    // Save position, limit and restore later instead of allocating a new ByteBuffer object
    final int oldPosition = buf.position();
    final int oldLimit = buf.limit();
    buf.limit(position + HyperLogLogCollector.getLatestNumBytesForDenseStorage());
    buf.position(position);
    try {
        final HyperLogLogCollector collector = HyperLogLogCollector.makeCollector(buf);
        if (byRow) {
            CardinalityAggregator.hashRow(selectorPluses, collector);
        } else {
            CardinalityAggregator.hashValues(selectorPluses, collector);
        }
    } finally {
        buf.limit(oldLimit);
        buf.position(oldPosition);
    }
}
Also used : HyperLogLogCollector(io.druid.hll.HyperLogLogCollector)

Example 12 with HyperLogLogCollector

use of io.druid.hll.HyperLogLogCollector in project druid by druid-io.

the class HyperUniquesSerdeForTest method getObjectStrategy.

@Override
public ObjectStrategy getObjectStrategy() {
    return new ObjectStrategy<HyperLogLogCollector>() {

        @Override
        public Class<? extends HyperLogLogCollector> getClazz() {
            return HyperLogLogCollector.class;
        }

        @Override
        public HyperLogLogCollector fromByteBuffer(ByteBuffer buffer, int numBytes) {
            final ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
            readOnlyBuffer.limit(readOnlyBuffer.position() + numBytes);
            return HyperLogLogCollector.makeCollector(readOnlyBuffer);
        }

        @Override
        public byte[] toBytes(HyperLogLogCollector collector) {
            if (collector == null) {
                return new byte[] {};
            }
            ByteBuffer val = collector.toByteBuffer();
            byte[] retVal = new byte[val.remaining()];
            val.asReadOnlyBuffer().get(retVal);
            return retVal;
        }

        @Override
        public int compare(HyperLogLogCollector o1, HyperLogLogCollector o2) {
            return comparator.compare(o1, o2);
        }
    };
}
Also used : HyperLogLogCollector(io.druid.hll.HyperLogLogCollector) ObjectStrategy(io.druid.segment.data.ObjectStrategy) ByteBuffer(java.nio.ByteBuffer)

Example 13 with HyperLogLogCollector

use of io.druid.hll.HyperLogLogCollector in project druid by druid-io.

the class HyperUniquesSerdeForTest method getExtractor.

@Override
public ComplexMetricExtractor getExtractor() {
    return new ComplexMetricExtractor() {

        @Override
        public Class<HyperLogLogCollector> extractedClass() {
            return HyperLogLogCollector.class;
        }

        @Override
        public HyperLogLogCollector extractValue(InputRow inputRow, String metricName) {
            Object rawValue = inputRow.getRaw(metricName);
            if (rawValue instanceof HyperLogLogCollector) {
                return (HyperLogLogCollector) rawValue;
            } else {
                HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector();
                List<String> dimValues = inputRow.getDimension(metricName);
                if (dimValues == null) {
                    return collector;
                }
                for (String dimensionValue : dimValues) {
                    collector.add(hashFn.hashBytes(StringUtils.toUtf8(dimensionValue)).asBytes());
                }
                return collector;
            }
        }
    };
}
Also used : HyperLogLogCollector(io.druid.hll.HyperLogLogCollector) InputRow(io.druid.data.input.InputRow)

Example 14 with HyperLogLogCollector

use of io.druid.hll.HyperLogLogCollector in project druid by druid-io.

the class MetricManipulatorFnsTest method constructorFeeder.

@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> constructorFeeder() {
    final ArrayList<Object[]> constructorArrays = new ArrayList<>();
    final long longVal = 13789;
    LongMinAggregator longMinAggregator = new LongMinAggregator(new TestLongColumnSelector() {

        @Override
        public long get() {
            return longVal;
        }
    });
    LongMinAggregatorFactory longMinAggregatorFactory = new LongMinAggregatorFactory(NAME, FIELD);
    constructorArrays.add(new Object[] { longMinAggregatorFactory, longMinAggregator, longMinAggregator, longMinAggregator, longVal, longVal });
    HyperUniquesAggregatorFactory hyperUniquesAggregatorFactory = new HyperUniquesAggregatorFactory(NAME, FIELD);
    HyperLogLogCollector collector = HyperLogLogCollector.makeLatestCollector();
    collector.add((short) 1, (byte) 5);
    constructorArrays.add(new Object[] { hyperUniquesAggregatorFactory, collector, collector, collector.estimateCardinality(), collector.toByteArray(), collector });
    LongSumAggregatorFactory longSumAggregatorFactory = new LongSumAggregatorFactory(NAME, FIELD);
    LongSumAggregator longSumAggregator = new LongSumAggregator(new TestLongColumnSelector() {

        @Override
        public long get() {
            return longVal;
        }
    });
    constructorArrays.add(new Object[] { longSumAggregatorFactory, longSumAggregator, longSumAggregator, longSumAggregator, longVal, longVal });
    for (Object[] argList : constructorArrays) {
        Assert.assertEquals(String.format("Arglist %s is too short. Expected 6 found %d", Arrays.toString(argList), argList.length), 6, argList.length);
    }
    return constructorArrays;
}
Also used : HyperLogLogCollector(io.druid.hll.HyperLogLogCollector) ArrayList(java.util.ArrayList) TestLongColumnSelector(io.druid.segment.TestLongColumnSelector) HyperUniquesAggregatorFactory(io.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory)

Aggregations

HyperLogLogCollector (io.druid.hll.HyperLogLogCollector)14 Test (org.junit.Test)6 InputRow (io.druid.data.input.InputRow)3 Comparator (java.util.Comparator)3 Random (java.util.Random)3 HyperUniquesAggregatorFactory (io.druid.query.aggregation.hyperloglog.HyperUniquesAggregatorFactory)2 ObjectStrategy (io.druid.segment.data.ObjectStrategy)2 ByteBuffer (java.nio.ByteBuffer)2 Interval (org.joda.time.Interval)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Optional (com.google.common.base.Optional)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 HashFunction (com.google.common.hash.HashFunction)1 Firehose (io.druid.data.input.Firehose)1 Granularity (io.druid.java.util.common.granularity.Granularity)1 FileSmoosher (io.druid.java.util.common.io.smoosh.FileSmoosher)1 SmooshedFileMapper (io.druid.java.util.common.io.smoosh.SmooshedFileMapper)1 SmooshedWriter (io.druid.java.util.common.io.smoosh.SmooshedWriter)1