【问题标题】:Spring Cloud 3.1 documentation on how to use KTable in a Spring boot appSpring Cloud 3.1 关于如何在 Spring Boot 应用程序中使用 KTable 的文档
【发布时间】:2021-07-16 02:40:17
【问题描述】:

我正在努力寻找关于我可以在哪里使用 Spring Cloud Streams 的任何文档,该文档采用 Kafka 主题并将其放入 KTable。 例如,在此处https://cloud.spring.io/spring-cloud-static/spring-cloud-stream-binder-kafka/3.0.0.RC1/reference/html/spring-cloud-stream-binder-kafka.html#_materializing_ktable_as_a_state_store 上查找文档后,在 Spring Boot 中使用注释执行此操作的方式没有什么非常具体的。 我希望我可以使用 KStream 创建一个简单的 KTable,在我的 application.properties 中我有这个: spring.cloud.stream.bindings.process-in-0.destination: my-topic

然后在我的配置中,我希望我能做这样的事情

@Bean
public Consumer<KStream<String, String>> process() {
    return input -> input.toTable(Materialized.as("my-store"))
}

请告知我缺少什么?

【问题讨论】:

    标签: spring-cloud spring-cloud-stream spring-cloud-stream-binder-kafka


    【解决方案1】:

    如果您只想将来自 Kafka 主题的数据作为 KTable 使用,那么您可以按如下方式执行此操作。

    @Bean
    public Consumer<KTable<String, String>> process() {
        return input -> {
            
        };
    }
    

    如果要将表具体化为命名存储,则可以将其添加到配置中。

    spring.cloud.stream.kafka.streams.bindings.process_in_0.consumer.materializedAs: my-store
    

    您也可以执行问题中的操作,即将其作为KStream 接收,然后转换为KTable。但是,如果这就是您需要做的全部,您可能宁愿按照我在此处的建议首先将其接收为 KTable

    【讨论】:

    • 在这个例子中,Spring 如何知道从哪个主题消费?我想查询这些数据以通过键获取记录
    • 您可以像在任何其他 Spring Cloud Stream 应用程序中一样提供主题。在这个特定的例子中,它将是spring.cloud.stream.bindings.process-in-0.destination=&lt;your-input-topic&gt;
    • 我还有一个要求,我需要在 REST 样式控制器中从这个 ktable 中读取,然后这将如何输入呢?我知道 InteractiveQueryService 所以我怀疑 id 需要注入它,但是这个查询服务是如何知道这个 KTable 的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-06
    • 2020-03-20
    • 2015-12-26
    • 2020-05-15
    • 2017-11-22
    • 2018-10-16
    • 1970-01-01
    相关资源
    最近更新 更多