您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關Storm中DRPC如何使用,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
1. 修改所有節點的 storm.yaml 配置文件,設置 drpc server
[grid@hadoop6 ~]$ vim storm-0.9.4/conf/storm.yaml drpc.servers: - "hadoop4"
2. 啟動drcp服務
storm drpc &
3. 編寫服務端程序
//來源于 https://github.com/nathanmarz/storm-starter/blob/master/src/jvm/storm/starter/BasicDRPCTopology.java package storm.starter; import backtype.storm.Config; import backtype.storm.LocalCluster; import backtype.storm.LocalDRPC; import backtype.storm.StormSubmitter; import backtype.storm.drpc.LinearDRPCTopologyBuilder; import backtype.storm.topology.BasicOutputCollector; import backtype.storm.topology.OutputFieldsDeclarer; import backtype.storm.topology.base.BaseBasicBolt; import backtype.storm.tuple.Fields; import backtype.storm.tuple.Tuple; import backtype.storm.tuple.Values; public class BasicDRPCTopology { public static class ExclaimBolt extends BaseBasicBolt { @Override public void execute(Tuple tuple, BasicOutputCollector collector) { String input = tuple.getString(1); collector.emit(new Values(tuple.getValue(0), input + "!")); } @Override public void declareOutputFields(OutputFieldsDeclarer declarer) { declarer.declare(new Fields("id", "result")); } } public static void main(String[] args) throws Exception { LinearDRPCTopologyBuilder builder = new LinearDRPCTopologyBuilder("exclamation"); builder.addBolt(new ExclaimBolt(), 3); Config conf = new Config(); if (args == null || args.length == 0) { LocalDRPC drpc = new LocalDRPC(); LocalCluster cluster = new LocalCluster(); cluster.submitTopology("drpc-demo", conf, builder.createLocalTopology(drpc)); for (String word : new String[]{"hello", "goodbye"}) { System.out.println("Result for \"" + word + "\": " + drpc.execute("exclamation", word)); } cluster.shutdown(); drpc.shutdown(); } else { conf.setNumWorkers(3); StormSubmitter.submitTopology(args[0], conf, builder.createRemoteTopology()); } } }
4. 將程序打包提交到Storm集群
storm jar Storm04-1.0-SNAPSHOT.jar storm.starter.BasicDRPCTopology exclamation
5. 編寫客戶端程序遠程調用服務
import backtype.storm.utils.DRPCClient; public class Client { public static void main(String[] args) throws Exception { DRPCClient client = new DRPCClient("192.168.0.106", 3772); String[] words = {"hello", "storm", "drpc"}; for (String word : words) { String result = client.execute("exclamation", word); System.out.println("Result for \"" + word + "\": " + result); } } }
6. 程序運行結果:
看完上述內容,你們對Storm中DRPC如何使用有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。