如何在DSL中使用KeyValueStore状态存储?

时间:2017-05-26 04:55:01

标签: apache-kafka-streams

KeyValueStore<String, Long> kvStore=(KeyValueStore<String, Long>) 
Stores.create("InterWindowStore1").withKeys(Serdes.String())
                .withValues(Serdes.Long())
                .persistent()
                .build().get();` 

我已经创建了statestore,如上面的代码所示,并尝试插入kvStore.put(key, value);,但它正在给我NPE

Caused by: java.lang.NullPointerException
    at org.apache.kafka.streams.state.internals.MeteredKeyValueStore.put(MeteredKeyValueStore.java:117)
    at org.apache.kafka.streams.processor.internals.ProcessorNode.process(ProcessorNode.java:82)
    at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.forward(ProcessorContextImpl.java:202)
    at org.apache.kafka.streams.kstream.internals.ForwardingCacheFlushListener.apply(ForwardingCacheFlushListener.java:42)
    at org.apache.kafka.streams.state.internals.CachingWindowStore.maybeForward(CachingWindowStore.java:103)
    at org.apache.kafka.streams.state.internals.CachingWindowStore.access$200(CachingWindowStore.java:34)
    at org.apache.kafka.streams.state.internals.CachingWindowStore$1.apply(CachingWindowStore.java:86)
    at org.apache.kafka.streams.state.internals.NamedCache.flush(NamedCache.java:131)
    at org.apache.kafka.streams.state.internals.NamedCache.flush(NamedCache.java:95)

1 个答案:

答案 0 :(得分:1)

正如您在评论中所描述的那样,您基本上是在进行窗口聚合:

KStream stream = ...
KTable table = stream.groupByKey().aggregate(..., TimeWindow.of(...));

当您KTable流可能包含窗口聚合的更新时,您希望修改此流。为此,您可以使用有状态变换器或值变换器:

StateStoreSupplier myState = State.create("nameOfMyState")....;

KStream result = table.toStream().transform(..., "nameOfMyState");

最后,您可以将结果写入输出主题:

result.to("output-topic");

您提供给Transformer的{​​{1}}可以通过transform中的给定上下文获取状态,并在每次生成/更新窗口输出时在init()内使用。