Apache Storm - 有状态的bolt - 调度方法运行

时间:2018-02-21 22:13:29

标签: methods apache-storm scheduling

将状态填充的apache风暴插入拓扑中,让我们说它看起来如下:

 public class WordCountBolt extends BaseStatefulBolt<KeyValueState<String, Long>> {
     private KeyValueState<String, Long> wordCounts;
     private OutputCollector collector;
 ...
     @Override
     public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
       this.collector = collector;
     }
     @Override
     public void initState(KeyValueState<String, Long> state) {
       wordCounts = state;
     }
     @Override
     public void execute(Tuple tuple) {
       String word = tuple.getString(0);
       Integer count = wordCounts.get(word, 0);
       count++;
       wordCounts.put(word, count);
       collector.emit(tuple, new Values(word, count));
       collector.ack(tuple);
     }
 ...
 }

我需要触发一个方法来更新状态(wordCounts),让我们说每X秒,而不是接收事件。这在Apache Storm statefull bolt中是否可行?是否可以简单地安排这样的方法重复运行在定义的时间间隔内?

public void updateState() {
      wordCounts.put("NewKey", 1);
}

1 个答案:

答案 0 :(得分:1)

我不确定我是否需要定期更新状态,但如果您需要经常运行一些代码,则tick元组可能对您有用。 https://kitmenke.com/blog/2014/08/04/tick-tuples-within-storm/