package storm.demo;
import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;
import java.util.Map;
import java.util.Random;
publicclassWordCountextendsBaseRichSpout{private SpoutOutputCollector collector;
//自己封装一个数据privatestaticfinal String[] msgs = new String[]{
"I have a dream",
"My dream is to be a data analyst",
"So many people dream about better life",
"better job, doing more and achieving more",
"creating new things",
"keep on rolling the routine",
"Our brains articulate our visions but it is ONLY our hearts that can make it happen",
"We all dream"
};
privatestaticfinal Random random = new Random();
@Overridepublicvoidopen(Map map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) {
this.collector = spoutOutputCollector;
}
@OverridepublicvoidnextTuple() {
String sentence =msgs[random.nextInt(8)];
collector.emit(new Values(sentence));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@OverridepublicvoiddeclareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
//给输出的字段指定索引
outputFieldsDeclarer.declare(new Fields("sentence"));
}
}