Java,如何在 apache kafka 中获取主题中的消息数 [英] Java, How to get number of messages in a topic in apache kafka

查看:47
本文介绍了Java,如何在 apache kafka 中获取主题中的消息数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 apache kafka 进行消息传递.我已经用 Java 实现了生产者和消费者.我们如何获取主题中的消息数量?

I am using apache kafka for messaging. I have implemented the producer and consumer in Java. How can we get the number of messages in a topic?

推荐答案

从消费者的角度来看,唯一想到的方法是实际消费消息并计算它们.

The only way that comes to mind for this from a consumer point of view is to actually consume the messages and count them then.

Kafka 代理公开 JMX 计数器以显示自启动以来收到的消息数,但您无法知道其中有多少已被清除.

The Kafka broker exposes JMX counters for number of messages received since start-up but you cannot know how many of them have been purged already.

在大多数常见情况下,最好将 Kafka 中的消息视为无限流,获取当前保留在磁盘上的数量的离散值是无关紧要的.此外,当处理一组代理时,事情会变得更加复杂,这些代理都在一个主题中包含消息的子集.

In most common scenarios, messages in Kafka is best seen as an infinite stream and getting a discrete value of how many that is currently being kept on disk is not relevant. Furthermore things get more complicated when dealing with a cluster of brokers which all have a subset of the messages in a topic.

这篇关于Java,如何在 apache kafka 中获取主题中的消息数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆