storm 的一个drpc例子(值得一看)
来源:互联网 发布:秦舞阳 知乎 编辑:程序博客网 时间:2024/06/05 15:18
2012-11-26 18:461465人阅读评论(0)收藏举报
- public staticvoid initConnectDB(){
- primaryKey = "id";
- rdbmsUrl = "jdbc:mysql://hadoop/DB" ;
- rdbmsUserName = "";
- rdbmsPassword = "";
- connector = new RDBMSConnector();
- try {
- con = connector.getConnection(rdbmsUrl, rdbmsUserName, rdbmsPassword);
- communicator = new RDBMSCommunicator2UFN(con);
- } catch (Exception e){
- System.out.println("connect to db exception in initConnectDB()");
- e.printStackTrace();
- }
- }
- public staticclass GetUserID extends BaseBasicBolt{
- //private RDBMSCommunicator communicator = null;
- private ResultSet rs =null;
- @Override
- public void prepare(Map stormConf, TopologyContext context) {
- System.out.println("in prepare con : "+con);
- //this.communicator = new RDBMSCommunicator(con);
- System.out.println("in pretpare communicator :"+communicator);
- }
- public void execute(Tuple input, BasicOutputCollector collector) {
- Object id = input.getValue(0);
- String userName = input.getString(1);
- String sql = String.format("select userID from usersinfo where username='%s'", userName);
- System.out.println("sql in get-user-id: "+sql);
- rs = communicator.selecteExec(sql);
- String userID = null;
- if (rs != null){
- try {
- rs.next();
- userID = rs.getString("userID");
- } catch (Exception e){
- e.printStackTrace();
- }
- collector.emit(new Values(id, userID));
- }
- }
- public void declareOutputFields(OutputFieldsDeclarer declarer) {
- declarer.declare(new Fields("id","userID"));
- }
- }
- public staticclass GetUserFunctionsID extends BaseBasicBolt{
- //private RDBMSCommunicator communicator = null;
- private ResultSet rs =null;
- @Override
- public void prepare(Map stormConf, TopologyContext context) {
- //communicator = new RDBMSCommunicator(con);
- }
- public void execute(Tuple input, BasicOutputCollector collector) {
- Object id = input.getValue(0);
- String userID = input.getString(1);
- if (userID == null || userID.trim().length() ==0){
- return;
- }
- String sql = String.format("select functionID from userfunctions where userID='%s'", userID);
- System.out.println("sql in get-user-functionid : "+sql);
- rs = communicator.selecteExec(sql);
- String functionID = null;
- if (rs != null){
- try {
- while(rs.next()){
- functionID = rs.getString("functionID");
- collector.emit(new Values(id,functionID));
- }
- } catch(Exception e){
- e.printStackTrace();
- }
- }
- }
- public void declareOutputFields(OutputFieldsDeclarer declarer) {
- declarer.declare(new Fields("id","functionID"));
- }
- }
- public staticclass GetUserFunctionsName extends BaseBatchBolt{
- //private RDBMSCommunicator communicator = null;
- private ResultSet rs = null;
- List<String> functionsName = new ArrayList<String>();
- BatchOutputCollector _collector;
- Object _id;
- public void execute(Tuple tuple) {
- String functionID = tuple.getString(1);
- if (functionID ==null || functionID.trim().length() == 0){
- return ;
- }
- String sql = String.format("select functionName from functionsinfo where functionID='%s'",functionID);
- System.out.println("sql in get-user-functionname : "+sql );
- rs = communicator.selecteExec(sql);
- String functionName = null;
- if(rs != null){
- try {
- rs.next();
- functionName = rs.getString("functionName");
- functionsName.add(functionName);
- } catch (Exception e){
- e.printStackTrace();
- }
- }
- }
- public void finishBatch() {
- _collector.emit(new Values(_id,functionsName.toString()));
- }
- public void prepare(Map conf, TopologyContext context,
- BatchOutputCollector collector, Object id) {
- _collector = collector;
- _id = id;
- }
- public void declareOutputFields(OutputFieldsDeclarer declarer) {
- declarer.declare(new Fields("id","user-funcions-name"));
- }
- }
- public static LinearDRPCTopologyBuilder construct(){
- initConnectDB();
- LinearDRPCTopologyBuilder builder = new LinearDRPCTopologyBuilder("user-functions-name");
- builder.addBolt(new GetUserID(),2);
- builder.addBolt(new GetUserFunctionsID(),2).shuffleGrouping();
- builder.addBolt(new GetUserFunctionsName(),2).fieldsGrouping(new Fields("id","functionID"));
- return builder;
- }
- public staticvoid main(String[] args) throws Exception{
- LinearDRPCTopologyBuilder builder = construct();
- Config conf = new Config();
- if(args==null || args.length==0) {
- conf.setMaxTaskParallelism(3);
- LocalDRPC drpc = new LocalDRPC();
- LocalCluster cluster = new LocalCluster();
- cluster.submitTopology("user-fn-drpc", conf, builder.createLocalTopology(drpc));
- String[] userNames = new String[] {"qingwu.fu"};
- for(String un: userNames) {
- System.out.println("Functions name of : " + un +": " + drpc.execute("user-functions-name", un));
- }
- cluster.shutdown();
- drpc.shutdown();
- } else {
- conf.setNumWorkers(6);
- StormSubmitter.submitTopology(args[0], conf, builder.createRemoteTopology());
- }
- }
转载:http://blog.csdn.net/baiyangfu_love/article/details/8227085
0 0
- storm 的一个drpc例子(值得一看)
- storm 的一个drpc例子
- storm DRPC例子
- STORM的DRPC通讯
- Storm集群的DRPC模式
- 012-storm的drpc通信
- storm drpc
- Storm DRPC
- Storm DRPC
- Storm DRPC
- storm drpc
- Storm DRPC
- 一个Struts2启动失败的例子,新手值得一看。
- Storm一个Topology中提供drpc和计算功能
- Twitter Storm: DRPC学习
- Twitter Storm: DRPC简介
- Storm DRPC 使用
- Twitter Storm: DRPC简介
- linux gpg 使用笔记
- 降低HWM方法实验move shrink
- React 框架解析
- 九度 题目1122:吃糖果
- 【BST】返回搜索二叉树最大的k个数
- storm 的一个drpc例子(值得一看)
- 8 第八天【健身】
- 1.12-1.17 项目进度
- Java安全通信:HTTPS与SSL
- android颜色配置文件color.xml
- www.ebay.com/cln/9_28532/-/158076451011/2015-01-17
- 关于make: *** No rule to make target""问题总结
- 关于过渡机制一点理解
- Bellman-Ford算法详讲