Many bolts in storm have one of the most common processing steps:
- Read a tuple;
- Based on this input tuple, after extraction, 0, 1 or more tuple are emitted;
- Finally, the ACK operation is used to confirm that the tuple is successfully processed.
Follow these steps to process the tuple tuples sent to the bolt in sequence.
This mode can implement simple functions or filter functions such as ETL. Storm encapsulates the corresponding interface ibasicbolt for this mode. Basebasicbolt and other classes implement this interface.
The following is based on basebasicbolt, which implements word frequency statistics based on the above pattern (CodeReference link: Storm-starter ):
Public Static Class Wordcount Extends Basebasicbolt { // Record the number of times each word and word appears Map <string, integer> counts =New Hashmap <string, integer> (); @ Override Public Void Execute (tuple, basicoutputcollector collector) {string word = Tuple. getstring (0 ); Integer count = Counts. Get (Word ); // Number of times a word appears If (COUNT = Null ) Count = 0 ; Count ++; Counts. Put (word, count ); // Number of times a word appears to be updated Collector. emit ( New Values (word, count )); // Emission Statistics } @ Override Public Void Declareoutputfields (outputfieldsdeclarer declarer) {declarer. Declare ( New Fields ("Word", "Count" ));}}