use of com.twitter.heron.api.windowing.TupleWindow in project incubator-heron by apache.
the class WindowedBoltExecutorTest method testExecuteWithTs.
@Test
public void testExecuteWithTs() throws Exception {
long[] timestamps = { 603, 605, 607, 618, 626, 636 };
for (long ts : timestamps) {
executor.execute(getTuple("s1", new Fields("ts"), new Values(ts)));
}
// Thread.sleep(120);
executor.waterMarkEventGenerator.run();
// System.out.println(testWindowedBolt.tupleWindows);
assertEquals(3, testWindowedBolt.tupleWindows.size());
TupleWindow first = testWindowedBolt.tupleWindows.get(0);
assertArrayEquals(new long[] { 603, 605, 607 }, new long[] { (long) first.get().get(0).getValue(0), (long) first.get().get(1).getValue(0), (long) first.get().get(2).getValue(0) });
TupleWindow second = testWindowedBolt.tupleWindows.get(1);
assertArrayEquals(new long[] { 603, 605, 607, 618 }, new long[] { (long) second.get().get(0).getValue(0), (long) second.get().get(1).getValue(0), (long) second.get().get(2).getValue(0), (long) second.get().get(3).getValue(0) });
TupleWindow third = testWindowedBolt.tupleWindows.get(2);
assertArrayEquals(new long[] { 618, 626 }, new long[] { (long) third.get().get(0).getValue(0), (long) third.get().get(1).getValue(0) });
}
use of com.twitter.heron.api.windowing.TupleWindow in project incubator-heron by apache.
the class GeneralReduceByKeyAndWindowOperatorTest method getTupleWindow.
private TupleWindow getTupleWindow(int nkeys, int count) {
TopologyAPI.StreamId componentStreamId = TopologyAPI.StreamId.newBuilder().setComponentName("sourceComponent").setId("default").build();
List<Tuple> tuples = new LinkedList<>();
for (int i = 0; i < nkeys; i++) {
for (int j = 0; j < count; ++j) {
Tuple tuple = getTuple(componentStreamId, new Fields("a"), new Values(new KeyValue<>(String.valueOf(i), j)));
tuples.add(tuple);
}
}
TupleWindow tupleWindow = new TupleWindowImpl(tuples, new LinkedList<>(), new LinkedList<>(), startTime, endTime);
return tupleWindow;
}
use of com.twitter.heron.api.windowing.TupleWindow in project incubator-heron by apache.
the class GeneralReduceByKeyAndWindowOperatorTest method testReduceByWindowOperator.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testReduceByWindowOperator() {
GeneralReduceByKeyAndWindowOperator<String, KeyValue<String, Integer>, Integer> reduceOperator = getReduceByWindowOperator(12);
TupleWindow tupleWindow = getTupleWindow(3, 5);
HashMap<String, Integer> expectedResults = new HashMap<>();
expectedResults.put("0", 22);
expectedResults.put("1", 22);
expectedResults.put("2", 22);
reduceOperator.execute(tupleWindow);
Assert.assertEquals(3, emittedTuples.size());
for (Object object : emittedTuples) {
KeyValue<KeyedWindow<String>, Integer> tuple = (KeyValue<KeyedWindow<String>, Integer>) object;
KeyedWindow<String> window = tuple.getKey();
String key = window.getKey();
Assert.assertEquals(5, window.getWindow().getCount());
Assert.assertEquals(startTime, window.getWindow().getStartTime());
Assert.assertEquals(endTime, window.getWindow().getEndTime());
Assert.assertEquals(expectedResults.get(key), tuple.getValue());
}
}
use of com.twitter.heron.api.windowing.TupleWindow in project incubator-heron by apache.
the class ReduceByKeyAndWindowOperatorTest method testReduceByWindowOperator.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testReduceByWindowOperator() {
ReduceByKeyAndWindowOperator<String, Integer, String> reduceOperator = getReduceByWindowOperator();
TupleWindow tupleWindow = getTupleWindow(3, 5);
HashMap<String, Integer> expectedResults = new HashMap<>();
expectedResults.put("0", 5);
expectedResults.put("1", 5);
expectedResults.put("2", 5);
reduceOperator.execute(tupleWindow);
Assert.assertEquals(3, emittedTuples.size());
for (Object object : emittedTuples) {
KeyValue<KeyedWindow<String>, Integer> tuple = (KeyValue<KeyedWindow<String>, Integer>) object;
KeyedWindow<String> window = tuple.getKey();
String key = window.getKey();
Assert.assertEquals(5, window.getWindow().getCount());
Assert.assertEquals(startTime, window.getWindow().getStartTime());
Assert.assertEquals(endTime, window.getWindow().getEndTime());
Assert.assertEquals(expectedResults.get(key), tuple.getValue());
}
}
use of com.twitter.heron.api.windowing.TupleWindow in project incubator-heron by apache.
the class ReduceByKeyAndWindowOperator method execute.
@SuppressWarnings("unchecked")
@Override
public void execute(TupleWindow inputWindow) {
Map<K, V> reduceMap = new HashMap<>();
Map<K, Integer> windowCountMap = new HashMap<>();
for (Tuple tuple : inputWindow.get()) {
R tup = (R) tuple.getValue(0);
addMap(reduceMap, windowCountMap, tup);
}
long startWindow;
long endWindow;
if (inputWindow.getStartTimestamp() == null) {
startWindow = 0;
} else {
startWindow = inputWindow.getStartTimestamp();
}
if (inputWindow.getEndTimestamp() == null) {
endWindow = 0;
} else {
endWindow = inputWindow.getEndTimestamp();
}
for (K key : reduceMap.keySet()) {
Window window = new Window(startWindow, endWindow, windowCountMap.get(key));
KeyedWindow<K> keyedWindow = new KeyedWindow<>(key, window);
collector.emit(new Values(new KeyValue<>(keyedWindow, reduceMap.get(key))));
}
}
Aggregations