Streaming with Storm – simple example with HDFS bolt

Spout

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 RandomWordSpout.java.

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;

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

 @Override
  public void nextTuple() {
    Utils.sleep(100);
    String[] words = new String[]{
      "Hortonworks",
      "MapR",
      "Cloudera",
      "Storm",
      "Hadoop",
      "Kafka",
      "Spark"
      };

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

  @Override
  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:

WordPress.com Logo

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

Google photo

You are commenting using your Google 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