Updated on 2022-08-16 GMT+08:00

Creating a Spout

Function Description

A Spout is a message source of Storm and message producer of Topology. Generally, a message source reads data from an external source and sends messages (Tuple) to Topology.

One message source can send multiple message streams, and therefore, OutputFieldsDeclarer.declarerStream can be used to define multiple streams, and then SpoutOutputCollector emits specific streams.

Code Sample

The following code snippet belongs to the nextTuple method in the RandomSentenceSpout class of the com.huawei.storm.example.common package, and these code snippets are used to split strings into words.

/** 
  * {@inheritDoc} 
  */ 
 @Override 
 public void nextTuple() 
 { 
     Utils.sleep(100); 
     String[] sentences = 
         new String[] {"the cow jumped over the moon",  
                       "an apple a day keeps the doctor away", 
                       "four score and seven years ago",  
                       "snow white and the seven dwarfs",  
                       "i am at two with nature"}; 
     String sentence = sentences[random.nextInt(sentences.length)]; 
     collector.emit(new Values(sentence)); 
 }