Java, How to get number of messages in a topic in apache kafka

后端 未结 17 1475
不思量自难忘°
不思量自难忘° 2020-11-30 19:11

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?

17条回答
  •  爱一瞬间的悲伤
    2020-11-30 19:51

    If you have access to server's JMX interface, the start & end offsets are present at:

    kafka.log:type=Log,name=LogStartOffset,topic=TOPICNAME,partition=PARTITIONNUMBER
    kafka.log:type=Log,name=LogEndOffset,topic=TOPICNAME,partition=PARTITIONNUMBER
    

    (you need to replace TOPICNAME & PARTITIONNUMBER). Bear in mind you need to check for each of the replicas of given partition, or you need to find out which one of the brokers is the leader for a given partition (and this can change over time).

    Alternatively, you can use Kafka Consumer methods beginningOffsets and endOffsets.

提交回复
热议问题