storm trident实战 filter,function的使用

来源:互联网 发布:企业网站源码带手机版 编辑:程序博客网 时间:2024/06/02 10:07

一、storm trident filter

      Filter通过返回true,和false。来判断是否对信息过滤

     1.1 代码

 

Java代码  收藏代码
  1. public static void main(String[] args) throws InterruptedException,  
  2.         AlreadyAliveException, InvalidTopologyException,  
  3.         AuthorizationException {  
  4.     FixedBatchSpout spout = new FixedBatchSpout(new Fields("a""b"),  
  5.             1new Values(12), new Values(41),  
  6.             new Values(30));  
  7.     spout.setCycle(false);  
  8.     TridentTopology topology = new TridentTopology();  
  9.     topology.newStream("spout", spout)  
  10.             .each(new Fields("a"), new MyFilter())  
  11.             .each(new Fields("a""b"), new PrintFilterBolt(),new Fields(""));  
  12.     Config config = new Config();  
  13.     config.setNumWorkers(2);  
  14.     config.setNumAckers(1);  
  15.     config.setDebug(false);  
  16.     StormSubmitter.submitTopology("trident_filter", config,  
  17.             topology.build());  
  18. }  

    MyFilter:

Java代码  收藏代码
  1. import org.apache.storm.trident.operation.BaseFilter;  
  2. import org.apache.storm.trident.tuple.TridentTuple;  
  3.   
  4. public class MyFilter extends BaseFilter {  
  5.   
  6.     /** 
  7.      *  
  8.      */  
  9.     private static final long serialVersionUID = 1L;  
  10.   
  11.     @Override  
  12.     public boolean isKeep(TridentTuple tuple) {  
  13.         return tuple.getInteger(0) == 1;  
  14.     }  
  15.       
  16. }  

    PrintFilterBolt:

Java代码  收藏代码
  1. public class PrintFilterBolt extends BaseFunction {  
  2.     /** 
  3.      *  
  4.      */  
  5.     private static final long serialVersionUID = 1L;  
  6.   
  7.     @Override  
  8.     public void execute(TridentTuple tuple, TridentCollector collector) {  
  9.         int firstIndex = tuple.getInteger(0);  
  10.         int secondIndex = tuple.getInteger(1);  
  11.         List<Integer> list = new ArrayList<Integer>();  
  12.         list.add(firstIndex);  
  13.         list.add(secondIndex);  
  14.         System.out.println("after storm filter opertition change is : "  
  15.                 + list.toString());  
  16.     }  
  17.   
  18. }  

   运行结果:

Java代码  收藏代码
  1. 2016-12-22 13:16:09.079 o.a.s.s.o.a.c.f.s.ConnectionStateManager [INFO] State change: CONNECTED  
  2. 2016-12-22 13:16:09.088 o.a.s.d.executor [INFO] Prepared bolt $spoutcoord-spout-spout:(2)  
  3. 2016-12-22 13:16:09.736 STDIO [INFO] after storm filter opertition change is : [12]  

二、Storm trident function

       函数的作用是接收一个tuple(需指定接收tuple的哪个字段),输出0个或多个tuples。输出的新字段值会被追加到原始输入tuple的后面, 如果一个function不输出tuple,那就意味这这个tuple被过滤掉了。

    2.1 代码 

Java代码  收藏代码
  1. public static void main(String[] args) throws InterruptedException,  
  2.             AlreadyAliveException, InvalidTopologyException,  
  3.             AuthorizationException {  
  4.   
  5.         FixedBatchSpout spout = new FixedBatchSpout(new Fields("a""b""c"),  
  6.                 1new Values(123), new Values(416),  
  7.                 new Values(308));  
  8.         spout.setCycle(false);  
  9.         TridentTopology topology = new TridentTopology();  
  10.         topology.newStream("spout", spout)  
  11.                 .each(new Fields("b"), new MyFunction(), new Fields("d"))  
  12.                 .each(new Fields("a""b""c""d"), new PrintFunctionBolt(),  
  13.                         new Fields(""));  
  14.         Config config = new Config();  
  15.         config.setNumWorkers(2);  
  16.         config.setNumAckers(1);  
  17.         config.setDebug(false);  
  18.         StormSubmitter.submitTopology("trident_function", config,  
  19.                 topology.build());  
  20.     }  
  21.       

   MyFunction:

  

Java代码  收藏代码
  1. public class MyFunction extends BaseFunction {  
  2.   
  3.     /** 
  4.      *  
  5.      */  
  6.     private static final long serialVersionUID = 1L;  
  7.       
  8.     public void execute(TridentTuple tuple, TridentCollector collector) {  
  9.         for(int i=0; i < tuple.getInteger(0); i++) {  
  10.             collector.emit(new Values(i));  
  11.         }  
  12.     }  
  13.   
  14. }  

   PrintFunctionBolt:

  伦理片 http://www.dotdy.com/  

Java代码  收藏代码
  1. public class PrintFunctionBolt extends BaseFunction {  
  2.     /** 
  3.      *  
  4.      */  
  5.     private static final long serialVersionUID = 1L;  
  6.   
  7.     @Override  
  8.     public void execute(TridentTuple tuple, TridentCollector collector) {  
  9.         int firstIndex = tuple.getInteger(0);  
  10.         int secondIndex = tuple.getInteger(1);  
  11.         int threeIndex = tuple.getInteger(2);  
  12.         int fourIndex = tuple.getInteger(3);  
  13.         List<Integer> list = new ArrayList<Integer>();  
  14.         list.add(firstIndex);  
  15.         list.add(secondIndex);  
  16.         list.add(threeIndex);  
  17.         list.add(fourIndex);  
  18.         System.out.println("after storm function opertition change is : " +list.toString());  
  19.     }  
  20.   
  21. }  

    运行效果:

Java代码  收藏代码
  1. 2016-12-22 13:22:34.365 o.a.s.s.o.a.z.ClientCnxn [INFO] Session establishment complete on server 192.168.80.130/192.168.80.130:2181, sessionid = 0x159285f1109000c, negotiated timeout = 20000  
  2. 2016-12-22 13:22:34.366 o.a.s.s.o.a.c.f.s.ConnectionStateManager [INFO] State change: CONNECTED  
  3. 2016-12-22 13:22:34.374 o.a.s.d.executor [INFO] Prepared bolt $spoutcoord-spout-spout:(2)  
  4. 2016-12-22 13:22:34.415 STDIO [INFO] after storm function opertition change is : [1230]  
  5. 2016-12-22 13:22:34.415 STDIO [INFO] after storm function opertition change is : [1231]  
  6. 2016-12-22 13:22:34.442 STDIO [INFO] after storm function opertition change is : [4160]  

 

0 0
原创粉丝点击