Our word count topology (depicted in the following diagram) will consist of a single spout connected to three downstream bolts.
The SentenceSpout
class will simply emit a stream of single-value tuples with the key name "sentence"
and a string value (a sentence), as shown in the following code:
{ "sentence":"my dog has fleas" }
To keep things simple, the source of our data will be a static list of sentences that we loop over, emitting a tuple for every sentence. In a real-world application, a spout would typically connect to a dynamic source, such as tweets retrieved from the Twitter API.
The split sentence bolt will subscribe to the sentence spout's tuple stream. For each tuple received, it will look up the "sentence"
object's value, split the value into words, and emit a tuple for each word:
{ "word" : "my" } { "word" : "dog" } { "word" : "has" } { "word" : "fleas" }
The word count bolt subscribes to the output of the SplitSentenceBolt
class, keeping a running count of how many times it has seen a particular word. Whenever it receives a tuple, it will increment the counter associated with a word and emit a tuple containing the word and the current count:
{ "word" : "dog", "count" : 5 }