Search in sources :

Example 1 with StreamBuilder

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

the class JoinExample method main.

public static void main(String[] args) throws Exception {
    StreamBuilder builder = new StreamBuilder();
    // a stream of (number, square) pairs
    PairStream<Integer, Integer> squares = builder.newStream(new NumberSpout(x -> x * x), new PairValueMapper<>(0, 1));
    // a stream of (number, cube) pairs
    PairStream<Integer, Integer> cubes = builder.newStream(new NumberSpout(x -> x * x * x), new PairValueMapper<>(0, 1));
    // create a windowed stream of five seconds duration
    squares.window(TumblingWindows.of(Duration.seconds(5))).join(cubes).print();
    Config config = new Config();
    String topoName = JoinExample.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) Duration(org.apache.storm.topology.base.BaseWindowedBolt.Duration) BaseRichSpout(org.apache.storm.topology.base.BaseRichSpout) 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) Function(org.apache.storm.streams.operations.Function) Values(org.apache.storm.tuple.Values) TumblingWindows(org.apache.storm.streams.windowing.TumblingWindows) Map(java.util.Map) Config(org.apache.storm.Config) SpoutOutputCollector(org.apache.storm.spout.SpoutOutputCollector) Config(org.apache.storm.Config) StreamBuilder(org.apache.storm.streams.StreamBuilder)

Example 2 with StreamBuilder

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

the class StatefulWordCount method main.

public static void main(String[] args) throws Exception {
    StreamBuilder builder = new StreamBuilder();
    // a stream of words
    builder.newStream(new TestWordSpout(), new ValueMapper<String>(0), 2).window(TumblingWindows.of(BaseWindowedBolt.Duration.seconds(2))).mapToPair(w -> Pair.of(w, 1)).countByKey().updateStateByKey(0L, (state, count) -> state + count).toPairStream().print();
    Config config = new Config();
    // use redis based state store for persistence
    config.put(Config.TOPOLOGY_STATE_PROVIDER, "org.apache.storm.redis.state.RedisKeyValueStateProvider");
    String topoName = "test";
    if (args.length > 0) {
        topoName = args[0];
    }
    config.setNumWorkers(1);
    StormSubmitter.submitTopologyWithProgressBar(topoName, config, builder.build());
}
Also used : Config(org.apache.storm.Config) TestWordSpout(org.apache.storm.testing.TestWordSpout) StreamBuilder(org.apache.storm.streams.StreamBuilder)

Example 3 with StreamBuilder

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

the class StateQueryExample method main.

public static void main(String[] args) throws Exception {
    StreamBuilder builder = new StreamBuilder();
    StreamState<String, Long> ss = builder.newStream(new TestWordSpout(), new ValueMapper<String>(0), 2).mapToPair(w -> Pair.of(w, 1)).updateStateByKey(0L, (count, val) -> count + 1);
    /*
         * A stream of words emitted by the QuerySpout is used as
         * the keys to query the state.
         */
    builder.newStream(new QuerySpout(), new ValueMapper<String>(0)).stateQuery(ss).print();
    Config config = new Config();
    // use redis based state store for persistence
    config.put(Config.TOPOLOGY_STATE_PROVIDER, "org.apache.storm.redis.state.RedisKeyValueStateProvider");
    String topoName = "test";
    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) Pair(org.apache.storm.streams.Pair) StreamBuilder(org.apache.storm.streams.StreamBuilder) StreamState(org.apache.storm.streams.StreamState) TopologyContext(org.apache.storm.task.TopologyContext) Fields(org.apache.storm.tuple.Fields) Utils(org.apache.storm.utils.Utils) Values(org.apache.storm.tuple.Values) Stream(org.apache.storm.streams.Stream) Map(java.util.Map) ValueMapper(org.apache.storm.streams.operations.mappers.ValueMapper) Config(org.apache.storm.Config) TestWordSpout(org.apache.storm.testing.TestWordSpout) SpoutOutputCollector(org.apache.storm.spout.SpoutOutputCollector) Config(org.apache.storm.Config) TestWordSpout(org.apache.storm.testing.TestWordSpout) StreamBuilder(org.apache.storm.streams.StreamBuilder)

Example 4 with StreamBuilder

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

the class TypedTupleExample method main.

/**
 * The spout emits sequences of (Integer, Long, Long). TupleValueMapper can be used to extract fields
 * from the values and produce a stream of typed tuple (Tuple3&lt;Integer, Long, Long&gt; in this case.
 */
public static void main(String[] args) throws Exception {
    StreamBuilder builder = new StreamBuilder();
    Stream<Tuple3<Integer, Long, Long>> stream = builder.newStream(new RandomIntegerSpout(), TupleValueMappers.of(0, 1, 2));
    PairStream<Long, Integer> pairs = stream.mapToPair(t -> Pair.of(t.value2 / 10000, t.value1));
    pairs.window(TumblingWindows.of(Count.of(10))).groupByKey().print();
    String topoName = "test";
    if (args.length > 0) {
        topoName = args[0];
    }
    Config config = new Config();
    config.setNumWorkers(1);
    StormSubmitter.submitTopologyWithProgressBar(topoName, config, builder.build());
}
Also used : Config(org.apache.storm.Config) Tuple3(org.apache.storm.streams.tuple.Tuple3) RandomIntegerSpout(org.apache.storm.starter.spout.RandomIntegerSpout) StreamBuilder(org.apache.storm.streams.StreamBuilder)

Example 5 with StreamBuilder

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

the class WordCountToBolt method main.

public static void main(String[] args) throws Exception {
    StreamBuilder builder = new StreamBuilder();
    // Redis config parameters for the RedisStoreBolt
    JedisPoolConfig poolConfig = new JedisPoolConfig.Builder().setHost("127.0.0.1").setPort(6379).build();
    // Storm tuple to redis key-value mapper
    RedisStoreMapper storeMapper = new WordCountStoreMapper();
    // The redis bolt (sink)
    IRichBolt redisStoreBolt = new RedisStoreBolt(poolConfig, storeMapper);
    // A stream of words
    builder.newStream(new TestWordSpout(), new ValueMapper<String>(0)).mapToPair(w -> Pair.of(w, 1)).countByKey().to(redisStoreBolt);
    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 : IRichBolt(org.apache.storm.topology.IRichBolt) RedisStoreBolt(org.apache.storm.redis.bolt.RedisStoreBolt) ValueMapper(org.apache.storm.streams.operations.mappers.ValueMapper) Config(org.apache.storm.Config) JedisPoolConfig(org.apache.storm.redis.common.config.JedisPoolConfig) TestWordSpout(org.apache.storm.testing.TestWordSpout) RedisStoreMapper(org.apache.storm.redis.common.mapper.RedisStoreMapper) JedisPoolConfig(org.apache.storm.redis.common.config.JedisPoolConfig) 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