我正在尝试使用Kafka Streams,并且创建了以下拓扑:
KStream<String, HistoryEvent> eventStream = builder.stream(applicationTopicName, Consumed.with(Serdes.String(),
historyEventSerde));
eventStream.selectKey((key, value) -> new HistoryEventKey(key, value.getIdentifier()))
.groupByKey()
.reduce((e1, e2) -> e2, Materialized.as(streamByKeyStoreName));
我以后像这样启动流:
private void startKafkaStreams(KafkaStreams streams) {
CompletableFuture<KafkaStreams.State> stateFuture = new CompletableFuture<>();
streams.setStateListener((newState, oldState) -> {
if(stateFuture.isDone()) {
return;
}
if(newState == KafkaStreams.State.RUNNING || newState == KafkaStreams.State.ERROR) {
stateFuture.complete(newState);
}
});
streams.start();
try {
KafkaStreams.State finalState = stateFuture.get();
if(finalState != KafkaStreams.State.RUNNING) {
// ...
}
} catch (InterruptedException ex) {
// ...
} catch(ExecutionException ex) {
// ...
}
}
“我的流”开始时没有出现错误,最终进入了RUNNING
状态,表示未来已经完成。稍后,我尝试访问在KTable拓扑中创建的存储:
public KafkaFlowHistory createFlowHistory(String flowId) {
ReadOnlyKeyValueStore<HistoryEventKey, HistoryEvent> store = streams.store(streamByKeyStoreName,
QueryableStoreTypes.keyValueStore());
return new KafkaFlowHistory(flowId, store, event -> topicProducer.send(new ProducerRecord<>(applicationTopicName, flowId, event)));
}
我已验证createFlowHistory
的初始化将来完成后会调用RUNNING
,但是我始终无法执行此操作,并且KafkaStreams报告了以下错误:
线程“主”中的异常 org.apache.kafka.streams.errors.InvalidStateStoreException:无法获取 状态存储流事件流文件服务测试实例按键 因为流线程是PARTITIONS_ASSIGNED,而不是RUNNING
显然,线程的状态已更改。在尝试查询商店并等待Kafka的内部线程进入正确状态时,是否需要手动进行此操作?
答案 0 :(得分:4)
在启动时,Kafka Streams会执行以下状态转换:
std::tuple<void*, size_t>
您需要等待第二个RUNNING状态才能查询。这是一个已知问题,我们希望在以后的版本中予以解决。