Search in sources :

Example 6 with UpdatableSketch

use of com.yahoo.sketches.tuple.UpdatableSketch in project sketches-pig by DataSketches.

the class DataToSketchAlgebraicIntermediateFinal 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 Union<S> union = new Union<S>(sketchSize_, summarySetOps_);
    final DataBag bag = (DataBag) inputTuple.get(0);
    if (bag == null) {
        throw new IllegalArgumentException("InputTuple.Field0: Bag may not be null");
    }
    for (final Tuple dataTuple : bag) {
        final Object item = dataTuple.get(0);
        if (item instanceof DataBag) {
            // this is a bag from the Initial function.
            // just insert each item of the tuple into the sketch
            final UpdatableSketch<U, S> sketch = sketchBuilder_.build();
            DataToSketch.updateSketch((DataBag) item, sketch);
            union.update(sketch);
        } else if (item instanceof DataByteArray) {
            // This is a sketch from a prior call to the
            // Intermediate function. merge it with the
            // current sketch.
            final Sketch<S> incomingSketch = Util.deserializeSketchFromTuple(dataTuple, summaryDeserializer_);
            union.update(incomingSketch);
        } else {
            // we should never get here.
            throw new IllegalArgumentException("InputTuple.Field0: Bag contains unrecognized types: " + item.getClass().getName());
        }
    }
    return Util.tupleFactory.newTuple(new DataByteArray(union.getResult().toByteArray()));
}
Also used : DEFAULT_NOMINAL_ENTRIES(com.yahoo.sketches.Util.DEFAULT_NOMINAL_ENTRIES) DataBag(org.apache.pig.data.DataBag) Sketch(com.yahoo.sketches.tuple.Sketch) UpdatableSketch(com.yahoo.sketches.tuple.UpdatableSketch) DataByteArray(org.apache.pig.data.DataByteArray) Union(com.yahoo.sketches.tuple.Union) Tuple(org.apache.pig.data.Tuple)

Example 7 with UpdatableSketch

use of com.yahoo.sketches.tuple.UpdatableSketch in project sketches-pig by DataSketches.

the class UnionDoubleSummarySketchTest method algebraicIntemediateFinalEstimation.

@Test
public void algebraicIntemediateFinalEstimation() throws Exception {
    EvalFunc<Tuple> func = new UnionDoubleSummarySketch.IntermediateFinal("16384");
    DataBag bag = BagFactory.getInstance().newDefaultBag();
    long value = 1;
    // this is to simulate the output from Initial
    {
        UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).setNominalEntries(16384).build();
        for (int i = 0; i < 20000; i++) sketch.update(value++, 1.0);
        DataBag innerBag = PigUtil.tuplesToBag(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
        bag.add(PigUtil.objectsToTuple(innerBag));
    }
    // this is to simulate the output from a prior call of IntermediateFinal
    {
        UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).setNominalEntries(16384).build();
        for (int i = 0; i < 20000; i++) sketch.update(value++, 1.0);
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
    }
    Tuple resultTuple = func.exec(PigUtil.objectsToTuple(bag));
    Assert.assertNotNull(resultTuple);
    Assert.assertEquals(resultTuple.size(), 1);
    DataByteArray bytes = (DataByteArray) resultTuple.get(0);
    Assert.assertTrue(bytes.size() > 0);
    Sketch<DoubleSummary> sketch = Sketches.heapifySketch(Memory.wrap(bytes.get()), new DoubleSummaryDeserializer());
    Assert.assertEquals(sketch.getEstimate(), 40000.0, 40000.0 * 0.01);
    SketchIterator<DoubleSummary> it = sketch.iterator();
    while (it.next()) {
        Assert.assertEquals(it.getSummary().getValue(), 1.0, 0.0);
    }
}
Also used : DataBag(org.apache.pig.data.DataBag) DoubleSummaryFactory(com.yahoo.sketches.tuple.DoubleSummaryFactory) UpdatableSketch(com.yahoo.sketches.tuple.UpdatableSketch) DoubleSummary(com.yahoo.sketches.tuple.DoubleSummary) DoubleSummaryDeserializer(com.yahoo.sketches.tuple.DoubleSummaryDeserializer) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Example 8 with UpdatableSketch

use of com.yahoo.sketches.tuple.UpdatableSketch in project sketches-pig by DataSketches.

the class UnionDoubleSummarySketchTest method algebraicIntemediateFinalSingleCall.

@Test
public void algebraicIntemediateFinalSingleCall() throws Exception {
    EvalFunc<Tuple> func = new UnionDoubleSummarySketch.IntermediateFinal("1024");
    DataBag bag = BagFactory.getInstance().newDefaultBag();
    long value = 1;
    // this is to simulate the output from a prior call of IntermediateFinal
    {
        UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).setNominalEntries(1024).build();
        for (int i = 0; i < 10000; i++) sketch.update(value++, 1.0);
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
    }
    Tuple resultTuple = func.exec(PigUtil.objectsToTuple(bag));
    Assert.assertNotNull(resultTuple);
    Assert.assertEquals(resultTuple.size(), 1);
    DataByteArray bytes = (DataByteArray) resultTuple.get(0);
    Assert.assertTrue(bytes.size() > 0);
    Sketch<DoubleSummary> sketch = Sketches.heapifySketch(Memory.wrap(bytes.get()), new DoubleSummaryDeserializer());
    Assert.assertEquals(sketch.getEstimate(), 10000.0, 10000.0 * 0.02);
    SketchIterator<DoubleSummary> it = sketch.iterator();
    while (it.next()) {
        Assert.assertEquals(it.getSummary().getValue(), 1.0, 0.0);
    }
}
Also used : DataBag(org.apache.pig.data.DataBag) DoubleSummaryFactory(com.yahoo.sketches.tuple.DoubleSummaryFactory) UpdatableSketch(com.yahoo.sketches.tuple.UpdatableSketch) DoubleSummary(com.yahoo.sketches.tuple.DoubleSummary) DoubleSummaryDeserializer(com.yahoo.sketches.tuple.DoubleSummaryDeserializer) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Example 9 with UpdatableSketch

use of com.yahoo.sketches.tuple.UpdatableSketch in project sketches-pig by DataSketches.

the class UnionDoubleSummarySketchTest method accumulator.

@Test
public void accumulator() throws Exception {
    Accumulator<Tuple> func = new UnionDoubleSummarySketch("4096");
    DataBag bag = BagFactory.getInstance().newDefaultBag();
    {
        UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).build();
        sketch.update(1, 1.0);
        sketch.update(2, 1.0);
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
    }
    func.accumulate(PigUtil.objectsToTuple(bag));
    bag = BagFactory.getInstance().newDefaultBag();
    {
        UpdatableSketch<Double, DoubleSummary> sketch = new UpdatableSketchBuilder<Double, DoubleSummary>(new DoubleSummaryFactory()).build();
        sketch.update(1, 1.0);
        sketch.update(2, 1.0);
        bag.add(PigUtil.objectsToTuple(new DataByteArray(sketch.compact().toByteArray())));
    }
    func.accumulate(PigUtil.objectsToTuple(bag));
    Tuple resultTuple = func.getValue();
    Assert.assertNotNull(resultTuple);
    Assert.assertEquals(resultTuple.size(), 1);
    DataByteArray bytes = (DataByteArray) resultTuple.get(0);
    Assert.assertTrue(bytes.size() > 0);
    Sketch<DoubleSummary> sketch = Sketches.heapifySketch(Memory.wrap(bytes.get()), new DoubleSummaryDeserializer());
    Assert.assertEquals(sketch.getEstimate(), 2.0, 0.0);
    SketchIterator<DoubleSummary> it = sketch.iterator();
    while (it.next()) {
        Assert.assertEquals(it.getSummary().getValue(), 2.0, 0.0);
    }
}
Also used : UpdatableSketch(com.yahoo.sketches.tuple.UpdatableSketch) DataBag(org.apache.pig.data.DataBag) DoubleSummary(com.yahoo.sketches.tuple.DoubleSummary) UpdatableSketchBuilder(com.yahoo.sketches.tuple.UpdatableSketchBuilder) DoubleSummaryDeserializer(com.yahoo.sketches.tuple.DoubleSummaryDeserializer) DoubleSummaryFactory(com.yahoo.sketches.tuple.DoubleSummaryFactory) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) Test(org.testng.annotations.Test)

Aggregations

UpdatableSketch (com.yahoo.sketches.tuple.UpdatableSketch)9 DataBag (org.apache.pig.data.DataBag)9 DataByteArray (org.apache.pig.data.DataByteArray)9 Tuple (org.apache.pig.data.Tuple)9 DoubleSummary (com.yahoo.sketches.tuple.DoubleSummary)8 DoubleSummaryDeserializer (com.yahoo.sketches.tuple.DoubleSummaryDeserializer)8 DoubleSummaryFactory (com.yahoo.sketches.tuple.DoubleSummaryFactory)8 Test (org.testng.annotations.Test)8 UpdatableSketchBuilder (com.yahoo.sketches.tuple.UpdatableSketchBuilder)5 DEFAULT_NOMINAL_ENTRIES (com.yahoo.sketches.Util.DEFAULT_NOMINAL_ENTRIES)1 Sketch (com.yahoo.sketches.tuple.Sketch)1 Union (com.yahoo.sketches.tuple.Union)1 Random (java.util.Random)1