Search in sources :

Example 6 with Aggregator

use of io.druid.query.aggregation.Aggregator in project druid by druid-io.

the class OnheapIncrementalIndex method doAggregate.

private void doAggregate(AggregatorFactory[] metrics, Aggregator[] aggs, ThreadLocal<InputRow> rowContainer, InputRow row, boolean reportParseExceptions) {
    rowContainer.set(row);
    for (int i = 0; i < aggs.length; i++) {
        final Aggregator agg = aggs[i];
        synchronized (agg) {
            try {
                agg.aggregate();
            } catch (ParseException e) {
                // "aggregate" can throw ParseExceptions if a selector expects something but gets something else.
                if (reportParseExceptions) {
                    throw new ParseException(e, "Encountered parse error for aggregator[%s]", metrics[i].getName());
                } else {
                    log.debug(e, "Encountered parse error, skipping aggregator[%s].", metrics[i].getName());
                }
            }
        }
    }
    rowContainer.set(null);
}
Also used : Aggregator(io.druid.query.aggregation.Aggregator) ParseException(io.druid.java.util.common.parsers.ParseException)

Example 7 with Aggregator

use of io.druid.query.aggregation.Aggregator in project druid by druid-io.

the class OnheapIncrementalIndex method addToFacts.

@Override
protected Integer addToFacts(AggregatorFactory[] metrics, boolean deserializeComplexMetrics, boolean reportParseExceptions, InputRow row, AtomicInteger numEntries, TimeAndDims key, ThreadLocal<InputRow> rowContainer, Supplier<InputRow> rowSupplier) throws IndexSizeExceededException {
    final Integer priorIndex = facts.getPriorIndex(key);
    Aggregator[] aggs;
    if (null != priorIndex) {
        aggs = concurrentGet(priorIndex);
        doAggregate(metrics, aggs, rowContainer, row, reportParseExceptions);
    } else {
        aggs = new Aggregator[metrics.length];
        factorizeAggs(metrics, aggs, rowContainer, row);
        doAggregate(metrics, aggs, rowContainer, row, reportParseExceptions);
        final Integer rowIndex = indexIncrement.getAndIncrement();
        concurrentSet(rowIndex, aggs);
        // Last ditch sanity checks
        if (numEntries.get() >= maxRowCount && facts.getPriorIndex(key) == null) {
            throw new IndexSizeExceededException("Maximum number of rows [%d] reached", maxRowCount);
        }
        final Integer prev = facts.putIfAbsent(key, rowIndex);
        if (null == prev) {
            numEntries.incrementAndGet();
        } else {
            // We lost a race
            aggs = concurrentGet(prev);
            doAggregate(metrics, aggs, rowContainer, row, reportParseExceptions);
            // Free up the misfire
            concurrentRemove(rowIndex);
        // This is expected to occur ~80% of the time in the worst scenarios
        }
    }
    return numEntries.get();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Aggregator(io.druid.query.aggregation.Aggregator)

Example 8 with Aggregator

use of io.druid.query.aggregation.Aggregator in project druid by druid-io.

the class FinalizingFieldAccessPostAggregatorTest method testComputeWithoutFinalizing.

@Test(expected = UnsupportedOperationException.class)
public void testComputeWithoutFinalizing() {
    String aggName = "rows";
    Aggregator agg = new CountAggregator();
    agg.aggregate();
    agg.aggregate();
    agg.aggregate();
    Map<String, Object> metricValues = Maps.newHashMap();
    metricValues.put(aggName, agg.get());
    FinalizingFieldAccessPostAggregator postAgg = new FinalizingFieldAccessPostAggregator("final_rows", aggName);
    Assert.assertEquals(new Long(3L), postAgg.compute(metricValues));
}
Also used : CountAggregator(io.druid.query.aggregation.CountAggregator) CountAggregator(io.druid.query.aggregation.CountAggregator) PostAggregator(io.druid.query.aggregation.PostAggregator) Aggregator(io.druid.query.aggregation.Aggregator) Test(org.junit.Test) GroupByQueryRunnerTest(io.druid.query.groupby.GroupByQueryRunnerTest)

Example 9 with Aggregator

use of io.druid.query.aggregation.Aggregator in project druid by druid-io.

the class StringArrayWritable method toBytes.

public static final byte[] toBytes(final InputRow row, AggregatorFactory[] aggs, boolean reportParseExceptions) {
    try {
        ByteArrayDataOutput out = ByteStreams.newDataOutput();
        //write timestamp
        out.writeLong(row.getTimestampFromEpoch());
        //writing all dimensions
        List<String> dimList = row.getDimensions();
        WritableUtils.writeVInt(out, dimList.size());
        if (dimList != null) {
            for (String dim : dimList) {
                List<String> dimValues = row.getDimension(dim);
                writeString(dim, out);
                writeStringArray(dimValues, out);
            }
        }
        //writing all metrics
        Supplier<InputRow> supplier = new Supplier<InputRow>() {

            @Override
            public InputRow get() {
                return row;
            }
        };
        WritableUtils.writeVInt(out, aggs.length);
        for (AggregatorFactory aggFactory : aggs) {
            String k = aggFactory.getName();
            writeString(k, out);
            Aggregator agg = aggFactory.factorize(IncrementalIndex.makeColumnSelectorFactory(VirtualColumns.EMPTY, aggFactory, supplier, true));
            try {
                agg.aggregate();
            } catch (ParseException e) {
                // "aggregate" can throw ParseExceptions if a selector expects something but gets something else.
                if (reportParseExceptions) {
                    throw new ParseException(e, "Encountered parse error for aggregator[%s]", k);
                }
                log.debug(e, "Encountered parse error, skipping aggregator[%s].", k);
            }
            String t = aggFactory.getTypeName();
            if (t.equals("float")) {
                out.writeFloat(agg.getFloat());
            } else if (t.equals("long")) {
                WritableUtils.writeVLong(out, agg.getLong());
            } else {
                //its a complex metric
                Object val = agg.get();
                ComplexMetricSerde serde = getComplexMetricSerde(t);
                writeBytes(serde.toBytes(val), out);
            }
        }
        return out.toByteArray();
    } catch (IOException ex) {
        throw Throwables.propagate(ex);
    }
}
Also used : ComplexMetricSerde(io.druid.segment.serde.ComplexMetricSerde) ByteArrayDataOutput(com.google.common.io.ByteArrayDataOutput) MapBasedInputRow(io.druid.data.input.MapBasedInputRow) InputRow(io.druid.data.input.InputRow) Aggregator(io.druid.query.aggregation.Aggregator) Supplier(com.google.common.base.Supplier) ParseException(io.druid.java.util.common.parsers.ParseException) IOException(java.io.IOException) AggregatorFactory(io.druid.query.aggregation.AggregatorFactory)

Example 10 with Aggregator

use of io.druid.query.aggregation.Aggregator in project druid by druid-io.

the class IncrementalIndexTest method testgetDimensions.

@Test
public void testgetDimensions() {
    final IncrementalIndex<Aggregator> incrementalIndex = new OnheapIncrementalIndex(new IncrementalIndexSchema.Builder().withQueryGranularity(Granularities.NONE).withMetrics(new AggregatorFactory[] { new CountAggregatorFactory("count") }).withDimensionsSpec(new DimensionsSpec(DimensionsSpec.getDefaultSchemas(Arrays.asList("dim0", "dim1")), null, null)).build(), true, 1000000);
    closer.closeLater(incrementalIndex);
    Assert.assertEquals(Arrays.asList("dim0", "dim1"), incrementalIndex.getDimensionNames());
}
Also used : CountAggregatorFactory(io.druid.query.aggregation.CountAggregatorFactory) OnheapIncrementalIndex(io.druid.segment.incremental.OnheapIncrementalIndex) DimensionsSpec(io.druid.data.input.impl.DimensionsSpec) Aggregator(io.druid.query.aggregation.Aggregator) CountAggregatorFactory(io.druid.query.aggregation.CountAggregatorFactory) DoubleSumAggregatorFactory(io.druid.query.aggregation.DoubleSumAggregatorFactory) AggregatorFactory(io.druid.query.aggregation.AggregatorFactory) FilteredAggregatorFactory(io.druid.query.aggregation.FilteredAggregatorFactory) LongSumAggregatorFactory(io.druid.query.aggregation.LongSumAggregatorFactory) IncrementalIndexSchema(io.druid.segment.incremental.IncrementalIndexSchema) Test(org.junit.Test)

Aggregations

Aggregator (io.druid.query.aggregation.Aggregator)15 Test (org.junit.Test)5 MapBasedInputRow (io.druid.data.input.MapBasedInputRow)4 AggregatorFactory (io.druid.query.aggregation.AggregatorFactory)3 OnheapIncrementalIndex (io.druid.segment.incremental.OnheapIncrementalIndex)3 ParseException (io.druid.java.util.common.parsers.ParseException)2 CountAggregatorFactory (io.druid.query.aggregation.CountAggregatorFactory)2 FilteredAggregatorFactory (io.druid.query.aggregation.FilteredAggregatorFactory)2 Cursor (io.druid.segment.Cursor)2 IndexedInts (io.druid.segment.data.IndexedInts)2 IncrementalIndexSchema (io.druid.segment.incremental.IncrementalIndexSchema)2 Supplier (com.google.common.base.Supplier)1 ByteArrayDataOutput (com.google.common.io.ByteArrayDataOutput)1 InputRow (io.druid.data.input.InputRow)1 DimensionsSpec (io.druid.data.input.impl.DimensionsSpec)1 QueryRunner (io.druid.query.QueryRunner)1 QueryRunnerFactory (io.druid.query.QueryRunnerFactory)1 BufferAggregator (io.druid.query.aggregation.BufferAggregator)1 CountAggregator (io.druid.query.aggregation.CountAggregator)1 DoubleSumAggregatorFactory (io.druid.query.aggregation.DoubleSumAggregatorFactory)1