您好,登錄后才能下訂單哦!
這篇文章主要介紹“storm啟動類怎么定義”,在日常操作中,相信很多人在storm啟動類怎么定義問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”storm啟動類怎么定義”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
在storm集群中真正運行Topology的主要有三個實例:工作進程丶線程和任務.
Storm集群中的每臺機器上都可以運行多個工作進程,每個工作進程又可以創建多個線程,每個線程可以執行多個任務.
Storm可靠性:是通過對消息樹給定一個唯一的ID,每送一個消息,都會同步發送一個ack或fail,對于網絡的寬帶會有一定的消耗,如果對于可靠性要求不高,可以通過使用不同的emit接口關閉該模式.
一、storm啟動類的定義。
package com.cmsz.storm.trading.test; import backtype.storm.topology.TopologyBuilder; import backtype.storm.tuple.Fields; public class MainStorm { public static void main(String[] args) throws Exception { TopologyBuilder builder = new TopologyBuilder(); builder.setSpout("A", new ASpout()); builder.setBolt("B", new BBolt()).shuffleGrouping("A", "streamId_B");//componentId和streamId builder.setBolt("C", new CBolt()).shuffleGrouping("A", "streamId_C");//componentId和streamId builder.setBolt("D", new DBolt()).fieldsGrouping("B", new Fields("id"));//componentId和streamId builder.setBolt("E", new EBolt()).fieldsGrouping("C", new Fields("id")); Config conf = new Config(); if (args != null && args.length > 0) { conf.setNumWorkers(1); StormSubmitter.submitTopology(args[0], conf, builder.createTopology()); } else { LocalCluster cluster = new LocalCluster(); cluster.submitTopology("myTopo", conf, builder.createTopology()); } } }
二、spout定義了streamId,接受的bolt要定義componentId與spout中定義的streamId("streamId_B"、"streamId_C")對應定義去接收,fiedsGrouping的new Fields("id")中的id要和componentId的對應bolt中new Fields("id","message")中的"id"對應就會以"id"進行分組
package com.cmsz.storm.trading.test; import java.util.Map; import java.util.Random; 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; public class ASpout extends BaseRichSpout{ SpoutOutputCollector collector; @Override public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) { this.collector = collector; } @Override public void nextTuple() { Utils.sleep(10); final String[] words = new String[] {"B_nathan", "C_mike", "B_jackson", "C_golda", "B_bertels"}; final Random rand = new Random(); final String word = words[rand.nextInt(words.length)]; if(word.indexOf("B_")>-1){ collector.emit("streamId_B",new Values(word)); }else if(word.indexOf("C_")>-1){ collector.emit("streamId_C",new Values(word)); } } @Override public void declareOutputFields(OutputFieldsDeclarer declarer) { declarer.declareStream("streamId_B", new Fields("streamId_B")); declarer.declareStream("streamId_C", new Fields("streamId_C")); } @Override public void ack(Object msgId) { super.ack(msgId); } @Override public void fail(Object msgId) { super.fail(msgId); } }
package com.cmsz.storm.trading.test; import java.util.Map; import backtype.storm.task.TopologyContext; import backtype.storm.topology.BasicOutputCollector; import backtype.storm.topology.IBasicBolt; import backtype.storm.topology.OutputFieldsDeclarer; import backtype.storm.tuple.Fields; import backtype.storm.tuple.Tuple; import backtype.storm.tuple.Values; public class BBolt implements IBasicBolt { @Override public void declareOutputFields(OutputFieldsDeclarer declarer) { declarer.declare(new Fields("id","message")); } @Override public Map<String, Object> getComponentConfiguration() { return null; } @Override public void prepare(Map stormConf, TopologyContext context) { } @Override public void execute(Tuple input, BasicOutputCollector collector) { String msg = input.getString(0); System.out.println(msg); collector.emit(new Values(msg,msg+"BBolt")); } @Override public void cleanup() { } }
package com.cmsz.storm.trading.test; import java.util.Map; import backtype.storm.task.TopologyContext; import backtype.storm.topology.BasicOutputCollector; import backtype.storm.topology.IBasicBolt; import backtype.storm.topology.OutputFieldsDeclarer; import backtype.storm.tuple.Fields; import backtype.storm.tuple.Tuple; import backtype.storm.tuple.Values; public class CBolt implements IBasicBolt { @Override public void declareOutputFields(OutputFieldsDeclarer declarer) { declarer.declare(new Fields("id","message")); } @Override public Map<String, Object> getComponentConfiguration() { return null; } @Override public void prepare(Map stormConf, TopologyContext context) { } @Override public void execute(Tuple input, BasicOutputCollector collector) { String msg = input.getString(0); System.out.println(msg); collector.emit(new Values(msg,msg+"CBolt")); } @Override public void cleanup() { } }
package com.cmsz.storm.trading.test; import java.util.Map; import backtype.storm.task.TopologyContext; import backtype.storm.topology.BasicOutputCollector; import backtype.storm.topology.IBasicBolt; import backtype.storm.topology.OutputFieldsDeclarer; import backtype.storm.tuple.Fields; import backtype.storm.tuple.Tuple; public class DBolt implements IBasicBolt { @Override public void declareOutputFields(OutputFieldsDeclarer declarer) { declarer.declare(new Fields("message")); } @Override public Map<String, Object> getComponentConfiguration() { return null; } @Override public void prepare(Map stormConf, TopologyContext context) { } @Override public void execute(Tuple input, BasicOutputCollector collector) { System.out.println("DBolt"+input.getString(0)); } @Override public void cleanup() { } }
package com.cmsz.storm.trading.test; import java.util.Map; import backtype.storm.task.TopologyContext; import backtype.storm.topology.BasicOutputCollector; import backtype.storm.topology.IBasicBolt; import backtype.storm.topology.OutputFieldsDeclarer; import backtype.storm.tuple.Fields; import backtype.storm.tuple.Tuple; public class EBolt implements IBasicBolt { @Override public void declareOutputFields(OutputFieldsDeclarer declarer) { declarer.declare(new Fields("message")); } @Override public Map<String, Object> getComponentConfiguration() { return null; } @Override public void prepare(Map stormConf, TopologyContext context) { } @Override public void execute(Tuple input, BasicOutputCollector collector) { System.out.println("EBolt"+input.getString(0)); } @Override public void cleanup() { } }
到此,關于“storm啟動類怎么定義”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。