/**
* Random Sentence Spout is a spout which generates tuples of sentences randomly.
*
* @author Tarun Sharma
* @version 1.0
* @see <a href="http://www.dream-lab.in/">DREAM:Lab</a>
* <p/>
* Copyright 2014 DREAM:Lab, Indian Institute of Science, 2014
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package mypackage.spouts;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.Random;
public class RandomSentenceSpout extends BaseRichSpout {
/**
*
*/
private static final long serialVersionUID = 3747139112340179001L;
private static Logger LOG = LoggerFactory.getLogger(RandomSentenceSpout.class);
SpoutOutputCollector collector;
Random r;
long myRandomMsgId;
@Override
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
this.collector = collector;
this.r = new Random();
}
@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[r.nextInt(sentences.length)];
//this.collector.emit(new Values(sentence));
//long myMessage = Long.decode("0xfe00000000000000");
myRandomMsgId++;
if (myRandomMsgId > 5) myRandomMsgId = 1;
LOG.info("The value of in RandomSentenceSpout is " + myRandomMsgId);
//this.collector.emit(new Values(sentence), myRandomMsgId);
this.collector.emit(new Values(sentence), myRandomMsgId);
}
@Override
public void ack(Object id) {
LOG.info("The ACK value of in RandomSentenceSpout is " + id);
}
@Override
public void fail(Object id) {
}
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("word"));
}
}