Search in sources :

Example 1 with SerializableFunction

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

the class StreamletWithKeybyCountAndReduce method buildTopology.

@Override
protected TestTopologyBuilder buildTopology(TestTopologyBuilder testTopologyBuilder) {
    Builder streamletBuilder = Builder.newBuilder();
    Streamlet<String> monthStreamlet = streamletBuilder.newSource(() -> MONTHS).setName("months-text").flatMap((String m) -> Arrays.asList(m.split(" - "))).setName("months").filter((month) -> incomingMonths.add(month.toLowerCase())).setName("unique-months");
    SerializableFunction<String, String> getSeason = month -> {
        if (SPRING_MONTHS.contains(month)) {
            return "spring";
        } else if (SUMMER_MONTHS.contains(month)) {
            return "summer";
        } else if (FALL_MONTHS.contains(month)) {
            return "fall";
        } else if (WINTER_MONTHS.contains(month)) {
            return "winter";
        } else {
            return "really?";
        }
    };
    SerializableFunction<String, Integer> getNumberOfDays = month -> {
        switch(month) {
            case "january":
                return 31;
            case "february":
                // Dont use this code in real projects
                return 28;
            case "march":
                return 31;
            case "april":
                return 30;
            case "may":
                return 31;
            case "june":
                return 30;
            case "july":
                return 31;
            case "august":
                return 31;
            case "september":
                return 30;
            case "october":
                return 31;
            case "november":
                return 30;
            case "december":
                return 31;
            default:
                // Shouldn't be here
                return -1;
        }
    };
    // Count months per season
    monthStreamlet.keyBy(getSeason, getNumberOfDays).setName("key-by-season").countByKey(x -> x.getKey()).setName("key-by-and-count").map(x -> String.format("%s: %d months", x.getKey(), x.getValue())).setName("to-string");
    // Sum days per season
    monthStreamlet.<String, Integer>reduceByKey(getSeason, getNumberOfDays, StreamletReducers::sum).setName("sum-by-season").map(x -> String.format("%s: %d days", x.getKey(), x.getValue())).setName("to-string-2");
    BuilderImpl streamletBuilderImpl = (BuilderImpl) streamletBuilder;
    TestTopologyBuilder topology = (TestTopologyBuilder) streamletBuilderImpl.build(testTopologyBuilder);
    return topology;
}
Also used : HashSet(java.util.HashSet) SerializableFunction(org.apache.heron.streamlet.SerializableFunction) Arrays(java.util.Arrays) MalformedURLException(java.net.MalformedURLException) Config(org.apache.heron.api.Config) AbstractTestTopology(org.apache.heron.integration_test.common.AbstractTestTopology) Set(java.util.Set) Builder(org.apache.heron.streamlet.Builder) TestTopologyBuilder(org.apache.heron.integration_test.core.TestTopologyBuilder) StreamletReducers(org.apache.heron.streamlet.StreamletReducers) BuilderImpl(org.apache.heron.streamlet.impl.BuilderImpl) Streamlet(org.apache.heron.streamlet.Streamlet) BuilderImpl(org.apache.heron.streamlet.impl.BuilderImpl) Builder(org.apache.heron.streamlet.Builder) TestTopologyBuilder(org.apache.heron.integration_test.core.TestTopologyBuilder) TestTopologyBuilder(org.apache.heron.integration_test.core.TestTopologyBuilder)

Example 2 with SerializableFunction

use of org.apache.heron.streamlet.SerializableFunction 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

HashSet (java.util.HashSet)2 Set (java.util.Set)2 Config (org.apache.heron.api.Config)2 SerializableFunction (org.apache.heron.streamlet.SerializableFunction)2 MalformedURLException (java.net.MalformedURLException)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 IOutputCollector (org.apache.heron.api.bolt.IOutputCollector)1 OutputCollector (org.apache.heron.api.bolt.OutputCollector)1 TopologyAPI (org.apache.heron.api.generated.TopologyAPI)1 TopologyBuilder (org.apache.heron.api.topology.TopologyBuilder)1 TopologyContext (org.apache.heron.api.topology.TopologyContext)1 Fields (org.apache.heron.api.tuple.Fields)1 Tuple (org.apache.heron.api.tuple.Tuple)1 Values (org.apache.heron.api.tuple.Values)1