简体   繁体   中英

Sent different tuples from 1 spout to different bolt in Apache Storm

Is it possible to sent different tuples from 1 spout to different bolt in Apache Storm? For instance, I had Spout A, which need to sent out Tuple B to Bolt C and Tuple D to Bolt E. How should I implement it using spout in Java? I mean how to write the code.

OutputCollector.emit(new Values(B, C))?

To emit tuples to different bolts from one Spout you can use named streams as follows :

Spout

@Override
public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
    outputFieldsDeclarer.declareStream("streamA", new Fields("A"));
    outputFieldsDeclarer.declareStream("streamB", new Fields("B"));
}


@Override
public void nextTuple() {
    outputCollector.emit("streamA", new Values("A"));
    outputCollector.emit("streamB", new Values("B"));
}

Then, each bolt subscribes to a specific stream :

builder.setBolt("MyBoltA", new BoltA()).shuffleGrouping("MySpout", "streamA"); 
builder.setBolt("MyBoltB", new BoltB()).shuffleGrouping("MySpout", "streamB");

Finally, if a bolt subscribes to several streams, you can use the following method to know from which stream a tuple has been emitted :

tuple.getSourceStreamId()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM