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

后端 未结 17 1494
不思量自难忘°
不思量自难忘° 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:36

    Sometimes the interest is in knowing the number of messages in each partition, for example, when testing a custom partitioner.The ensuing steps have been tested to work with Kafka 0.10.2.1-2 from Confluent 3.2. Given a Kafka topic, kt and the following command-line:

    $ kafka-run-class kafka.tools.GetOffsetShell \
      --broker-list host01:9092,host02:9092,host02:9092 --topic kt
    

    That prints the sample output showing the count of messages in the three partitions:

    kt:2:6138
    kt:1:6123
    kt:0:6137
    

    The number of lines could be more or less depending on the number of partitions for the topic.

提交回复
热议问题