Toshihiko
Toshihiko

Reputation: 317

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))?

Upvotes: 5

Views: 3081

Answers (1)

fhussonnois
fhussonnois

Reputation: 1727

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()

Upvotes: 10

Related Questions