简体   繁体   English

如何从Kafka主题中检索最新消息

[英]How to retrieve lastest message from Kafka topic

I'm just a newbie of Kafka and I have a problem:我只是 Kafka 的新手,我有一个问题:

I have topic 'A' in Kafka, I start Spring boot app and use MessageChannel to send some message to topic 'A', then I stop app.我在 Kafka 中有主题“A”,我启动 Spring Boot 应用程序并使用 MessageChannel 向主题“A”发送一些消息,然后我停止应用程序。

Is there anyway to get the latest message I've sent to topic 'A' (not all message) when I start app again?无论如何,当我再次启动应用程序时,是否可以获得我发送到主题“A”(并非所有消息)的最新消息? I've searched all solutions but they did not help me much, it always receive message immediately if I only sending new message.我已经搜索了所有解决方案,但它们对我没有太大帮助,如果我只发送新消息,它总是会立即收到消息。 If you have runable code, pls share, I very much appreciated :(如果您有可运行的代码,请分享,我非常感谢:(

    // Start application


    // Get latest message in topic 'A' then do some LOGIC
    if (exist latest message) {
          //Print latest message
    }

Your consumers store their offset (that is, the last read position). 您的使用者存储其偏移量(即最后读取的位置)。 After restart, they continue reading from this point. 重新启动后,他们将从这一点继续阅读。 This behavior is by design. 此行为是设计使然。

The offset reset properties are used when the offset for this particular consumer group is not yet known for some reason (say, it's a new consumer group or offsets have expired), but still, there are generally two options - a) reread all from the beginning b) start listening for new messages, forgetting everything that was before now. 当由于某种原因(例如,这是一个新的消费组或偏移量已过期)而尚未得知此特定使用者组的偏移量时,将使用偏移量重置属性,但是通常还是有两个选择-a)从开始b)开始收听新消息,忘记之前的所有内容。

There are hacky ways to achieve what you've described, but they are not straightforward and not recommended (a trivial one: consumer messages while just skipping them until you reach the partition EOF) 哈克的方式来实现你所描述的东西,但他们并不简单,不推荐使用(是小事一桩:直到你到达分区EOF消费者的消息,而只是跳过它们)

Maybe just Kafka is not the right tool for this problem. 也许仅仅是Kafka不是解决此问题的正确工具。

One option could be combining what @saabeilin said about "re-reading all from the beggining" plus Log Compaction . 一种选择可能是结合@saabeilin所说的“重新开始阅读所有内容”以及Log Compaction

If you configure it properly, the logs will be compacted keeping only last ids, so the re-read operation will result in getting only last message sended. 如果配置正确,日志将被压缩,仅保留最后一个ID,因此重新读取操作将导致仅发送最后一条消息。

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

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