Hi,
my case very, very simple: I want expose small compacted topic to Spring Application as key value store. I think very simple option would be a global store from Kafka Streams. I do not want any transformation - just expose messages as-is.
I can resolve all problem with sophisticated Spring setup. The problem that all examples I found focus on transformations, processors, pipes, etc. I do not want it. All I want is public global store, automatically synchronized with topic. I want the store be available to plain @Components
I found one example how to implement that here: Kafka Streams With Spring Boot | Baeldung
the factoryBean is an instance of StreamsBuilderFactoryBean that is wired into the controller. This provides the KafkaStreams instance managed by this factory bean. Therefore, we can obtain the counts key/value state store that we created earlier, represented by KTable . At this point, we can use this to get the current count for the requested word from the local state store.
@GetMapping("/count/{word}")
public Long getWordCount(@PathVariable String word) {
KafkaStreams kafkaStreams = factoryBean.getKafkaStreams();
ReadOnlyKeyValueStore<String, Long> counts = kafkaStreams.store(
StoreQueryParameters.fromNameAndType("counts", QueryableStoreTypes.keyValueStore())
);
return counts.get(word);
}
This example presents exactly what I need. But I have a doubt - is it reasonable to call factoryBean.getKafkaStreams().store(...)
on each request, over and over again? It looks a bit unefficient…