Search in sources :

Example 6 with StreamBuilder

use of org.apache.storm.streams.StreamBuilder in project storm by apache.

the class QueryPlanner method compile.

public AbstractStreamsProcessor compile(Map<String, ISqlStreamsDataSource> sources, String query) throws Exception {
    StreamsRel relNode = getPlan(query);
    StreamsPlanCreator streamsPlanCreator = new StreamsPlanCreator(sources, new RexBuilder(typeFactory));
    relNode.streamsPlan(streamsPlanCreator);
    final StreamBuilder streamBuilder = streamsPlanCreator.getStreamBuilder();
    final Stream<Values> lastStream = streamsPlanCreator.pop();
    final DataContext dc = streamsPlanCreator.getDataContext();
    final List<CompilingClassLoader> cls = streamsPlanCreator.getClassLoaders();
    return new AbstractStreamsProcessor() {

        @Override
        public StormTopology build() {
            return streamBuilder.build();
        }

        @Override
        public Stream<Values> outputStream() {
            return lastStream;
        }

        @Override
        public DataContext getDataContext() {
            return dc;
        }

        @Override
        public List<CompilingClassLoader> getClassLoaders() {
            return cls;
        }
    };
}
Also used : DataContext(org.apache.calcite.DataContext) AbstractStreamsProcessor(org.apache.storm.sql.AbstractStreamsProcessor) CompilingClassLoader(org.apache.storm.sql.javac.CompilingClassLoader) Values(org.apache.storm.tuple.Values) RexBuilder(org.apache.calcite.rex.RexBuilder) StreamsRel(org.apache.storm.sql.planner.streams.rel.StreamsRel) StreamBuilder(org.apache.storm.streams.StreamBuilder)

Example 7 with StreamBuilder

use of org.apache.storm.streams.StreamBuilder in project storm by apache.

the class WindowedWordCount method main.

public static void main(String[] args) throws Exception {
    StreamBuilder builder = new StreamBuilder();
    // A stream of random sentences
    builder.newStream(new RandomSentenceSpout(), new ValueMapper<String>(0), 2).window(TumblingWindows.of(Duration.seconds(2))).flatMap(s -> Arrays.asList(s.split(" "))).mapToPair(w -> Pair.of(w, 1)).countByKey().filter(x -> x.getSecond() >= 5).print();
    Config config = new Config();
    String topoName = "test";
    if (args.length > 0) {
        topoName = args[0];
    }
    config.setNumWorkers(1);
    StormSubmitter.submitTopologyWithProgressBar(topoName, config, builder.build());
}
Also used : RandomSentenceSpout(org.apache.storm.starter.spout.RandomSentenceSpout) StormSubmitter(org.apache.storm.StormSubmitter) Duration(org.apache.storm.topology.base.BaseWindowedBolt.Duration) Arrays(java.util.Arrays) Pair(org.apache.storm.streams.Pair) StreamBuilder(org.apache.storm.streams.StreamBuilder) TumblingWindows(org.apache.storm.streams.windowing.TumblingWindows) ValueMapper(org.apache.storm.streams.operations.mappers.ValueMapper) Config(org.apache.storm.Config) RandomSentenceSpout(org.apache.storm.starter.spout.RandomSentenceSpout) Config(org.apache.storm.Config) StreamBuilder(org.apache.storm.streams.StreamBuilder)

Example 8 with StreamBuilder

use of org.apache.storm.streams.StreamBuilder in project storm by apache.

the class AggregateExample method main.

@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
    StreamBuilder builder = new StreamBuilder();
    /**
     * Computes average of the stream of numbers emitted by the spout. Internally the per-partition
     * sum and counts are accumulated and emitted to a downstream task where the partially accumulated
     * results are merged and the final result is emitted.
     */
    builder.newStream(new RandomIntegerSpout(), new ValueMapper<Integer>(0), 2).window(TumblingWindows.of(BaseWindowedBolt.Duration.seconds(5))).filter(x -> x > 0 && x < 500).aggregate(new Avg()).print();
    Config config = new Config();
    String topoName = "AGG_EXAMPLE";
    if (args.length > 0) {
        topoName = args[0];
    }
    config.setNumWorkers(1);
    StormSubmitter.submitTopologyWithProgressBar(topoName, config, builder.build());
}
Also used : Config(org.apache.storm.Config) RandomIntegerSpout(org.apache.storm.starter.spout.RandomIntegerSpout) StreamBuilder(org.apache.storm.streams.StreamBuilder)

Example 9 with StreamBuilder

use of org.apache.storm.streams.StreamBuilder in project storm by apache.

the class BranchExample method main.

@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
    StreamBuilder builder = new StreamBuilder();
    Stream<Integer>[] evenAndOdd = builder.newStream(new RandomIntegerSpout(), new ValueMapper<Integer>(0)).branch(x -> (x % 2) == 0, x -> (x % 2) == 1);
    evenAndOdd[0].forEach(x -> LOG.info("EVEN> " + x));
    evenAndOdd[1].forEach(x -> LOG.info("ODD > " + x));
    Config config = new Config();
    String topoName = "branchExample";
    if (args.length > 0) {
        topoName = args[0];
    }
    config.setNumWorkers(1);
    StormSubmitter.submitTopologyWithProgressBar(topoName, config, builder.build());
}
Also used : ValueMapper(org.apache.storm.streams.operations.mappers.ValueMapper) Config(org.apache.storm.Config) RandomIntegerSpout(org.apache.storm.starter.spout.RandomIntegerSpout) Stream(org.apache.storm.streams.Stream) StreamBuilder(org.apache.storm.streams.StreamBuilder)

Example 10 with StreamBuilder

use of org.apache.storm.streams.StreamBuilder in project storm by apache.

the class GroupByKeyAndWindowExample method main.

public static void main(String[] args) throws Exception {
    StreamBuilder builder = new StreamBuilder();
    // a stream of stock quotes
    builder.newStream(new StockQuotes(), new PairValueMapper<String, Double>(0, 1)).groupByKeyAndWindow(SlidingWindows.of(Count.of(6), Count.of(3))).print();
    // a stream of stock quotes
    builder.newStream(new StockQuotes(), new PairValueMapper<String, Double>(0, 1)).reduceByKeyAndWindow((x, y) -> x > y ? x : y, SlidingWindows.of(Count.of(6), Count.of(3))).print();
    Config config = new Config();
    String topoName = GroupByKeyAndWindowExample.class.getName();
    if (args.length > 0) {
        topoName = args[0];
    }
    config.setNumWorkers(1);
    StormSubmitter.submitTopologyWithProgressBar(topoName, config, builder.build());
}
Also used : StormSubmitter(org.apache.storm.StormSubmitter) OutputFieldsDeclarer(org.apache.storm.topology.OutputFieldsDeclarer) BaseRichSpout(org.apache.storm.topology.base.BaseRichSpout) Arrays(java.util.Arrays) PairValueMapper(org.apache.storm.streams.operations.mappers.PairValueMapper) PairStream(org.apache.storm.streams.PairStream) StreamBuilder(org.apache.storm.streams.StreamBuilder) TopologyContext(org.apache.storm.task.TopologyContext) Fields(org.apache.storm.tuple.Fields) Utils(org.apache.storm.utils.Utils) Reducer(org.apache.storm.streams.operations.Reducer) List(java.util.List) Values(org.apache.storm.tuple.Values) SlidingWindows(org.apache.storm.streams.windowing.SlidingWindows) Window(org.apache.storm.streams.windowing.Window) Map(java.util.Map) Count(org.apache.storm.topology.base.BaseWindowedBolt.Count) Config(org.apache.storm.Config) SpoutOutputCollector(org.apache.storm.spout.SpoutOutputCollector) Config(org.apache.storm.Config) StreamBuilder(org.apache.storm.streams.StreamBuilder)

Aggregations

StreamBuilder (org.apache.storm.streams.StreamBuilder)10 Config (org.apache.storm.Config)9 StormSubmitter (org.apache.storm.StormSubmitter)4 ValueMapper (org.apache.storm.streams.operations.mappers.ValueMapper)4 Values (org.apache.storm.tuple.Values)4 Map (java.util.Map)3 SpoutOutputCollector (org.apache.storm.spout.SpoutOutputCollector)3 RandomIntegerSpout (org.apache.storm.starter.spout.RandomIntegerSpout)3 TopologyContext (org.apache.storm.task.TopologyContext)3 TestWordSpout (org.apache.storm.testing.TestWordSpout)3 OutputFieldsDeclarer (org.apache.storm.topology.OutputFieldsDeclarer)3 BaseRichSpout (org.apache.storm.topology.base.BaseRichSpout)3 Fields (org.apache.storm.tuple.Fields)3 Utils (org.apache.storm.utils.Utils)3 Arrays (java.util.Arrays)2 Pair (org.apache.storm.streams.Pair)2 PairStream (org.apache.storm.streams.PairStream)2 Stream (org.apache.storm.streams.Stream)2 PairValueMapper (org.apache.storm.streams.operations.mappers.PairValueMapper)2 TumblingWindows (org.apache.storm.streams.windowing.TumblingWindows)2