简体   繁体   English

读取来自 Kafka 主题的所有消息

[英]Reading all messages from a Kafka topic

I have the following use case:我有以下用例:

I have two Kafka topics, one is meant to be used a stream of incoming messages to be processed, the other is meant as store of records that is meant to be used as a bootstrap to the initial state of the application.我有两个 Kafka 主题,一个用于要处理的传入消息流,另一个用作记录存储,用作应用程序初始状态的引导程序。

Is there a way to do the following:有没有办法做到以下几点:

  1. Read all messages from a Kafka topic when the application starts up and store all ConsumerRecord in memory from the topic that is meant to bootstrap the application to its initial state当应用程序启动时读取来自 Kafka 主题的所有消息,并将所有ConsumerRecord存储在内存中的主题中,该主题旨在将应用程序引导到其初始状态
  2. Only after all messages have been read allow the ConsumerRecord from the stream topic to be processed只有在读取所有消息后,才允许处理来自流主题的ConsumerRecord
  3. As there may be additional records on the state topic to incorporate them into the application's state when the application is running without having to restart the application.因为状态主题上可能有额外的记录,以便在应用程序运行时将它们合并到应用程序的状态中,而无需重新启动应用程序。

Thanks!谢谢!

Start your bootstrap consumer first.首先启动你的引导消费者。

Read the other topic till a particular offset is reached or (if you want the end, you can read as long as there is no polled records available [this is not the best way!]).阅读另一个主题,直到达到特定的偏移量或(如果你想要结束,只要没有可用的轮询记录,你就可以阅读[这不是最好的方法!])。 If you want to start at particular offset every-time you have to use a seek.如果你想每次都从特定的偏移量开始,你必须使用搜索。 Also use a unique consumer group id for this since you want to all the records.由于您想要所有记录,因此也为此使用唯一的消费者组 ID。 You might want to handle the rebalance case appropriately.您可能希望适当地处理重新平衡的情况。

Then close that consumer and start the other stream consumer and process the data.然后关闭该消费者并启动另一个流消费者并处理数据。 Using Ktables with Kafka streams might be better, but I am not familiar with it.将 Ktables 与 Kafka 流一起使用可能会更好,但我不熟悉它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM