3、SpringBoot集成Storm WorldCount

RandomSentenceSpout

//数据源,在已知的英文句子中,随机发送一条句子出去。
public class RandomSentenceSpout extends BaseRichSpout {

    //用来收集Spout输出的tuple
    private SpoutOutputCollector collector;
    private Random random;


    //该方法调用一次,主要由storm框架传入SpoutOutputCollector
    @Override
    public void open(Map map, TopologyContext topologyContext, SpoutOutputCollector spoutOutputCollector) {
        this.collector = collector;
        random = new Random();
        //连接kafka mysql ,打开本地文件
    }

    /**
     * 上帝之手
     * while(true)
     *      spout.nextTuple()
     */
    @Override
    public void nextTuple() {
        String[] sentences = new String[]{
                "the cow jumped over the moon","the dog jumped over the moon",
                "the pig jumped over the gun","the fish jumped over the moon","the duck jumped over the moon",
                "the man jumped over the sun","the girl jumped over the sun","the boy jumped over the sun"
        };
        String sentence = sentences[random.nextInt(sentences.length)];

        collector.emit(new Values(sentence));

        System.out.println("RandomSentenceSpout 发送数据:"+sentence);
    }

    //消息源可以发射多条消息流stream
    @Override
    public void declareOutputFields(OutputFieldsDeclarer outputFieldsDeclarer) {
        outputFieldsDeclarer.declare(new Fields("sentence"));
    }
}

原文地址:https://www.cnblogs.com/xidianzxm/p/10751382.html