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();
}
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;
}
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);
}
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);
}
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);
}
}
}
Aggregations