Search in sources :

Example 11 with KeyValue

use of org.apache.heron.streamlet.KeyValue in project heron by twitter.

the class GeneralReduceByKeyAndWindowOperatorTest method testReduceByWindowOperator.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testReduceByWindowOperator() {
    GeneralReduceByKeyAndWindowOperator<KeyValue<String, Integer>, String, 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());
    }
}
Also used : KeyValue(org.apache.heron.streamlet.KeyValue) HashMap(java.util.HashMap) KeyedWindow(org.apache.heron.streamlet.KeyedWindow) TupleWindow(org.apache.heron.api.windowing.TupleWindow) Test(org.junit.Test)

Example 12 with KeyValue

use of org.apache.heron.streamlet.KeyValue in project heron by twitter.

the class JoinOperatorTest method testOuterRightJoinOperator.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testOuterRightJoinOperator() {
    JoinOperator<String, KeyValue<String, String>, KeyValue<String, String>, String> joinOperator = getJoinOperator(JoinType.OUTER_RIGHT);
    TupleWindow tupleWindow = getTupleWindow();
    Set<String> expectedResultsK1 = new HashSet<>();
    expectedResultsK1.add("01");
    expectedResultsK1.add("03");
    expectedResultsK1.add("21");
    expectedResultsK1.add("23");
    expectedResultsK1.add("41");
    expectedResultsK1.add("43");
    Set<String> expectedResultsK2 = new HashSet<>();
    expectedResultsK2.add("null8");
    expectedResultsK2.add("null9");
    expectedResultsK2.add("null10");
    expectedResultsK2.add("null11");
    joinOperator.execute(tupleWindow);
    Assert.assertEquals(10, emittedTuples.size());
    for (Object object : emittedTuples) {
        KeyValue<KeyedWindow<String>, String> tuple = (KeyValue<KeyedWindow<String>, String>) object;
        KeyedWindow<String> keyedWindow = tuple.getKey();
        switch(keyedWindow.getKey()) {
            case "key1":
                Assert.assertTrue(expectedResultsK1.contains(tuple.getValue()));
                expectedResultsK1.remove(tuple.getValue());
                break;
            case "key2":
                Assert.assertTrue(expectedResultsK2.contains(tuple.getValue()));
                expectedResultsK2.remove(tuple.getValue());
                break;
            case "key3":
                Assert.assertTrue(expectedResultsK2.contains(tuple.getValue()));
                expectedResultsK2.remove(tuple.getValue());
                break;
            default:
                Assert.fail();
        }
        Assert.assertEquals(12, keyedWindow.getWindow().getCount());
        Assert.assertEquals(startTime, keyedWindow.getWindow().getStartTime());
        Assert.assertEquals(endTime, keyedWindow.getWindow().getEndTime());
    }
    Assert.assertEquals(0, expectedResultsK1.size());
    Assert.assertEquals(0, expectedResultsK2.size());
}
Also used : KeyValue(org.apache.heron.streamlet.KeyValue) KeyedWindow(org.apache.heron.streamlet.KeyedWindow) TupleWindow(org.apache.heron.api.windowing.TupleWindow) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with KeyValue

use of org.apache.heron.streamlet.KeyValue in project heron by twitter.

the class JoinOperatorTest method testInnerJoinOperator.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testInnerJoinOperator() {
    JoinOperator<String, KeyValue<String, String>, KeyValue<String, String>, String> joinOperator = getJoinOperator(JoinType.INNER);
    TupleWindow tupleWindow = getTupleWindow();
    Set<String> expectedResults = new HashSet<>();
    expectedResults.add("01");
    expectedResults.add("03");
    expectedResults.add("21");
    expectedResults.add("23");
    expectedResults.add("41");
    expectedResults.add("43");
    joinOperator.execute(tupleWindow);
    Assert.assertEquals(2 * 3, emittedTuples.size());
    for (Object object : emittedTuples) {
        KeyValue<KeyedWindow<String>, String> tuple = (KeyValue<KeyedWindow<String>, String>) object;
        KeyedWindow<String> keyedWindow = tuple.getKey();
        Assert.assertEquals("key1", keyedWindow.getKey());
        Assert.assertEquals(12, keyedWindow.getWindow().getCount());
        Assert.assertEquals(startTime, keyedWindow.getWindow().getStartTime());
        Assert.assertEquals(endTime, keyedWindow.getWindow().getEndTime());
        Assert.assertTrue(expectedResults.contains(tuple.getValue()));
        expectedResults.remove(tuple.getValue());
    }
    Assert.assertEquals(0, expectedResults.size());
}
Also used : KeyValue(org.apache.heron.streamlet.KeyValue) KeyedWindow(org.apache.heron.streamlet.KeyedWindow) TupleWindow(org.apache.heron.api.windowing.TupleWindow) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with KeyValue

use of org.apache.heron.streamlet.KeyValue in project heron by twitter.

the class JoinOperatorTest method testOuterJoinOperator.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testOuterJoinOperator() {
    JoinOperator<String, KeyValue<String, String>, KeyValue<String, String>, String> joinOperator = getJoinOperator(JoinType.OUTER);
    TupleWindow tupleWindow = getTupleWindow();
    Set<String> expectedResultsK1 = new HashSet<>();
    expectedResultsK1.add("01");
    expectedResultsK1.add("03");
    expectedResultsK1.add("21");
    expectedResultsK1.add("23");
    expectedResultsK1.add("41");
    expectedResultsK1.add("43");
    Set<String> expectedResultsK2 = new HashSet<>();
    expectedResultsK2.add("5null");
    expectedResultsK2.add("6null");
    expectedResultsK2.add("7null");
    Set<String> expectedResultsK3 = new HashSet<>();
    expectedResultsK3.add("null8");
    expectedResultsK3.add("null9");
    expectedResultsK3.add("null10");
    expectedResultsK3.add("null11");
    joinOperator.execute(tupleWindow);
    Assert.assertEquals(13, emittedTuples.size());
    for (Object object : emittedTuples) {
        KeyValue<KeyedWindow<String>, String> tuple = (KeyValue<KeyedWindow<String>, String>) object;
        KeyedWindow<String> keyedWindow = tuple.getKey();
        switch(keyedWindow.getKey()) {
            case "key1":
                Assert.assertTrue(expectedResultsK1.contains(tuple.getValue()));
                expectedResultsK1.remove(tuple.getValue());
                break;
            case "key2":
                Assert.assertTrue(expectedResultsK2.contains(tuple.getValue()));
                expectedResultsK2.remove(tuple.getValue());
                break;
            case "key3":
                Assert.assertTrue(expectedResultsK3.contains(tuple.getValue()));
                expectedResultsK3.remove(tuple.getValue());
                break;
            default:
                Assert.fail();
        }
        Assert.assertEquals(12, keyedWindow.getWindow().getCount());
        Assert.assertEquals(startTime, keyedWindow.getWindow().getStartTime());
        Assert.assertEquals(endTime, keyedWindow.getWindow().getEndTime());
    }
    Assert.assertEquals(0, expectedResultsK1.size());
    Assert.assertEquals(0, expectedResultsK3.size());
}
Also used : KeyValue(org.apache.heron.streamlet.KeyValue) KeyedWindow(org.apache.heron.streamlet.KeyedWindow) TupleWindow(org.apache.heron.api.windowing.TupleWindow) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with KeyValue

use of org.apache.heron.streamlet.KeyValue in project heron by twitter.

the class JoinOperatorTest method getJoinOperator.

@SuppressWarnings({ "rawtypes", "unchecked" })
private JoinOperator<String, KeyValue<String, String>, KeyValue<String, String>, String> getJoinOperator(JoinType type) {
    SerializableFunction<KeyValue<String, String>, String> f = x -> x == null ? "null" : x.getKey();
    JoinOperator<String, KeyValue<String, String>, KeyValue<String, String>, String> joinOperator = new JoinOperator(type, "leftComponent", "rightComponent", f, f, (SerializableBiFunction<KeyValue<String, String>, KeyValue<String, String>, String>) (o, o2) -> (o == null ? "null" : o.getValue()) + (o2 == null ? "null" : o2.getValue()));
    joinOperator.prepare(new Config(), PowerMockito.mock(TopologyContext.class), new OutputCollector(new IOutputCollector() {

        @Override
        public void reportError(Throwable error) {
        }

        @Override
        public List<Integer> emit(String streamId, Collection<Tuple> anchors, List<Object> tuple) {
            emittedTuples.addAll(tuple);
            return null;
        }

        @Override
        public void emitDirect(int taskId, String streamId, Collection<Tuple> anchors, List<Object> tuple) {
        }

        @Override
        public void ack(Tuple input) {
        }

        @Override
        public void fail(Tuple input) {
        }
    }));
    return joinOperator;
}
Also used : TopologyContext(org.apache.heron.api.topology.TopologyContext) OutputCollector(org.apache.heron.api.bolt.OutputCollector) TopologyAPI(org.apache.heron.api.generated.TopologyAPI) JoinType(org.apache.heron.streamlet.JoinType) HashMap(java.util.HashMap) HashSet(java.util.HashSet) SerializableBiFunction(org.apache.heron.streamlet.SerializableBiFunction) Map(java.util.Map) Tuple(org.apache.heron.api.tuple.Tuple) IOutputCollector(org.apache.heron.api.bolt.IOutputCollector) LinkedList(java.util.LinkedList) PowerMockito(org.powermock.api.mockito.PowerMockito) Before(org.junit.Before) KeyValue(org.apache.heron.streamlet.KeyValue) KeyedWindow(org.apache.heron.streamlet.KeyedWindow) Collection(java.util.Collection) Set(java.util.Set) TopologyContextImpl(org.apache.heron.common.utils.topology.TopologyContextImpl) Test(org.junit.Test) TupleImpl(org.apache.heron.common.utils.tuple.TupleImpl) TupleWindowImpl(org.apache.heron.api.windowing.TupleWindowImpl) SerializableFunction(org.apache.heron.streamlet.SerializableFunction) Mockito(org.mockito.Mockito) List(java.util.List) Fields(org.apache.heron.api.tuple.Fields) Config(org.apache.heron.api.Config) Values(org.apache.heron.api.tuple.Values) Assert(org.junit.Assert) TupleWindow(org.apache.heron.api.windowing.TupleWindow) Collections(java.util.Collections) TopologyBuilder(org.apache.heron.api.topology.TopologyBuilder) OutputCollector(org.apache.heron.api.bolt.OutputCollector) IOutputCollector(org.apache.heron.api.bolt.IOutputCollector) KeyValue(org.apache.heron.streamlet.KeyValue) Config(org.apache.heron.api.Config) IOutputCollector(org.apache.heron.api.bolt.IOutputCollector) Collection(java.util.Collection) LinkedList(java.util.LinkedList) List(java.util.List) TopologyContext(org.apache.heron.api.topology.TopologyContext) Tuple(org.apache.heron.api.tuple.Tuple)

Aggregations

KeyValue (org.apache.heron.streamlet.KeyValue)16 TupleWindow (org.apache.heron.api.windowing.TupleWindow)11 KeyedWindow (org.apache.heron.streamlet.KeyedWindow)9 Values (org.apache.heron.api.tuple.Values)8 Test (org.junit.Test)7 Tuple (org.apache.heron.api.tuple.Tuple)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 LinkedList (java.util.LinkedList)4 TopologyAPI (org.apache.heron.api.generated.TopologyAPI)3 Fields (org.apache.heron.api.tuple.Fields)3 TupleWindowImpl (org.apache.heron.api.windowing.TupleWindowImpl)3 Collection (java.util.Collection)2 List (java.util.List)2 Config (org.apache.heron.api.Config)2 IOutputCollector (org.apache.heron.api.bolt.IOutputCollector)2 OutputCollector (org.apache.heron.api.bolt.OutputCollector)2 TopologyContext (org.apache.heron.api.topology.TopologyContext)2 Window (org.apache.heron.streamlet.Window)2 DecimalFormat (java.text.DecimalFormat)1