【问题标题】:Spring and Kafka streams - How to use Query APISpring 和 Kafka 流 - 如何使用查询 API
【发布时间】:2019-11-18 16:25:37
【问题描述】:

我是 kafka 和 kafka 流的新手。我有一个与 kafka 生产者、消费者、KStream 和 KTable 一起工作的基本 Spring 服务。现在,我想检查我的 KTable 记录,所以为了实现它,我正在尝试使用 Kafka Query API。

这可以通过以下方式实现(没有 Spring 集成):

KafkaStreams streams = new KafkaStreams(topology, config);
// Get access to the custom store
MyReadableCustomStore<String,String> store = streams.store("the-custom-store", new MyCustomStoreType<String,String>());
// Query the store
String value = store.read("key");

现在,我尝试使用基于 Spring 的 InteractiveQueryService 进行查询。但我在 Spring 引导中遇到了一些依赖问题。

在 Spring 中使用 kafka 查询 API 的最佳方法是什么?

我的服务中的 Spring kafka 配置如下所示:

@Bean("streamsBuilder")
public StreamsBuilderFactoryBean recordsStreamBuilderFactoryBean() {
    Map<String, Object> config = new HashMap<>();
    // set some properties
    return new StreamsBuilderFactoryBean(new KafkaStreamsConfiguration(config));
}

你能建议吗?

【问题讨论】:

    标签: spring apache-kafka apache-kafka-streams spring-kafka


    【解决方案1】:

    这是一个 Spring Boot 应用程序,用于展示如何...

    @SpringBootApplication
    @EnableKafkaStreams
    public class So58918956Application {
    
        public static void main(String[] args) {
            SpringApplication.run(So58918956Application.class, args);
        }
    
        @Bean
        public CountDownLatch latch(StreamsBuilderFactoryBean streamsBuilderFB) {
            CountDownLatch latch = new CountDownLatch(1);
            streamsBuilderFB.setStateListener((newState, oldState) -> {
                if (State.RUNNING.equals(newState)) {
                    latch.countDown();
                }
            });
            return latch;
        }
    
        @Bean
        public KTable<String, String> table(StreamsBuilder streamsBuilder) {
            Serde<String> serde = Serdes.String();
            KTable<String, String> table = streamsBuilder.table("so58918956",
                    Consumed.with(serde, serde)
                            .withOffsetResetPolicy(AutoOffsetReset.EARLIEST), 
                    Materialized.as("the-custom-store"));
            return table;
        }
    
        @Bean
        public ApplicationRunner runner(StreamsBuilderFactoryBean streamsBuilderFB,
                KafkaTemplate<String, String> template, KTable<String, String> table) {
    
            return args -> {
                template.send("so58918956", "key", "value");
                latch(streamsBuilderFB).await(10, TimeUnit.SECONDS);
                ReadOnlyKeyValueStore<String, String> store = streamsBuilderFB.getKafkaStreams().store(
                        table.queryableStoreName(),
                        QueryableStoreTypes.keyValueStore());
                System.out.println(store.get("key"));
            };
        }
    
        @Bean
        public NewTopic topic() {
            return TopicBuilder.name("so58918956").partitions(1).replicas(1).build();
        }
    
    }
    

    【讨论】:

    • 问题不是如何定义KStream,而是如何用Spring查询KTable的存储?
    • 我有一些带有商店定义的基本 KTable,我想在这个商店上运行一个查询,从一些 REST 控制器并检查商店数据。任何想法我该怎么做.. 或者您可能有其他想法来查看商店数据
    • 感谢@Gary 的帮助!这就是我一直在寻找的。​​span>
    • @Gary 有来自 Spring 网站的文件吗?参考很短。
    • @Gary “streamsBuilderFB”的 bean 声明在哪里?我复制了您的代码,但在我的代码中为空。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 2023-04-06
    • 2021-09-03
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 2019-05-11
    相关资源
    最近更新 更多