storm自带例子详解 (一)——WordCountTopologyNode

来源:互联网 发布:软件著作权代理 编辑:程序博客网 时间:2024/06/06 13:21
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements.  See the NOTICE file * distributed with this work for additional information * regarding copyright ownership.  The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License.  You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package storm.starter;import backtype.storm.Config;import backtype.storm.LocalCluster;import backtype.storm.StormSubmitter;import backtype.storm.spout.ShellSpout;import backtype.storm.task.ShellBolt;import backtype.storm.topology.*;import backtype.storm.topology.base.BaseBasicBolt;import backtype.storm.tuple.Fields;import backtype.storm.tuple.Tuple;import backtype.storm.tuple.Values;import java.util.HashMap;import java.util.Map;/** * This topology demonstrates Storm's stream groupings and multilang capabilities. */ /*** 一个使用了多语言机制的storm——WordCount*/public class WordCountTopologyNode {// 定义一个Bolt,继承自ShellBolt,用ShellBolt来实现多语言机制(使用js实现了Bolt)public static class SplitSentence extends ShellBolt implements IRichBolt {// 构造函数public SplitSentence() {// 调用父类的构造函数——父类的构造函数将splitsentence.js作为Boltsuper("node", "splitsentence.js");}@Overridepublic void declareOutputFields(OutputFieldsDeclarer declarer) {// 定义个字段worddeclarer.declare(new Fields("word"));}@Overridepublic Map<String, Object> getComponentConfiguration() {return null;}}// 定义另外一个喷口——也是使用了多语言机制(使用js实现了Spout)    public static class RandomSentence extends ShellSpout implements IRichSpout {        public RandomSentence() {// 同样的使用randomsentence.js作为实际的Spout            super("node", "randomsentence.js");        }        @Override        public void declareOutputFields(OutputFieldsDeclarer declarer) {// 定义一个字段word            declarer.declare(new Fields("word"));        }        @Override        public Map<String, Object> getComponentConfiguration() {            return null;        }    }// 定义一个Boltpublic static class WordCount extends BaseBasicBolt {Map<String, Integer> counts = new HashMap<String, Integer>();@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);// 发射单词和对应的计数(字段分别是word和count)collector.emit(new Values(word, count));}@Overridepublic void declareOutputFields(OutputFieldsDeclarer declarer) {// 定义两个字段word和countdeclarer.declare(new Fields("word", "count"));}  }  public static void main(String[] args) throws Exception {// 创建一个拓扑    TopologyBuilder builder = new TopologyBuilder();// 设置Spout    builder.setSpout("spout", new RandomSentence(), 5);// 设置Bolt——split    builder.setBolt("split", new SplitSentence(), 8).shuffleGrouping("spout");// 设置Bolt——count    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.submitTopologyWithProgressBar(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();    }  }}

0 0