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

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

    Since ConsumerOffsetChecker is no longer supported, you can use this command to check all messages in topic:

    bin/kafka-run-class.sh kafka.admin.ConsumerGroupCommand \
        --group my-group \
        --bootstrap-server localhost:9092 \
        --describe
    

    Where LAG is the count of messages in topic partition:

    Also you can try to use kafkacat. This is an open source project that may help you to read messages from a topic and partition and prints them to stdout. Here is a sample that reads the last 10 messages from sample-kafka-topic topic, then exit:

    kafkacat -b localhost:9092 -t sample-kafka-topic -p 0 -o -10 -e
    

提交回复
热议问题