Methods apachestorm-statefulbolt-scheduling方法运行
将一个状态完整的apache storm bolt插入到拓扑中,假设如下所示:Methods apachestorm-statefulbolt-scheduling方法运行,methods,apache-storm,scheduling,Methods,Apache Storm,Scheduling,将一个状态完整的apache storm bolt插入到拓扑中,假设如下所示: public class WordCountBolt extends BaseStatefulBolt<KeyValueState<String, Long>> { private KeyValueState<String, Long> wordCounts; private OutputCollector collector; ... @Over
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);
}
...
}
我不知道为什么需要定期更新状态,但是如果需要经常运行一些代码,勾选元组可能适合您 嘿,谢谢你的回答。我的用例是每X分钟/预定义的时间间隔重新计算模型值。因此,我的螺栓包含一个模型,我希望执行一个逻辑,该逻辑每次更新模型,例如30分钟。你知道一个更好的方法来实现这一点吗?不,勾选元组应该可以。唯一需要注意的是,如果螺栓落在后面(许多元组排队),则tick元组可能会延迟一点。如果这是不可接受的,您可以从另一个线程执行模型更新。
public void updateState() {
wordCounts.put("NewKey", 1);
}