Search in sources :

Example 1 with StreamletReducers

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

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

the class WindowedWordCountTopology method main.

public static void main(String[] args) throws Exception {
    Builder processingGraphBuilder = Builder.newBuilder();
    processingGraphBuilder.newSource(() -> StreamletUtils.randomFromList(SENTENCES)).setName("random-sentences-source").flatMap(sentence -> Arrays.asList(sentence.toLowerCase().split("\\s+"))).setName("flatten-into-individual-words").reduceByKeyAndWindow(// The key extractor (the word is left unchanged)
    word -> word, // Value extractor (the value is always 1)
    word -> 1, WindowConfig.TumblingCountWindow(50), StreamletReducers::sum).setName("reduce-operation").consume(kv -> {
        String logMessage = String.format("(word: %s, count: %d)", kv.getKey().getKey(), kv.getValue());
        LOG.info(logMessage);
    });
    // The topology's parallelism (the number of containers across which the topology's
    // processing instance will be split) can be defined via the second command-line
    // argument (or else the default of 2 will be used).
    int topologyParallelism = StreamletUtils.getParallelism(args, 2);
    Config config = Config.newBuilder().setNumContainers(topologyParallelism).build();
    // 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(org.apache.heron.streamlet.Runner) WindowConfig(org.apache.heron.streamlet.WindowConfig) Config(org.apache.heron.streamlet.Config) Builder(org.apache.heron.streamlet.Builder) StreamletReducers(org.apache.heron.streamlet.StreamletReducers)

Aggregations

Builder (org.apache.heron.streamlet.Builder)2 StreamletReducers (org.apache.heron.streamlet.StreamletReducers)2 MalformedURLException (java.net.MalformedURLException)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Config (org.apache.heron.api.Config)1 AbstractTestTopology (org.apache.heron.integration_test.common.AbstractTestTopology)1 TestTopologyBuilder (org.apache.heron.integration_test.core.TestTopologyBuilder)1 Config (org.apache.heron.streamlet.Config)1 Runner (org.apache.heron.streamlet.Runner)1 SerializableFunction (org.apache.heron.streamlet.SerializableFunction)1 Streamlet (org.apache.heron.streamlet.Streamlet)1 WindowConfig (org.apache.heron.streamlet.WindowConfig)1 BuilderImpl (org.apache.heron.streamlet.impl.BuilderImpl)1