Search in sources :

Example 1 with StreamState

use of org.apache.storm.streams.StreamState 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");
    if (args.length > 0) {
        config.setNumWorkers(1);
        StormSubmitter.submitTopologyWithProgressBar(args[0], config, builder.build());
    } else {
        try (LocalCluster cluster = new LocalCluster();
            LocalCluster.LocalTopology topo = cluster.submitTopology("test", config, builder.build())) {
            Utils.sleep(60_000);
        }
    }
}
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) LocalCluster(org.apache.storm.LocalCluster) 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) LocalCluster(org.apache.storm.LocalCluster) Config(org.apache.storm.Config) TestWordSpout(org.apache.storm.testing.TestWordSpout) StreamBuilder(org.apache.storm.streams.StreamBuilder)

Aggregations

Map (java.util.Map)1 Config (org.apache.storm.Config)1 LocalCluster (org.apache.storm.LocalCluster)1 StormSubmitter (org.apache.storm.StormSubmitter)1 SpoutOutputCollector (org.apache.storm.spout.SpoutOutputCollector)1 Pair (org.apache.storm.streams.Pair)1 Stream (org.apache.storm.streams.Stream)1 StreamBuilder (org.apache.storm.streams.StreamBuilder)1 StreamState (org.apache.storm.streams.StreamState)1 ValueMapper (org.apache.storm.streams.operations.mappers.ValueMapper)1 TopologyContext (org.apache.storm.task.TopologyContext)1 TestWordSpout (org.apache.storm.testing.TestWordSpout)1 OutputFieldsDeclarer (org.apache.storm.topology.OutputFieldsDeclarer)1 BaseRichSpout (org.apache.storm.topology.base.BaseRichSpout)1 Fields (org.apache.storm.tuple.Fields)1 Values (org.apache.storm.tuple.Values)1 Utils (org.apache.storm.utils.Utils)1