Search in sources :

Example 6 with ValueMapper

use of org.apache.storm.streams.operations.mappers.ValueMapper 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();
    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 : LocalCluster(org.apache.storm.LocalCluster) 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 7 with ValueMapper

use of org.apache.storm.streams.operations.mappers.ValueMapper 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();
    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 : RandomSentenceSpout(org.apache.storm.starter.spout.RandomSentenceSpout) LocalCluster(org.apache.storm.LocalCluster) 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) Utils(org.apache.storm.utils.Utils) RandomSentenceSpout(org.apache.storm.starter.spout.RandomSentenceSpout) LocalCluster(org.apache.storm.LocalCluster) Config(org.apache.storm.Config) StreamBuilder(org.apache.storm.streams.StreamBuilder)

Example 8 with ValueMapper

use of org.apache.storm.streams.operations.mappers.ValueMapper in project storm by apache.

the class StreamBuilderTest method testPartitionByKeySinglePartition.

@Test
public void testPartitionByKeySinglePartition() {
    TopologyContext mockContext = Mockito.mock(TopologyContext.class);
    OutputCollector mockCollector = Mockito.mock(OutputCollector.class);
    Stream<Integer> stream = streamBuilder.newStream(newSpout(Utils.DEFAULT_STREAM_ID), new ValueMapper<>(0));
    stream.mapToPair(x -> Pair.of(x, x)).reduceByKey((x, y) -> x + y).print();
    StormTopology topology = streamBuilder.build();
    assertEquals(1, topology.get_bolts_size());
}
Also used : OutputFieldsDeclarer(org.apache.storm.topology.OutputFieldsDeclarer) BaseRichSpout(org.apache.storm.topology.base.BaseRichSpout) IRichSpout(org.apache.storm.topology.IRichSpout) BaseWindowedBolt(org.apache.storm.topology.base.BaseWindowedBolt) TopologyContext(org.apache.storm.task.TopologyContext) HashMap(java.util.HashMap) Count(org.apache.storm.streams.operations.aggregators.Count) Bolt(org.apache.storm.generated.Bolt) Tuple(org.apache.storm.tuple.Tuple) OutputCollector(org.apache.storm.task.OutputCollector) StormTopology(org.apache.storm.generated.StormTopology) BranchProcessor(org.apache.storm.streams.processors.BranchProcessor) Map(java.util.Map) ValueMapper(org.apache.storm.streams.operations.mappers.ValueMapper) Before(org.junit.Before) BaseRichBolt(org.apache.storm.topology.base.BaseRichBolt) PairValueMapper(org.apache.storm.streams.operations.mappers.PairValueMapper) Grouping(org.apache.storm.generated.Grouping) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Fields(org.apache.storm.tuple.Fields) Utils(org.apache.storm.utils.Utils) GlobalStreamId(org.apache.storm.generated.GlobalStreamId) Mockito(org.mockito.Mockito) TumblingWindows(org.apache.storm.streams.windowing.TumblingWindows) SpoutSpec(org.apache.storm.generated.SpoutSpec) IRichBolt(org.apache.storm.topology.IRichBolt) NullStruct(org.apache.storm.generated.NullStruct) SpoutOutputCollector(org.apache.storm.spout.SpoutOutputCollector) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) OutputCollector(org.apache.storm.task.OutputCollector) SpoutOutputCollector(org.apache.storm.spout.SpoutOutputCollector) StormTopology(org.apache.storm.generated.StormTopology) TopologyContext(org.apache.storm.task.TopologyContext) Test(org.junit.Test)

Example 9 with ValueMapper

use of org.apache.storm.streams.operations.mappers.ValueMapper in project storm by apache.

the class StreamBuilderTest method testMultiPartitionByKey.

@Test
public void testMultiPartitionByKey() {
    TopologyContext mockContext = Mockito.mock(TopologyContext.class);
    OutputCollector mockCollector = Mockito.mock(OutputCollector.class);
    Stream<Integer> stream = streamBuilder.newStream(newSpout(Utils.DEFAULT_STREAM_ID), new ValueMapper<>(0));
    stream.mapToPair(x -> Pair.of(x, x)).window(TumblingWindows.of(BaseWindowedBolt.Count.of(10))).reduceByKey((x, y) -> x + y).reduceByKey((x, y) -> 0).print();
    StormTopology topology = streamBuilder.build();
    assertEquals(2, topology.get_bolts_size());
}
Also used : OutputFieldsDeclarer(org.apache.storm.topology.OutputFieldsDeclarer) BaseRichSpout(org.apache.storm.topology.base.BaseRichSpout) IRichSpout(org.apache.storm.topology.IRichSpout) BaseWindowedBolt(org.apache.storm.topology.base.BaseWindowedBolt) TopologyContext(org.apache.storm.task.TopologyContext) HashMap(java.util.HashMap) Count(org.apache.storm.streams.operations.aggregators.Count) Bolt(org.apache.storm.generated.Bolt) Tuple(org.apache.storm.tuple.Tuple) OutputCollector(org.apache.storm.task.OutputCollector) StormTopology(org.apache.storm.generated.StormTopology) BranchProcessor(org.apache.storm.streams.processors.BranchProcessor) Map(java.util.Map) ValueMapper(org.apache.storm.streams.operations.mappers.ValueMapper) Before(org.junit.Before) BaseRichBolt(org.apache.storm.topology.base.BaseRichBolt) PairValueMapper(org.apache.storm.streams.operations.mappers.PairValueMapper) Grouping(org.apache.storm.generated.Grouping) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Fields(org.apache.storm.tuple.Fields) Utils(org.apache.storm.utils.Utils) GlobalStreamId(org.apache.storm.generated.GlobalStreamId) Mockito(org.mockito.Mockito) TumblingWindows(org.apache.storm.streams.windowing.TumblingWindows) SpoutSpec(org.apache.storm.generated.SpoutSpec) IRichBolt(org.apache.storm.topology.IRichBolt) NullStruct(org.apache.storm.generated.NullStruct) SpoutOutputCollector(org.apache.storm.spout.SpoutOutputCollector) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) OutputCollector(org.apache.storm.task.OutputCollector) SpoutOutputCollector(org.apache.storm.spout.SpoutOutputCollector) StormTopology(org.apache.storm.generated.StormTopology) TopologyContext(org.apache.storm.task.TopologyContext) Test(org.junit.Test)

Aggregations

ValueMapper (org.apache.storm.streams.operations.mappers.ValueMapper)9 Utils (org.apache.storm.utils.Utils)7 Map (java.util.Map)6 SpoutOutputCollector (org.apache.storm.spout.SpoutOutputCollector)6 TumblingWindows (org.apache.storm.streams.windowing.TumblingWindows)6 TopologyContext (org.apache.storm.task.TopologyContext)6 IRichBolt (org.apache.storm.topology.IRichBolt)6 OutputFieldsDeclarer (org.apache.storm.topology.OutputFieldsDeclarer)6 BaseRichSpout (org.apache.storm.topology.base.BaseRichSpout)6 Fields (org.apache.storm.tuple.Fields)6 Collections (java.util.Collections)5 HashMap (java.util.HashMap)5 Bolt (org.apache.storm.generated.Bolt)5 GlobalStreamId (org.apache.storm.generated.GlobalStreamId)5 Grouping (org.apache.storm.generated.Grouping)5 NullStruct (org.apache.storm.generated.NullStruct)5 SpoutSpec (org.apache.storm.generated.SpoutSpec)5 StormTopology (org.apache.storm.generated.StormTopology)5 Count (org.apache.storm.streams.operations.aggregators.Count)5 PairValueMapper (org.apache.storm.streams.operations.mappers.PairValueMapper)5