我正在和Kafka Streams一起玩,在调查WordCountProcessorDemo
时,我意识到我一定缺少部分图片。即,该库如何保证在下面的代码中不会发生脏读:
@Override
public void process(final String dummy, final String line) {
final String[] words = line.toLowerCase(Locale.getDefault()).split(" ");
for (final String word : words) {
final Integer oldValue = this.kvStore.get(word);
if (oldValue == null) {
this.kvStore.put(word, 1);
} else {
this.kvStore.put(word, oldValue + 1);
}
}
context.commit();
}
据我所知,在触发kvStore.get(..)
之后,状态可能会被另一个StreamProcessor实例更改,该实例位于其他使用不同分区的计算机上。因此,由于我们执行了脏读,因此状态将变得不一致。
Kafka Streams是否以某种方式处理这种情况?
答案 0 :(得分:1)
状态可能会被另一个StreamProcessor实例更改
不是。状态是分片的,因此每个Processor
都有自己在整体状态中的专有份额。