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

Creating a Storm Spout

Function Description

A Spout is a message source of Storm and message producer of the topology. Generally, a message source reads data from an external source and sends messages (Tuple) to the 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.

Sample Code

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));  
  }