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))?
Spouts. A spout is a source of streams in a topology. Generally spouts will read tuples from an external source and emit them into the topology (e.g. a Kestrel queue or the Twitter API). Spouts can either be reliable or unreliable.
The main method in bolts is the execute method which takes in as input a new tuple. Bolts emit new tuples using the OutputCollector object.
open − Provides the spout with an environment to execute. The executors will run this method to initialize the spout.
The tuple is the main data structure in Storm. A tuple is a named list of values, where each value can be any type. Tuples are dynamically typed – the types of the fields do not need to be declared. Tuples have helper methods like getInteger and getString to get field values without having to cast the result.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With