Kafka Streams持久存储错误:状态存储,可能已迁移到另一个实例

时间:2018-04-24 22:26:17

标签: apache-kafka apache-kafka-streams

我正在使用Kafka Streams和Spring Boot。在我的用例中,当我从其他微服务接收客户事件时,我需要存储在客户物化视图中,当我收到订单事件时,我需要加入客户并订购然后存储在客户订单物化视图中。为实现这一目标,我创建了持久性键值存储客户存储updating this when a new event comes

    StoreBuilder customerStateStore = Stores.keyValueStoreBuilder(Stores.persistentKeyValueStore("customer"),Serdes.String(), customerSerde).withLoggingEnabled(new HashMap<>());
streamsBuilder.addStateStore(customerStateStore);
KTable<String,Customer> customerKTable=streamsBuilder.table("customer",Consumed.with(Serdes.String(),customerSerde));
    customerKTable.foreach(((key, value) -> System.out.println("Customer from Topic: "+value)));

配置拓扑,流和启动流对象。当我尝试使用ReadOnlyKeyValueStore访问商店时,我得到了以下异常,即使我不久前存储了一些对象

streams.start();
ReadOnlyKeyValueStore<String, Customer> customerStore = streams.store("customer", QueryableStoreTypes.keyValueStore());
System.out.println("customerStore.approximateNumEntries()-> " + customerStore.approximateNumEntries());

代码已上传至Github以供参考。感谢您的帮助。

例外:

org.apache.kafka.streams.errors.InvalidStateStoreException: the state store, customer, may have migrated to another instance.
    at org.apache.kafka.streams.state.internals.QueryableStoreProvider.getStore(QueryableStoreProvider.java:60)
    at org.apache.kafka.streams.KafkaStreams.store(KafkaStreams.java:1043)
    at com.kafkastream.service.EventsListener.main(EventsListener.java:94)

1 个答案:

答案 0 :(得分:2)

国营商店通常需要一些时间来准备。最简单的方法如下。 (官方文件中的代码)

public static <T> T waitUntilStoreIsQueryable(final String storeName,
                                              final QueryableStoreType<T> queryableStoreType,
                                              final KafkaStreams streams) throws InterruptedException {
  while (true) {
    try {
      return streams.store(storeName, queryableStoreType);
    } catch (InvalidStateStoreException ignored) {
      // store not yet ready for querying
      Thread.sleep(100);
    }
  }
}

您可以在文档中找到其他信息。 https://docs.confluent.io/current/streams/faq.html#interactive-queries