linux下eclipse开发storm,如何在eclipse调试storm程序?

32b5c1813c872e701c42843f244dde6f.png

江户川乱折腾

一、介绍storm提供了两种运行模式:本地模式和分布式模式。本地模式针对开发调试storm topologies非常有用。Storm has two modes of operation: local mode and distributed mode. In local mode, Storm executes completely in process by simulating worker nodes with threads. Local mode is useful for testing and development of topologies因为多数程序开发者都是使用windows系统进行程序开发,如果在本机不安装storm环境的情况下,开发、调试storm程序。如果你正在为此问题而烦恼,请使用本文提供的方法。二、实施步骤如何基于eclipse+maven调试storm程序,步骤如下:1.搭建好开发环境(eclipse+maven,本人使用的是eclipse Kepler 与maven3.1.1)2.创建maven项目,并修改pom.xml,内容如pom.xml(机器联网,下载所需的依赖jar)Github上的pom.xml,引入的依赖太多,有些不需要,3. 编写storm程序,指定为本地模式运行。本文提供的程序是wordcount重要的是LocalCluster cluster = new LocalCluster();这一句Config conf = new Config();conf.setDebug(true);conf.setNumWorkers(2);LocalCluster cluster = new LocalCluster();cluster.submitTopology("test", conf, builder.createTopology());Utils.sleep(10000);cluster.killTopology("test");cluster.shutdown();pom.xml文件4.0.0storm.starterstorm-starter0.0.1-SNAPSHOTjarUTF-8github-releaseshttp://oss.sonatype.org/content/repositories/github-releases/clojars.orghttp://clojars.org/repojunitjunit4.11teststormstorm0.9.0.1providedcommons-collectionscommons-collections3.2.1storm程序package storm.starter;import java.util.HashMap;import java.util.Map;import storm.starter.spout.RandomSentenceSpout;import backtype.storm.Config;import backtype.storm.LocalCluster;import backtype.storm.StormSubmitter;import backtype.storm.topology.BasicOutputCollector;import backtype.storm.topology.OutputFieldsDeclarer;import backtype.storm.topology.TopologyBuilder;import backtype.storm.topology.base.BaseBasicBolt;import backtype.storm.tuple.Fields;import backtype.storm.tuple.Tuple;import backtype.storm.tuple.Values;/*** This topology demonstrates Storm's stream groupings and multilang* capabilities.*/public class WordCountTopology {public static class SplitSentence extends BaseBasicBolt {@Overridepublic void execute(Tuple input, BasicOutputCollector collector) {try {String msg = input.getString(0);System.out.println(msg + "-------------------");if (msg != null) {String[] s = msg.split(" ");for (String string : s) {collector.emit(new Values(string));}}} catch (Exception e) {e.printStackTrace();}}@Overridepublic void declareOutputFields(OutputFieldsDeclarer declarer) {declarer.declare(new Fields("word"));}}public static class WordCount extends BaseBasicBolt {Map counts = new HashMap();@Overridepublic void execute(Tuple tuple, BasicOutputCollector collector) {String word = tuple.getString(0);Integer count = counts.get(word);if (count == null)count = 0;count++;counts.put(word, count);collector.emit(new Values(word, count));}@Overridepublic void declareOutputFields(OutputFieldsDeclarer declarer) {declarer.declare(new Fields("word", "count"));}}public static void main(String[] args) throws Exception {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);if (args != null && args.length > 0) {conf.setNumWorkers(3);StormSubmitter.submitTopology(args[0], conf,builder.createTopology());} else {conf.setMaxTaskParallelism(3);LocalCluster cluster = new LocalCluster();cluster.submitTopology("word-count", conf, builder.createTopology());Thread.sleep(10000);cluster.shutdown();}}}package storm.starter.spout;import backtype.storm.spout.SpoutOutputCollector;import backtype.storm.task.TopologyContext;import backtype.storm.topology.OutputFieldsDeclarer;import backtype.storm.topology.base.BaseRichSpout;import backtype.storm.tuple.Fields;import backtype.storm.tuple.Values;import backtype.storm.utils.Utils;import java.util.Map;import java.util.Random;public class RandomSentenceSpout extends BaseRichSpout {SpoutOutputCollector _collector;Random _rand;@Overridepublic void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {_collector = collector;_rand = new Random();}@Overridepublic void nextTuple() {Utils.sleep(100);String[] sentences = new String[]{ "the cow jumped over the moon", "an apple a day keeps the doctor away","four score and seven years ago", "snow white and the seven dwarfs", "i am at two with nature" };String sentence = sentences[_rand.nextInt(sentences.length)];_collector.emit(new Values(sentence));}@Overridepublic void ack(Object id) {}@Overridepublic void fail(Object id) {}@Overridepublic void declareOutputFields(OutputFieldsDeclarer declarer) {declarer.declare(new Fields("word"));}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值