Search in sources :

Example 1 with HllSketch

use of org.apache.datasketches.hll.HllSketch in project druid by druid-io.

the class HllSketchAggregatorFactory method makeAggregateCombiner.

@Override
public AggregateCombiner makeAggregateCombiner() {
    return new ObjectAggregateCombiner<HllSketch>() {

        private final Union union = new Union(lgK);

        @Override
        public void reset(final ColumnValueSelector selector) {
            union.reset();
            fold(selector);
        }

        @Override
        public void fold(final ColumnValueSelector selector) {
            final HllSketch sketch = (HllSketch) selector.getObject();
            union.update(sketch);
        }

        @Nullable
        @Override
        public HllSketch getObject() {
            return union.getResult(tgtHllType);
        }

        @Override
        public Class<HllSketch> classOfObject() {
            return HllSketch.class;
        }
    };
}
Also used : HllSketch(org.apache.datasketches.hll.HllSketch) ObjectAggregateCombiner(org.apache.druid.query.aggregation.ObjectAggregateCombiner) Union(org.apache.datasketches.hll.Union) ColumnValueSelector(org.apache.druid.segment.ColumnValueSelector)

Example 2 with HllSketch

use of org.apache.datasketches.hll.HllSketch in project druid by druid-io.

the class HllSketchBuildBufferAggregatorHelper method relocate.

/**
 * Helper for implementing {@link org.apache.druid.query.aggregation.BufferAggregator#relocate} and
 * {@link org.apache.druid.query.aggregation.VectorAggregator#relocate}.
 */
public void relocate(int oldPosition, int newPosition, ByteBuffer oldBuf, ByteBuffer newBuf) {
    HllSketch sketch = sketchCache.get(oldBuf).get(oldPosition);
    final WritableMemory oldMem = getMemory(oldBuf).writableRegion(oldPosition, size);
    if (sketch.isSameResource(oldMem)) {
        // sketch has not moved
        final WritableMemory newMem = getMemory(newBuf).writableRegion(newPosition, size);
        sketch = HllSketch.writableWrap(newMem);
    }
    putSketchIntoCache(newBuf, newPosition, sketch);
}
Also used : HllSketch(org.apache.datasketches.hll.HllSketch) WritableMemory(org.apache.datasketches.memory.WritableMemory)

Example 3 with HllSketch

use of org.apache.datasketches.hll.HllSketch in project druid by druid-io.

the class GenerateTestData method generateSketches.

private static void generateSketches() throws Exception {
    int lgK = 12;
    String date = "20170101";
    Path rawPath = FileSystems.getDefault().getPath("hll_raw.tsv");
    Path sketchPath = FileSystems.getDefault().getPath("hll_sketches.tsv");
    try (BufferedWriter out1 = Files.newBufferedWriter(rawPath, StandardCharsets.UTF_8)) {
        try (BufferedWriter out2 = Files.newBufferedWriter(sketchPath, StandardCharsets.UTF_8)) {
            Random rand = ThreadLocalRandom.current();
            int key = 0;
            for (int i = 0; i < 100; i++) {
                HllSketch sketch = new HllSketch(lgK);
                String dimension = Integer.toString(rand.nextInt(10) + 1);
                writeRawRecord(out1, date, dimension, key);
                sketch.update(key++);
                writeRawRecord(out1, date, dimension, key);
                sketch.update(key++);
                writeSketchRecord(out2, date, dimension, sketch);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) HllSketch(org.apache.datasketches.hll.HllSketch) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Random(java.util.Random) BufferedWriter(java.io.BufferedWriter)

Example 4 with HllSketch

use of org.apache.datasketches.hll.HllSketch in project druid by druid-io.

the class HllSketchAggregatorFactory method finalizeComputation.

@Nullable
@Override
public Object finalizeComputation(@Nullable final Object object) {
    if (object == null) {
        return null;
    }
    final HllSketch sketch = (HllSketch) object;
    final double estimate = sketch.getEstimate();
    if (round) {
        return Math.round(estimate);
    } else {
        return estimate;
    }
}
Also used : HllSketch(org.apache.datasketches.hll.HllSketch) Nullable(javax.annotation.Nullable)

Example 5 with HllSketch

use of org.apache.datasketches.hll.HllSketch in project druid by druid-io.

the class HllSketchMergeBufferAggregator method aggregate.

@Override
public void aggregate(final ByteBuffer buf, final int position) {
    final HllSketch sketch = selector.getObject();
    if (sketch == null) {
        return;
    }
    final WritableMemory mem = WritableMemory.writableWrap(buf, ByteOrder.LITTLE_ENDIAN).writableRegion(position, helper.getSize());
    final Union union = Union.writableWrap(mem);
    union.update(sketch);
}
Also used : HllSketch(org.apache.datasketches.hll.HllSketch) WritableMemory(org.apache.datasketches.memory.WritableMemory) Union(org.apache.datasketches.hll.Union)

Aggregations

HllSketch (org.apache.datasketches.hll.HllSketch)11 Union (org.apache.datasketches.hll.Union)5 WritableMemory (org.apache.datasketches.memory.WritableMemory)3 Interval (org.joda.time.Interval)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 Nullable (javax.annotation.Nullable)2 Granularity (org.apache.druid.java.util.common.granularity.Granularity)2 DateTime (org.joda.time.DateTime)2 JsonCreator (com.fasterxml.jackson.annotation.JsonCreator)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 SmileMediaTypes (com.fasterxml.jackson.jaxrs.smile.SmileMediaTypes)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions (com.google.common.base.Preconditions)1 Throwables (com.google.common.base.Throwables)1 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1