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

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

    Apache Kafka command to get un handled messages on all partitions of a topic:

    kafka-run-class kafka.tools.ConsumerOffsetChecker 
        --topic test --zookeeper localhost:2181 
        --group test_group
    

    Prints:

    Group      Topic        Pid Offset          logSize         Lag             Owner
    test_group test         0   11051           11053           2               none
    test_group test         1   10810           10812           2               none
    test_group test         2   11027           11028           1               none
    

    Column 6 is the un-handled messages. Add them up like this:

    kafka-run-class kafka.tools.ConsumerOffsetChecker 
        --topic test --zookeeper localhost:2181 
        --group test_group 2>/dev/null | awk 'NR>1 {sum += $6} 
        END {print sum}'
    

    awk reads the rows, skips the header line and adds up the 6th column and at the end prints the sum.

    Prints

    5
    

提交回复
热议问题