storm_使用streamid的例子

有时同一个spout或者bolt需要发出多类不同的消息。如对一个字串拆分为单词后,将各单词一一发送给各节点,发送完后再发送一条结束的任务。

使用方法如下:

1.Spout的declareOutputFields方法中定义多组stream:

@Override

public void declareOutputFields(OutputFieldsDeclarer declarer) {

declarer.declareStream("working",new Fields("word", "key"));

declarer.declareStream("stop", new Fields("key"));

}

在emit时指定streamid,第一个参数。如

_collector.emit("working", new Values(word, fileName));

_collector.emit("stop", new Values(fileName));

注,是第一个参数,和tupleId不是一回事,tupleId是在values参数后面。

2.Bolt如何使用

public void execute(Tuple tuple) {

String streamId = tuple.getSourceStreamId();

if (streamId.equals("working")){

}

if (streamId.equals("stop")){

}

}

3.Topology中如何定义呢:

在流分组的第二个参数上:

builder.setSpout("wordSpout", new WordSpout(), 4);

builder.setBolt("CountBolt", new CountBolt(), 4)

.fieldsGrouping("wordSpout", "working", new Fields("word"))

.allGrouping("wordSpout", "stop");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值