Storm本地模式下的DRPC開發
阿新 • • 發佈:2018-12-16
根據官方文件Local DRPC模式開發可以很容易的寫出程式碼
下面是我執行過的程式碼:
package cn.ysjh.drpc; import org.apache.storm.Config; import org.apache.storm.LocalCluster; import org.apache.storm.LocalDRPC; import org.apache.storm.drpc.LinearDRPCTopologyBuilder; import org.apache.storm.task.OutputCollector; import org.apache.storm.task.TopologyContext; import org.apache.storm.topology.OutputFieldsDeclarer; import org.apache.storm.topology.base.BaseRichBolt; import org.apache.storm.tuple.Fields; import org.apache.storm.tuple.Tuple; import org.apache.storm.tuple.Values; import java.util.Map; public class LocalDRPCStorm { public static class MyBolt extends BaseRichBolt{ private OutputCollector outputCollector; @Override public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) { this.outputCollector=outputCollector; } @Override public void execute(Tuple tuple) { Object value = tuple.getValue(0); String name = tuple.getString(1); String result="My name:"+name; this.outputCollector.emit(new Values(value,result)); } @Override public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) { outputFieldsDeclarer.declare(new Fields("id","result")); } } public static void main(String[] ages){ LinearDRPCTopologyBuilder builder = new LinearDRPCTopologyBuilder("addUser"); builder.addBolt(new MyBolt()); LocalDRPC drpc = new LocalDRPC(); LocalCluster cluster = new LocalCluster(); cluster.submitTopology("localDRPC",new Config(),builder.createLocalTopology(drpc)); String execute = drpc.execute("addUser", "ysjh"); System.err.println("成功"+execute); cluster.shutdown(); drpc.shutdown(); } }
執行結果:
如果執行程式碼後找不到結果,可以使用Debug模式打個斷點