Search in sources :

Example 6 with MutableDouble

use of org.apache.commons.lang.mutable.MutableDouble in project apex-malhar by apache.

the class SumCountMap method endWindow.

/**
 * Emits on all ports that are connected. Data is precomputed during process
 * on input port endWindow just emits it for each key Clears the internal data
 * before return
 */
@Override
public void endWindow() {
    // Should allow users to send each key as a separate tuple to load balance
    // This is an aggregate node, so load balancing would most likely not be
    // needed
    HashMap<K, V> tuples = new HashMap<K, V>();
    HashMap<K, Integer> ctuples = new HashMap<K, Integer>();
    HashMap<K, Double> dtuples = new HashMap<K, Double>();
    HashMap<K, Integer> ituples = new HashMap<K, Integer>();
    HashMap<K, Float> ftuples = new HashMap<K, Float>();
    HashMap<K, Long> ltuples = new HashMap<K, Long>();
    HashMap<K, Short> stuples = new HashMap<K, Short>();
    for (Map.Entry<K, MutableDouble> e : sums.entrySet()) {
        K key = e.getKey();
        MutableDouble val = e.getValue();
        tuples.put(key, getValue(val.doubleValue()));
        dtuples.put(key, val.doubleValue());
        ituples.put(key, val.intValue());
        ftuples.put(key, val.floatValue());
        ltuples.put(key, val.longValue());
        stuples.put(key, val.shortValue());
        // ctuples.put(key, counts.get(e.getKey()).toInteger());
        MutableInt c = counts.get(e.getKey());
        if (c != null) {
            ctuples.put(key, c.toInteger());
        }
    }
    sum.emit(tuples);
    sumDouble.emit(dtuples);
    sumInteger.emit(ituples);
    sumLong.emit(ltuples);
    sumShort.emit(stuples);
    sumFloat.emit(ftuples);
    count.emit(ctuples);
    clearCache();
}
Also used : HashMap(java.util.HashMap) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) UnifierHashMapInteger(org.apache.apex.malhar.lib.util.UnifierHashMapInteger) MutableInt(org.apache.commons.lang.mutable.MutableInt) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with MutableDouble

use of org.apache.commons.lang.mutable.MutableDouble in project apex-malhar by apache.

the class MultiWindowDimensionAggregation method endWindow.

@Override
public void endWindow() {
    int totalWindowsOccupied = cacheOject.size();
    for (Map.Entry<String, Map<String, KeyValPair<MutableDouble, Integer>>> e : outputMap.entrySet()) {
        for (Map.Entry<String, KeyValPair<MutableDouble, Integer>> dimensionValObj : e.getValue().entrySet()) {
            Map<String, DimensionObject<String>> outputData = new HashMap<String, DimensionObject<String>>();
            KeyValPair<MutableDouble, Integer> keyVal = dimensionValObj.getValue();
            if (operationType == AggregateOperation.SUM) {
                outputData.put(e.getKey(), new DimensionObject<String>(keyVal.getKey(), dimensionValObj.getKey()));
            } else if (operationType == AggregateOperation.AVERAGE) {
                if (keyVal.getValue() != 0) {
                    double totalCount = ((double) (totalWindowsOccupied * applicationWindowSize)) / 1000;
                    outputData.put(e.getKey(), new DimensionObject<String>(new MutableDouble(keyVal.getKey().doubleValue() / totalCount), dimensionValObj.getKey()));
                }
            }
            if (!outputData.isEmpty()) {
                output.emit(outputData);
            }
        }
    }
    currentWindow = (currentWindow + 1) % windowSize;
}
Also used : HashMap(java.util.HashMap) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with MutableDouble

use of org.apache.commons.lang.mutable.MutableDouble in project apex-malhar by apache.

the class BasicCountersTest method testBasicCountersAggregator.

@Test
public void testBasicCountersAggregator() throws InstantiationException, IllegalAccessException {
    List<Object> physicalCounters = Lists.newArrayList();
    for (int i = 0; i < 5; i++) {
        BasicCounters<MutableDouble> doubleBasicCounters = new BasicCounters<MutableDouble>(MutableDouble.class);
        MutableDouble counterA = doubleBasicCounters.findCounter(CounterKeys.A);
        counterA.increment();
        physicalCounters.add(doubleBasicCounters);
    }
    BasicCounters.DoubleAggregator<MutableDouble> aggregator = new BasicCounters.DoubleAggregator<MutableDouble>();
    @SuppressWarnings("unchecked") Map<String, NumberAggregate.DoubleAggregate> aggregateMap = (Map<String, NumberAggregate.DoubleAggregate>) aggregator.aggregate(physicalCounters);
    Assert.assertNotNull("null", aggregateMap.get(CounterKeys.A.name()));
    NumberAggregate.DoubleAggregate aggregate = aggregateMap.get(CounterKeys.A.name());
    Assert.assertEquals(aggregate.getSum().doubleValue(), 5.0, 0);
    Assert.assertEquals(aggregate.getMin().doubleValue(), 1.0, 0);
    Assert.assertEquals(aggregate.getMax().doubleValue(), 1.0, 0);
    Assert.assertEquals(aggregate.getAvg().doubleValue(), 1.0, 0);
}
Also used : NumberAggregate(com.datatorrent.common.util.NumberAggregate) MutableDouble(org.apache.commons.lang.mutable.MutableDouble) Map(java.util.Map) Test(org.junit.Test)

Example 9 with MutableDouble

use of org.apache.commons.lang.mutable.MutableDouble in project apex-malhar by apache.

the class BasicCountersTest method testBasicCounters.

@Test
public void testBasicCounters() throws InstantiationException, IllegalAccessException {
    BasicCounters<MutableDouble> doubleBasicCounters = new BasicCounters<MutableDouble>(MutableDouble.class);
    MutableDouble counterA = doubleBasicCounters.findCounter(CounterKeys.A);
    counterA.increment();
    MutableDouble counterAInCounters = doubleBasicCounters.getCounter(CounterKeys.A);
    Assert.assertNotNull("null", doubleBasicCounters.getCounter(CounterKeys.A));
    Assert.assertTrue("equality", counterAInCounters.equals(counterA));
    Assert.assertEquals(counterA.doubleValue(), 1.0, 0);
}
Also used : MutableDouble(org.apache.commons.lang.mutable.MutableDouble) Test(org.junit.Test)

Example 10 with MutableDouble

use of org.apache.commons.lang.mutable.MutableDouble in project apex-malhar by apache.

the class DimensionTimeBucketSumOperator method process.

@Override
public void process(String timeBucket, String key, String field, Number value) {
    String finalKey = timeBucket + "|" + key;
    Map<String, Number> m = dataMap.get(finalKey);
    if (value == null) {
        return;
    }
    if (m == null) {
        m = new HashMap<String, Number>();
        m.put(field, new MutableDouble(value));
        dataMap.put(finalKey, m);
    } else {
        Number n = m.get(field);
        if (n == null) {
            m.put(field, new MutableDouble(value));
        } else {
            ((MutableDouble) n).add(value);
        }
    }
}
Also used : MutableDouble(org.apache.commons.lang.mutable.MutableDouble)

Aggregations

MutableDouble (org.apache.commons.lang.mutable.MutableDouble)25 HashMap (java.util.HashMap)17 Map (java.util.Map)16 CollectorTestSink (org.apache.apex.malhar.lib.testbench.CollectorTestSink)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 DimensionObject (org.apache.apex.malhar.lib.logs.DimensionObject)4 Account (com.google.gerrit.reviewdb.client.Account)3 LinkedHashMap (java.util.LinkedHashMap)3 KeyValPair (org.apache.apex.malhar.lib.util.KeyValPair)3 AggregateOperation (com.datatorrent.apps.logstream.LogstreamUtil.AggregateOperation)2 LogstreamPropertyRegistry (com.datatorrent.apps.logstream.PropertyRegistry.LogstreamPropertyRegistry)2 QueryParseException (com.google.gerrit.server.query.QueryParseException)2 ChangeData (com.google.gerrit.server.query.change.ChangeData)2 List (java.util.List)2 UnifierHashMap (org.apache.apex.malhar.lib.util.UnifierHashMap)2 NumberAggregate (com.datatorrent.common.util.NumberAggregate)1 DTThrowable (com.datatorrent.netlet.util.DTThrowable)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1