publicclassWordCountSpoutextendsBaseRichSpout{privatestaticfinallong serialVersionUID =1L;//定义要产生的数据privateString[] datas ={"I love Beijing","I love China","Beijing is the capital of China"};//定义一个变量保存输出流privateSpoutOutputCollector collector;@OverridepublicvoidnextTuple(){//每隔2秒采集一次Utils.sleep(2000);// 又storm框架调用,用于如何接收数据//产生3以内的随机数int random =(newRandom()).nextInt(3);String data = datas[random];//发送给下一级组件System.out.println("采集的数数是:"+data);this.collector.emit(newValues(data));}@Overridepublicvoidopen(Map arg0,TopologyContext arg1,SpoutOutputCollector collector){// 相当于初始化方法// SpoutOutputCollector collector : 输出流this.collector = collector;}@OverridepublicvoiddeclareOutputFields(OutputFieldsDeclarer declare){// 声明tuple格式
declare.declare(newFields("sentence"));}}
(2)开发WordCountSplitBolt组件
publicclassWordCountSplitBoltextendsBaseRichBolt{privatestaticfinallong serialVersionUID =1L;privateOutputCollector collector;@Overridepublicvoidexecute(Tuple tuple){// 如何处理上一级组件发来的数据String data = tuple.getStringByField("sentence");//分词String[] words = data.split(" ");//输出for(String w : words){
collector.emit(newValues(w,1));}}@Overridepublicvoidprepare(Map arg0,TopologyContext arg1,OutputCollector collector){// bolt进行初始化this.collector = collector;}@OverridepublicvoiddeclareOutputFields(OutputFieldsDeclarer d){// 申明下级tuple的格式
d.declare(newFields("word","count"));}}
(3)开发WordCountTotalBolt组件
publicclassWordCountTotalBoltextendsBaseRichBolt{privatestaticfinallong serialVersionUID =1L;privateOutputCollector collector;privateMap<String,Integer> resut =newHashMap<>();@Overridepublicvoidexecute(Tuple tuple){// 对每个单词进行计数//取数据String word = tuple.getStringByField("word");int count = tuple.getIntegerByField("count");if(resut.containsKey(word)){int total = resut.get(word);
resut.put(word, total+count);}else{
resut.put(word, count);}System.out.println("统计结果是:"+resut);//结果发动到下一级组件this.collector.emit(newValues(word,resut.get(word)));}@Overridepublicvoidprepare(Map arg0,TopologyContext arg1,OutputCollector collector){// TODO Auto-generated method stubthis.collector = collector;}@OverridepublicvoiddeclareOutputFields(OutputFieldsDeclarer declare){// TODO Auto-generated method stub
declare.declare(newFields("word","total"));}}