【Storm 入门】 Blot分流

时间:2021-01-24 07:33:59

场景介绍

这是一个很普通的使用场景,spout 发送数据至blot1,blot1根据数据分类发送至blotA 和 blotB

Spout->Blot1
Blot1->BlotA
Blot1->BlotB

代码介绍

  1. 在总blot中声明几个流streamId,在execute中emit中指定发送的流
  2. 在Topology中,指定blot读取哪个streamId的stream
# in Blot1.java
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declareStream("blota", new Fields("afield"));
declarer.declareStream("blotb", new Fields("bfield"));
}
public void execute(Tuple tuple) {
int t = (int)tuple.getValue(0);
if(t%2==0)
_collector.emit("blota", new Values(t));
else
_collector.emit("blotb", new Values(t));
}
# in Topology
public static void main(String[] args) throws Exception {
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("num", new NumSpout(), 10);
builder.setBolt("blot1", new RouterBolt(), 3).shuffleGrouping("num");
builder.setBolt("b1", new Bolt1(), 3).shuffleGrouping("blot1","blota");
builder.setBolt("b2", new Bolt2(), 2).shuffleGrouping("blot1","blotb");
}