Search in sources :

Example 1 with KeyValue

use of com.twitter.heron.streamlet.KeyValue in project incubator-heron by apache.

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(com.twitter.heron.streamlet.KeyValue) KeyedWindow(com.twitter.heron.streamlet.KeyedWindow) TupleWindow(com.twitter.heron.api.windowing.TupleWindow) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with KeyValue

use of com.twitter.heron.streamlet.KeyValue in project incubator-heron by apache.

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 : TopologyBuilder(com.twitter.heron.api.topology.TopologyBuilder) TopologyAPI(com.twitter.heron.api.generated.TopologyAPI) Config(com.twitter.heron.api.Config) HashMap(java.util.HashMap) HashSet(java.util.HashSet) Values(com.twitter.heron.api.tuple.Values) TupleImpl(com.twitter.heron.common.utils.tuple.TupleImpl) TopologyContextImpl(com.twitter.heron.common.utils.topology.TopologyContextImpl) Map(java.util.Map) Fields(com.twitter.heron.api.tuple.Fields) SerializableFunction(com.twitter.heron.streamlet.SerializableFunction) LinkedList(java.util.LinkedList) PowerMockito(org.powermock.api.mockito.PowerMockito) SerializableBiFunction(com.twitter.heron.streamlet.SerializableBiFunction) TupleWindow(com.twitter.heron.api.windowing.TupleWindow) Before(org.junit.Before) JoinType(com.twitter.heron.streamlet.JoinType) Collection(java.util.Collection) KeyedWindow(com.twitter.heron.streamlet.KeyedWindow) Set(java.util.Set) Tuple(com.twitter.heron.api.tuple.Tuple) Test(org.junit.Test) TopologyContext(com.twitter.heron.api.topology.TopologyContext) Mockito(org.mockito.Mockito) IOutputCollector(com.twitter.heron.api.bolt.IOutputCollector) List(java.util.List) OutputCollector(com.twitter.heron.api.bolt.OutputCollector) KeyValue(com.twitter.heron.streamlet.KeyValue) Assert(org.junit.Assert) Collections(java.util.Collections) TupleWindowImpl(com.twitter.heron.api.windowing.TupleWindowImpl) IOutputCollector(com.twitter.heron.api.bolt.IOutputCollector) OutputCollector(com.twitter.heron.api.bolt.OutputCollector) KeyValue(com.twitter.heron.streamlet.KeyValue) Config(com.twitter.heron.api.Config) IOutputCollector(com.twitter.heron.api.bolt.IOutputCollector) Collection(java.util.Collection) LinkedList(java.util.LinkedList) List(java.util.List) TopologyContext(com.twitter.heron.api.topology.TopologyContext) Tuple(com.twitter.heron.api.tuple.Tuple)

Example 3 with KeyValue

use of com.twitter.heron.streamlet.KeyValue in project incubator-heron by apache.

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(com.twitter.heron.streamlet.KeyValue) KeyedWindow(com.twitter.heron.streamlet.KeyedWindow) TupleWindow(com.twitter.heron.api.windowing.TupleWindow) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with KeyValue

use of com.twitter.heron.streamlet.KeyValue in project incubator-heron by apache.

the class JoinOperatorTest method getTupleWindow.

private TupleWindow getTupleWindow() {
    TopologyAPI.StreamId leftComponentStreamId = TopologyAPI.StreamId.newBuilder().setComponentName("leftComponent").setId("s1").build();
    TopologyAPI.StreamId rightComponentStreamId = TopologyAPI.StreamId.newBuilder().setComponentName("rightComponent").setId("s1").build();
    List<Tuple> tuples = new LinkedList<>();
    for (int i = 0; i < 5; i++) {
        Tuple tuple;
        if (i % 2 == 0) {
            tuple = getTuple(leftComponentStreamId, new Fields("a"), new Values(new KeyValue<String, String>("key1", String.valueOf(i))));
        } else {
            tuple = getTuple(rightComponentStreamId, new Fields("a"), new Values(new KeyValue<String, String>("key1", String.valueOf(i))));
        }
        tuples.add(tuple);
    }
    for (int i = 5; i < 8; i++) {
        Tuple tuple = getTuple(leftComponentStreamId, new Fields("a"), new Values(new KeyValue<String, String>("key2", String.valueOf(i))));
        tuples.add(tuple);
    }
    for (int i = 8; i < 12; i++) {
        Tuple tuple = getTuple(rightComponentStreamId, new Fields("a"), new Values(new KeyValue<String, String>("key3", String.valueOf(i))));
        tuples.add(tuple);
    }
    TupleWindow tupleWindow = new TupleWindowImpl(tuples, new LinkedList<>(), new LinkedList<>(), startTime, endTime);
    return tupleWindow;
}
Also used : KeyValue(com.twitter.heron.streamlet.KeyValue) Values(com.twitter.heron.api.tuple.Values) TupleWindow(com.twitter.heron.api.windowing.TupleWindow) LinkedList(java.util.LinkedList) TopologyAPI(com.twitter.heron.api.generated.TopologyAPI) Fields(com.twitter.heron.api.tuple.Fields) TupleWindowImpl(com.twitter.heron.api.windowing.TupleWindowImpl) Tuple(com.twitter.heron.api.tuple.Tuple)

Example 5 with KeyValue

use of com.twitter.heron.streamlet.KeyValue in project incubator-heron by apache.

the class SmartWatchTopology method main.

public static void main(String[] args) throws Exception {
    Builder processingGraphBuilder = Builder.newBuilder();
    processingGraphBuilder.newSource(SmartWatchReading::new).setName("incoming-watch-readings").reduceByKeyAndWindow(// Key extractor
    reading -> reading.getJoggerId(), // Value extractor
    reading -> reading.getFeetRun(), // The time window (1 minute of clock time)
    WindowConfig.TumblingTimeWindow(Duration.ofSeconds(10)), // The reduce function (produces a cumulative sum)
    (cumulative, incoming) -> cumulative + incoming).setName("reduce-to-total-distance-per-jogger").map(keyWindow -> {
        // The per-key result of the previous reduce step
        long totalFeetRun = keyWindow.getValue();
        // The amount of time elapsed
        long startTime = keyWindow.getKey().getWindow().getStartTime();
        long endTime = keyWindow.getKey().getWindow().getEndTime();
        // Cast to float to use as denominator
        long timeLengthMillis = endTime - startTime;
        // The feet-per-minute calculation
        float feetPerMinute = totalFeetRun / (float) (timeLengthMillis / 1000);
        // Reduce to two decimal places
        String paceString = new DecimalFormat("#.##").format(feetPerMinute);
        // Return a per-jogger average pace
        return new KeyValue<>(keyWindow.getKey().getKey(), paceString);
    }).setName("calculate-average-speed").consume(kv -> {
        String logMessage = String.format("(runner: %s, avgFeetPerMinute: %s)", kv.getKey(), kv.getValue());
        LOG.info(logMessage);
    });
    Config config = Config.defaultConfig();
    // Fetches the topology name from the first command-line argument
    String topologyName = StreamletUtils.getTopologyName(args);
    // Finally, the processing graph and configuration are passed to the Runner, which converts
    // the graph into a Heron topology that can be run in a Heron cluster.
    new Runner().run(topologyName, config, processingGraphBuilder);
}
Also used : Runner(com.twitter.heron.streamlet.Runner) KeyValue(com.twitter.heron.streamlet.KeyValue) Config(com.twitter.heron.streamlet.Config) WindowConfig(com.twitter.heron.streamlet.WindowConfig) Builder(com.twitter.heron.streamlet.Builder) DecimalFormat(java.text.DecimalFormat)

Aggregations

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