Search in sources :

Example 11 with ItemsSketch

use of com.yahoo.sketches.frequencies.ItemsSketch in project sketches-pig by DataSketches.

the class UnionFrequentItemsSketchAlgebraicIntermediateFinal method exec.

@Override
public Tuple exec(final Tuple inputTuple) throws IOException {
    if (isFirstCall_) {
        // this is to see in the log which way was used by Pig
        Logger.getLogger(getClass()).info("algebraic is used");
        isFirstCall_ = false;
    }
    final ItemsSketch<T> sketch = new ItemsSketch<T>(sketchSize_);
    final DataBag bag = (DataBag) inputTuple.get(0);
    if (bag == null) {
        throw new IllegalArgumentException("InputTuple.Field0: Bag may not be null");
    }
    for (Tuple dataTuple : bag) {
        final Object item = dataTuple.get(0);
        if (item instanceof DataBag) {
            // this is from a prior call to the initial function, so there is a nested bag.
            for (final Tuple innerTuple : (DataBag) item) {
                final ItemsSketch<T> incomingSketch = Util.deserializeSketchFromTuple(innerTuple, serDe_);
                sketch.merge(incomingSketch);
            }
        } else if (item instanceof DataByteArray) {
            // This is a sketch from a call to the Intermediate function
            // Merge it with the current sketch.
            final ItemsSketch<T> incomingSketch = Util.deserializeSketchFromTuple(dataTuple, serDe_);
            if (incomingSketch.isEmpty()) {
                continue;
            }
            sketch.merge(incomingSketch);
        } else {
            // we should never get here.
            throw new IllegalArgumentException("InputTuple.Field0: Bag contains unrecognized types: " + item.getClass().getName());
        }
    }
    return Util.serializeSketchToTuple(sketch, serDe_);
}
Also used : DataBag(org.apache.pig.data.DataBag) ItemsSketch(com.yahoo.sketches.frequencies.ItemsSketch) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple)

Aggregations

ItemsSketch (com.yahoo.sketches.frequencies.ItemsSketch)11 DataBag (org.apache.pig.data.DataBag)11 DataByteArray (org.apache.pig.data.DataByteArray)11 Tuple (org.apache.pig.data.Tuple)11 ArrayOfStringsSerDe (com.yahoo.sketches.ArrayOfStringsSerDe)9 Test (org.testng.annotations.Test)9