Search in sources :

Example 1 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 2 with Aggregator

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

the class TimeExtractionTopNAlgorithm method scanAndAggregate.

@Override
protected void scanAndAggregate(TopNParams params, int[] dimValSelector, Map<String, Aggregator[]> aggregatesStore, int numProcessed) {
    if (params.getCardinality() < 0) {
        throw new UnsupportedOperationException("Cannot operate on a dimension with unknown cardinality");
    }
    final Cursor cursor = params.getCursor();
    final DimensionSelector dimSelector = params.getDimSelector();
    while (!cursor.isDone()) {
        final String key = dimSelector.lookupName(dimSelector.getRow().get(0));
        Aggregator[] theAggregators = aggregatesStore.get(key);
        if (theAggregators == null) {
            theAggregators = makeAggregators(cursor, query.getAggregatorSpecs());
            aggregatesStore.put(key, theAggregators);
        }
        for (Aggregator aggregator : theAggregators) {
            aggregator.aggregate();
        }
        cursor.advance();
    }
}
Also used : DimensionSelector(io.druid.segment.DimensionSelector) Aggregator(io.druid.query.aggregation.Aggregator) Cursor(io.druid.segment.Cursor)

Example 3 with Aggregator

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

the class StringTopNColumnSelectorStrategy method dimExtractionScanAndAggregateWithCardinalityKnown.

private void dimExtractionScanAndAggregateWithCardinalityKnown(TopNQuery query, Cursor cursor, DimensionSelector selector, Aggregator[][] rowSelector, Map<String, Aggregator[]> aggregatesStore) {
    while (!cursor.isDone()) {
        final IndexedInts dimValues = selector.getRow();
        for (int i = 0; i < dimValues.size(); ++i) {
            final int dimIndex = dimValues.get(i);
            Aggregator[] theAggregators = rowSelector[dimIndex];
            if (theAggregators == null) {
                final String key = selector.lookupName(dimIndex);
                theAggregators = aggregatesStore.get(key);
                if (theAggregators == null) {
                    theAggregators = BaseTopNAlgorithm.makeAggregators(cursor, query.getAggregatorSpecs());
                    aggregatesStore.put(key, theAggregators);
                }
                rowSelector[dimIndex] = theAggregators;
            }
            for (Aggregator aggregator : theAggregators) {
                aggregator.aggregate();
            }
        }
        cursor.advance();
    }
}
Also used : IndexedInts(io.druid.segment.data.IndexedInts) Aggregator(io.druid.query.aggregation.Aggregator)

Example 4 with Aggregator

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

the class StringTopNColumnSelectorStrategy method dimExtractionScanAndAggregateWithCardinalityUnknown.

private void dimExtractionScanAndAggregateWithCardinalityUnknown(TopNQuery query, Cursor cursor, DimensionSelector selector, Map<String, Aggregator[]> aggregatesStore) {
    while (!cursor.isDone()) {
        final IndexedInts dimValues = selector.getRow();
        for (int i = 0; i < dimValues.size(); ++i) {
            final int dimIndex = dimValues.get(i);
            final String key = selector.lookupName(dimIndex);
            Aggregator[] theAggregators = aggregatesStore.get(key);
            if (theAggregators == null) {
                theAggregators = BaseTopNAlgorithm.makeAggregators(cursor, query.getAggregatorSpecs());
                aggregatesStore.put(key, theAggregators);
            }
            for (Aggregator aggregator : theAggregators) {
                aggregator.aggregate();
            }
        }
        cursor.advance();
    }
}
Also used : IndexedInts(io.druid.segment.data.IndexedInts) Aggregator(io.druid.query.aggregation.Aggregator)

Example 5 with Aggregator

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

the class BaseTopNAlgorithm method makeAggregators.

public static Aggregator[] makeAggregators(Cursor cursor, List<AggregatorFactory> aggregatorSpecs) {
    Aggregator[] aggregators = new Aggregator[aggregatorSpecs.size()];
    int aggregatorIndex = 0;
    for (AggregatorFactory spec : aggregatorSpecs) {
        aggregators[aggregatorIndex] = spec.factorize(cursor);
        ++aggregatorIndex;
    }
    return aggregators;
}
Also used : Aggregator(io.druid.query.aggregation.Aggregator) BufferAggregator(io.druid.query.aggregation.BufferAggregator) AggregatorFactory(io.druid.query.aggregation.AggregatorFactory)

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