Streaming with Storm – simple example with HDFS bolt


A spout is a source of streams in topology. In this example, the source of stream is a Java method that returns random words from an array of strings.
This file is in the spout folder. The spout file is named

package org.package.spout;

import backtype.storm.spout.SpoutOutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseRichSpout;
import backtype.storm.tuple.Fields;
import backtype.storm.tuple.Values;
import backtype.storm.utils.Utils;

import java.util.Map;
import java.util.Random;

public class RandomWordSpout extends BaseRichSpout {
  SpoutOutputCollector _collector;
  Random _rand;

  public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
    _collector = collector;
    _rand = new Random();

  public void nextTuple() {
    String[] words = new String[]{

    String word = words[_rand.nextInt(words.length)];
    _collector.emit(new Values(word));

  public void declareOutputFields(OutputFieldsDeclarer declarer) {
    declarer.declare(new Fields("word"));


Package it to see if it builds successfully.

mvn package

If everything is ok, the topology file can be written. Go on to the next page.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s