有没有办法从Kafka主题获取最新消息?

编程入门 行业动态 更新时间:2024-10-22 14:27:14
本文介绍了有没有办法从Kafka主题获取最新消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有多个分区的Kafka主题,我想知道Java中是否有一种方法可以获取该主题的最后一条消息.我不在乎我只是想获取最新消息的分区.

I have a Kafka topic with multiple partitions and I wonder if there is a way in Java to fetch the last message for the topic. I don't care for the partitions I just want to get the latest message.

我尝试了 @KafkaListener ,但是只有在主题更新时,它才会获取消息.如果打开应用程序后没有发布任何内容,则不会返回任何内容.

I have tried @KafkaListener but it fetches the message only when the topic is updated. If there is nothing published after the application is opened nothing is returned.

也许听众根本不是解决问题的正确方法?

Maybe the listener is not the right approach to the problem at all?

推荐答案

以下代码段对我有用.您可以尝试一下.注释中的解释.

This following snippet worked for me. You may try this. Explanation in the comments.

KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties); consumer.subscribe(Collections.singletonList(topic)); consumer.poll(Duration.ofSeconds(10)); consumer.assignment().forEach(System.out::println); AtomicLong maxTimestamp = new AtomicLong(); AtomicReference<ConsumerRecord<String, String>> latestRecord = new AtomicReference<>(); // get the last offsets for each partition consumer.endOffsets(consumer.assignment()).forEach((topicPartition, offset) -> { System.out.println("offset: "+offset); // seek to the last offset of each partition consumer.seek(topicPartition, (offset==0) ? offset:offset - 1); // poll to get the last record in each partition consumer.poll(Duration.ofSeconds(10)).forEach(record -> { // the latest record in the 'topic' is the one with the highest timestamp if (record.timestamp() > maxTimestamp.get()) { maxTimestamp.set(record.timestamp()); latestRecord.set(record); } }); }); System.out.println(latestRecord.get());

更多推荐

有没有办法从Kafka主题获取最新消息?

本文发布于:2023-10-24 14:37:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524199.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最新消息   没有办法   主题   Kafka

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!