storm trident groupby


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import backtype.storm.Config;
import backtype.storm.LocalCluster;
import backtype.storm.generated.StormTopology;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Values;
import storm.trident.TridentTopology;
import storm.trident.operation.BaseAggregator;
import storm.trident.operation.BaseFilter;
import storm.trident.operation.BaseFunction;
import storm.trident.operation.Filter;
import storm.trident.operation.TridentCollector;
import storm.trident.testing.FixedBatchSpout;
import storm.trident.tuple.TridentTuple;


public class TridentLocalPologyGroupBy {
public static class SumBolt extends BaseFunction{
@Override
public void execute(TridentTuple tuple, TridentCollector collector) {
String value = tuple.getString(0);
System.err.println("value="+value);



}

}


public static class MyAgg extends BaseAggregator<Map<String, Integer>>{


@Override
public void aggregate(Map<String, Integer> val, TridentTuple tuple,
TridentCollector collector) {

String word = tuple.getString(0);
Integer value = val.get(word);
if(value==null){
value=0;
}
value++;
//把数据保存到一个map对象中
val.put(word, value);
//把结果写出去
System.err.println("===============================");
for (Entry<String, Integer> entry : val.entrySet()) {
System.err.println(entry);
}



}


@Override
public void complete(Map<String, Integer> val, TridentCollector collector) {

collector.emit(new Values(val));
}


@Override
public Map<String, Integer> init(Object arg0, TridentCollector arg1) {

return new HashMap<String, Integer>();
}

}

public static class MyBolt extends BaseFunction{


@Override
public void execute(TridentTuple tuple, TridentCollector collector) {
//因为返回值是map 强制转换为map类型
Map<String, Integer> value = (Map)tuple.getValue(0);
for (Entry<String, Integer> entry : value.entrySet()) {
System.out.println("entry==="+entry);
}
}

}


public static void main(String[] args) {
//输出为new Fields("sentence")
FixedBatchSpout spout = new FixedBatchSpout(new Fields("sentence"), 4, new Values("a"),new Values("b"),new Values("a"),new Values("c"));
//spout.setCycle(true);
spout.setCycle(false);
TridentTopology tridentTopology = new TridentTopology();
tridentTopology.newStream("spout1", spout)
.groupBy(new Fields("sentence"))
.aggregate(new Fields("sentence"), new MyAgg(), new Fields("Map"))
.each( new Fields("Map"), new MyBolt(), new Fields(""));



    LocalCluster localCluster = new LocalCluster();
    localCluster.submitTopology("trident", new Config(), tridentTopology.build());
}

}


执行结果为:

===============================
a=1
===============================
b=1
===============================
a=2
===============================
c=1
entry===b=1
entry===a=2
entry===c=1


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值