|
| 1 | +package com.msj.storm.trident.example; |
| 2 | + |
| 3 | +import storm.trident.Stream; |
| 4 | +import storm.trident.TridentTopology; |
| 5 | +import storm.trident.operation.BaseFunction; |
| 6 | +import storm.trident.operation.TridentCollector; |
| 7 | +import storm.trident.testing.FixedBatchSpout; |
| 8 | +import storm.trident.tuple.TridentTuple; |
| 9 | +import backtype.storm.Config; |
| 10 | +import backtype.storm.LocalCluster; |
| 11 | +import backtype.storm.StormSubmitter; |
| 12 | +import backtype.storm.generated.StormTopology; |
| 13 | +import backtype.storm.tuple.Fields; |
| 14 | +import backtype.storm.tuple.Values; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Vincent.M mengshaojie@188.com |
| 18 | + * @date 2017/5/11 下午2:57 |
| 19 | + * @copyright ©2017 孟少杰 All Rights Reserved |
| 20 | + * @desc 测试TridentFunction |
| 21 | + */ |
| 22 | +public class TridentFunction { |
| 23 | + |
| 24 | + //继承BaseFunction类,重新execute方法 |
| 25 | + public static class SumFunction extends BaseFunction { |
| 26 | + @Override |
| 27 | + public void execute(TridentTuple tuple, TridentCollector collector) { |
| 28 | + System.out.println("传入进来的内容为:" + tuple); |
| 29 | + //获取a、b俩个域 |
| 30 | + int a = tuple.getInteger(0); |
| 31 | + int b = tuple.getInteger(1); |
| 32 | + int sum = a + b; |
| 33 | + //发射数据 |
| 34 | + collector.emit(new Values(sum)); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + //继承BaseFunction类,重新execute方法 |
| 39 | + public static class Result extends BaseFunction { |
| 40 | + @Override |
| 41 | + public void execute(TridentTuple tuple, TridentCollector collector) { |
| 42 | + //获取tuple输入内容 |
| 43 | + System.out.println(); |
| 44 | + Integer a = tuple.getIntegerByField("a"); |
| 45 | + Integer b = tuple.getIntegerByField("b"); |
| 46 | + Integer c = tuple.getIntegerByField("c"); |
| 47 | + Integer d = tuple.getIntegerByField("d"); |
| 48 | + System.out.println("a: "+ a + ", b: " + b + ", c: " + c + ", d: " + d); |
| 49 | + Integer sum = tuple.getIntegerByField("sum"); |
| 50 | + System.out.println("sum: "+ sum); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static StormTopology buildTopology() { |
| 55 | + TridentTopology topology = new TridentTopology(); |
| 56 | + //设定数据源 |
| 57 | + FixedBatchSpout spout = new FixedBatchSpout( |
| 58 | + new Fields("a", "b", "c", "d"), //声明输入的域字段为"a"、"b"、"c"、"d" |
| 59 | + 4, //设置批处理大小为1 |
| 60 | + //设置数据源内容 |
| 61 | + //测试数据 |
| 62 | + new Values(1, 4, 7, 10), |
| 63 | + new Values(1, 1, 3, 11), |
| 64 | + new Values(2, 2, 7, 1), |
| 65 | + new Values(2, 5, 7, 2)); |
| 66 | + //指定是否循环 |
| 67 | + spout.setCycle(false); |
| 68 | + //指定输入源spout |
| 69 | + Stream inputStream = topology.newStream("spout", spout); |
| 70 | + /** |
| 71 | + * 要实现流sqout - bolt的模式 在trident里是使用each来做的 |
| 72 | + * each方法参数: |
| 73 | + * 1.输入数据源参数名称:"a", "b", "c", "d" |
| 74 | + * 2.需要流转执行的function对象(也就是bolt对象):new SumFunction() |
| 75 | + * 3.指定function对象里的输出参数名称:sum |
| 76 | + */ |
| 77 | + inputStream.each(new Fields("a", "b", "c", "d"), new SumFunction(), new Fields("sum")) |
| 78 | + /** |
| 79 | + * 继续使用each调用下一个function(bolt) |
| 80 | + * 第一个输入参数为:"a", "b", "c", "d", "sum" |
| 81 | + * 第二个参数为:new Result() 也就是执行函数,第三个参数为没有输出 |
| 82 | + */ |
| 83 | + .each(new Fields("a", "b", "c", "d", "sum"), new Result(), new Fields()); |
| 84 | + return topology.build(); //利用这种方式,我们返回一个StormTopology对象,进行提交 |
| 85 | + } |
| 86 | + |
| 87 | + public static void main(String[] args) throws Exception { |
| 88 | + |
| 89 | + Config conf = new Config(); |
| 90 | + //设置batch最大处理 |
| 91 | + conf.setNumWorkers(2); |
| 92 | + conf.setMaxSpoutPending(20); |
| 93 | + if(args.length == 0) { |
| 94 | + LocalCluster cluster = new LocalCluster(); |
| 95 | + cluster.submitTopology("trident-function", conf, buildTopology()); |
| 96 | + Thread.sleep(10000); |
| 97 | + cluster.shutdown(); |
| 98 | + } else { |
| 99 | + StormSubmitter.submitTopology(args[0], conf, buildTopology()); |
| 100 | + } |
| 101 | + |
| 102 | + } |
| 103 | +} |
0 commit comments