Storm Trident示例function, filter, projection

以下代码演示function, filter, projection的使用,可结合注释

省略部分代码,省略部分可参考:https://blog.csdn.net/nickta/article/details/79666918

FixedBatchSpout spout = new FixedBatchSpout(new Fields("user", "score"), 3,  
                new Values("nickt1", 4), 
                new Values("nickt2", 7),  
                new Values("nickt3", 8), 
                new Values("nickt4", 9),  
                new Values("nickt5", 7), 
                new Values("nickt6", 11), 
                new Values("nickt7", 5) 
                ); 
        spout.setCycle(false); 
        TridentTopology topology = new TridentTopology(); 
        topology.newStream("spout1", spout) 
                .shuffle() 
                .each(new Fields("user"),new BaseFilter() {
                    @Override
                    public boolean isKeep(TridentTuple tuple) {
                        if(tuple.getString(0).equals("nickt2")) {
                            return false;
                        }
                        return true;
                    }
                })//过滤点user为nickt2的tuple
                .each(new Fields("user", "score"),new Debug("filter print:"))
                .each(new Fields("score"), new BaseFunction() {
                    
                    @Override
                    public void execute(TridentTuple tuple, TridentCollector collector) {
                        collector.emit(new Values(tuple.getIntegerByField("score") + 100));
                    }
                }, new Fields("sum"))//把score加上100后,生成新的sum字段,并追加到原字段后面,此步操作后就包括了user/score/sum三个字段
                .each(new Fields("user", "score", "sum"),new Debug("function print:"))
                .project(new Fields("user"))
                .each(new Fields("user"),new Debug("project print:"));//project投射之后,只有user字段了
                   

输出:

<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(filter print:): [nickt1, 4]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(function print:): [nickt1, 4, 104]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(project print:): [nickt1]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(filter print:): [nickt3, 8]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(function print:): [nickt3, 8, 108]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(project print:): [nickt3]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(filter print:): [nickt4, 9]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(function print:): [nickt4, 9, 109]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(project print:): [nickt4]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(filter print:): [nickt5, 7]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(function print:): [nickt5, 7, 107]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(project print:): [nickt5]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(filter print:): [nickt6, 11]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(function print:): [nickt6, 11, 111]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(project print:): [nickt6]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(filter print:): [nickt7, 5]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(function print:): [nickt7, 5, 105]
<Sat Mar 24 13:41:42 CST 2018[partition0-Thread-68-b-0-executor[33 33]]> DEBUG(project print:): [nickt7]

转载于:https://www.cnblogs.com/nickt/p/8638663.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值