如何从KafkaStreams PersistentKeyValueStore中删除记录?

时间:2018-01-19 10:04:44

标签: apache-kafka apache-kafka-streams

我把一些记录放到了PersistentKeyValueStore:

Long nowValue = Optional.ofNullable(myStore.get(key)).orElse(0L) + newValue;
myStore.put(key, nowValue);

运行良好。我可以通过println看到记录:

KeyValueIterator<String, Long> iter = myStore.all();
while (iter.hasNext()) {
     KeyValue<String, Long> entry = iter.next();
     System.out.println(entry.value)
}
iter.close();

但是当我尝试删除记录时:

KeyValueIterator<String, Long> iter = myStore.all();
while (iter.hasNext()) {
     iter.remove();
}
iter.close();

它出现异常:

java.lang.UnsupportedOperationException: RocksDB iterator does not support remove()
    at org.apache.kafka.streams.state.internals.RocksDBStore$RocksDbIterator.remove(RocksDBStore.java:515)
    at org.apache.kafka.streams.state.internals.InnerMeteredKeyValueStore$MeteredKeyValueIterator.remove(InnerMeteredKeyValueStore.java:306)
    at com.loogtech.bi.compute.realtime.processor.AddCashProcessor$TotalCashAddPunctuator.punctuate(AddCashProcessor.java:115)
    at org.apache.kafka.streams.processor.internals.ProcessorNode$4.run(ProcessorNode.java:131)
    at org.apache.kafka.streams.processor.internals.StreamsMetricsImpl.measureLatencyNs(StreamsMetricsImpl.java:208)
    at org.apache.kafka.streams.processor.internals.ProcessorNode.punctuate(ProcessorNode.java:134)
    at org.apache.kafka.streams.processor.internals.StreamTask.punctuate(StreamTask.java:263)
    at org.apache.kafka.streams.processor.internals.PunctuationQueue.mayPunctuate(PunctuationQueue.java:54)
    at org.apache.kafka.streams.processor.internals.StreamTask.maybePunctuateSystemTime(StreamTask.java:619)
    at org.apache.kafka.streams.processor.internals.AssignedTasks.punctuate(AssignedTasks.java:430)
    at org.apache.kafka.streams.processor.internals.TaskManager.punctuate(TaskManager.java:324)
    at org.apache.kafka.streams.processor.internals.StreamThread.punctuate(StreamThread.java:969)
    at org.apache.kafka.streams.processor.internals.StreamThread.runOnce(StreamThread.java:834)
    at org.apache.kafka.streams.processor.internals.StreamThread.runLoop(StreamThread.java:774)
    at org.apache.kafka.streams.processor.internals.StreamThread.run(StreamThread.java:744)

似乎无法通过myStore.remove(key)

删除记录

如何按键删除记录?

1 个答案:

答案 0 :(得分:3)

接口KeyValueStore提供#delete(...)方法(不是#remove(...))来删除商店中的记录。