Search in sources :

Example 1 with RandomSentenceSpout

use of org.apache.storm.starter.spout.RandomSentenceSpout in project storm by apache.

the class KafkaProducerTopology method newTopology.

/**
     * @param brokerUrl Kafka broker URL
     * @param topicName Topic to which publish sentences
     * @return A Storm topology that produces random sentences using {@link RandomSentenceSpout} and uses a {@link KafkaBolt} to
     * publish the sentences to the kafka topic specified
     */
public static StormTopology newTopology(String brokerUrl, String topicName) {
    final TopologyBuilder builder = new TopologyBuilder();
    builder.setSpout("spout", new RandomSentenceSpout.TimeStamped(""), 2);
    /* The output field of the RandomSentenceSpout ("word") is provided as the boltMessageField
          so that this gets written out as the message in the kafka topic. */
    final KafkaBolt<String, String> bolt = new KafkaBolt<String, String>().withProducerProperties(newProps(brokerUrl, topicName)).withTopicSelector(new DefaultTopicSelector(topicName)).withTupleToKafkaMapper(new FieldNameBasedTupleToKafkaMapper<>("key", "word"));
    builder.setBolt("forwardToKafka", bolt, 1).shuffleGrouping("spout");
    return builder.createTopology();
}
Also used : RandomSentenceSpout(org.apache.storm.starter.spout.RandomSentenceSpout) TopologyBuilder(org.apache.storm.topology.TopologyBuilder) DefaultTopicSelector(org.apache.storm.kafka.bolt.selector.DefaultTopicSelector)

Example 2 with RandomSentenceSpout

use of org.apache.storm.starter.spout.RandomSentenceSpout in project storm by apache.

the class WordCountTopology method run.

protected int run(String[] args) {
    TopologyBuilder builder = new TopologyBuilder();
    builder.setSpout("spout", new RandomSentenceSpout(), 5);
    builder.setBolt("split", new SplitSentence(), 8).shuffleGrouping("spout");
    builder.setBolt("count", new WordCount(), 12).fieldsGrouping("split", new Fields("word"));
    conf.setDebug(true);
    String topologyName = "word-count";
    if (isLocal) {
        conf.setMaxTaskParallelism(3);
        ttl = 10;
    } else {
        conf.setNumWorkers(3);
    }
    if (args != null && args.length > 0) {
        topologyName = args[0];
    }
    return submit(topologyName, conf, builder);
}
Also used : RandomSentenceSpout(org.apache.storm.starter.spout.RandomSentenceSpout) Fields(org.apache.storm.tuple.Fields) TopologyBuilder(org.apache.storm.topology.TopologyBuilder)

Example 3 with RandomSentenceSpout

use of org.apache.storm.starter.spout.RandomSentenceSpout 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 4 with RandomSentenceSpout

use of org.apache.storm.starter.spout.RandomSentenceSpout in project jstorm by alibaba.

the class WordCountTopology method test.

public static void test() {
    TopologyBuilder builder = new TopologyBuilder();
    builder.setSpout("spout", new RandomSentenceSpout(), 5);
    builder.setBolt("split", new SplitSentence(), 8).shuffleGrouping("spout");
    builder.setBolt("count", new WordCount(), 12).fieldsGrouping("split", new Fields("word"));
    Config conf = new Config();
    conf.setDebug(true);
    String[] className = Thread.currentThread().getStackTrace()[1].getClassName().split("\\.");
    String topologyName = className[className.length - 1];
    try {
        JStormHelper.runTopology(builder.createTopology(), topologyName, conf, 60, new JStormHelper.CheckAckedFail(conf), isLocal);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Assert.fail("Failed");
    }
}
Also used : RandomSentenceSpout(org.apache.storm.starter.spout.RandomSentenceSpout) JStormHelper(com.alibaba.starter.utils.JStormHelper) Fields(backtype.storm.tuple.Fields) TopologyBuilder(backtype.storm.topology.TopologyBuilder) TransactionalTopologyBuilder(backtype.storm.transactional.TransactionalTopologyBuilder) Config(backtype.storm.Config)

Aggregations

RandomSentenceSpout (org.apache.storm.starter.spout.RandomSentenceSpout)4 TopologyBuilder (org.apache.storm.topology.TopologyBuilder)2 Config (backtype.storm.Config)1 TopologyBuilder (backtype.storm.topology.TopologyBuilder)1 TransactionalTopologyBuilder (backtype.storm.transactional.TransactionalTopologyBuilder)1 Fields (backtype.storm.tuple.Fields)1 JStormHelper (com.alibaba.starter.utils.JStormHelper)1 Arrays (java.util.Arrays)1 Config (org.apache.storm.Config)1 LocalCluster (org.apache.storm.LocalCluster)1 StormSubmitter (org.apache.storm.StormSubmitter)1 DefaultTopicSelector (org.apache.storm.kafka.bolt.selector.DefaultTopicSelector)1 Pair (org.apache.storm.streams.Pair)1 StreamBuilder (org.apache.storm.streams.StreamBuilder)1 ValueMapper (org.apache.storm.streams.operations.mappers.ValueMapper)1 TumblingWindows (org.apache.storm.streams.windowing.TumblingWindows)1 Duration (org.apache.storm.topology.base.BaseWindowedBolt.Duration)1 Fields (org.apache.storm.tuple.Fields)1 Utils (org.apache.storm.utils.Utils)1