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();
}
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);
}
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);
}
}
}
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");
}
}
Aggregations