Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有办法从卡夫卡主题中获取最后一条信息?_Java_Apache Kafka_Spring Kafka - Fatal编程技术网

Java 有没有办法从卡夫卡主题中获取最后一条信息?

Java 有没有办法从卡夫卡主题中获取最后一条信息?,java,apache-kafka,spring-kafka,Java,Apache Kafka,Spring Kafka,我有一个带有多个分区的Kafka主题,我想知道Java中是否有方法获取该主题的最后一条消息。我不关心分区,我只想得到最新消息 我尝试过@KafkaListener,但它只在主题更新时获取消息。如果在应用程序打开后没有发布任何内容,则不会返回任何内容 可能侦听器根本不是解决问题的正确方法?您必须使用每个分区的最新消息,然后在客户端进行比较(如果消息包含时间戳,则使用消息上的时间戳)。原因是卡夫卡不能保证分区间的排序。在分区内,可以确保偏移量最大的消息是推送到它的最新消息 下面这个片段对我很有用。你

我有一个带有多个分区的Kafka主题,我想知道Java中是否有方法获取该主题的最后一条消息。我不关心分区,我只想得到最新消息

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


可能侦听器根本不是解决问题的正确方法?

您必须使用每个分区的最新消息,然后在客户端进行比较(如果消息包含时间戳,则使用消息上的时间戳)。原因是卡夫卡不能保证分区间的排序。在分区内,可以确保偏移量最大的消息是推送到它的最新消息

下面这个片段对我很有用。你可以试试这个。评论中的解释

        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());
KafkaConsumer消费者=新的KafkaConsumer(属性);
consumer.subscribe(Collections.singletonList(主题));
消费者投票(持续时间为10秒);
consumer.assignment().forEach(System.out::println);
AtomicLong maxTimestamp=新的AtomicLong();
AtomicReference latestRecord=新的AtomicReference();
//获取每个分区的最后偏移量
consumer.endoffset(consumer.assignment()).forEach((topicPartition,offset)->{
系统输出打印项次(“偏移量:+偏移量);
//查找每个分区的最后一个偏移量
seek(topicPartition,(offset==0)?offset:offset-1;
//轮询以获取每个分区中的最后一条记录
consumer.poll(持续时间秒(10)).forEach(记录->{
//“主题”中的最新记录是具有最高时间戳的记录
if(record.timestamp()>maxTimestamp.get()){
maxtimstamp.set(record.timestamp());
最新记录集(记录);
}
});
});
System.out.println(latestRecord.get());

似乎有问题。。。即使偏移量为0,您的代码仍然尝试在consumer.seek(topicPartition,offset-1)中减去1;如果网络很慢或者卡夫卡很忙怎么办?然后轮询(10秒)可能返回零条记录或部分结果。@WernerDaehn必须根据网络条件进行调整,可能是可以配置的,这是一个示例片段。@JavaTechnical是,理解。但据我所知,没有好的解决办法。你必须等待很长时间,并希望它足够长。“long”的含义取决于用例。有一个Jira项目,我已经添加了一些评论。如果其他人也发表评论,我们将不胜感激。