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

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

    I haven't tried this myself, but it seems to make sense.

    You can also use kafka.tools.ConsumerOffsetChecker (source).

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-30 19:32

    In most recent versions of Kafka Manager, there is a column titled Summed Recent Offsets.

    0 讨论(0)
  • 2020-11-30 19:34

    I actually use this for benchmarking my POC. The item you want to use ConsumerOffsetChecker. You can run it using bash script like below.

    bin/kafka-run-class.sh kafka.tools.ConsumerOffsetChecker  --topic test --zookeeper localhost:2181 --group testgroup
    

    And below is the result : As you can see on the red box, 999 is the number of message currently in the topic.

    Update: ConsumerOffsetChecker is deprecated since 0.10.0, you may want to start using ConsumerGroupCommand.

    0 讨论(0)
  • 2020-11-30 19:34

    Run the following (assuming kafka-console-consumer.sh is on the path):

    kafka-console-consumer.sh  --from-beginning \
    --bootstrap-server yourbroker:9092 --property print.key=true  \
    --property print.value=false --property print.partition \
    --topic yourtopic --timeout-ms 5000 | tail -n 10|grep "Processed a total of"
    
    0 讨论(0)
  • 2020-11-30 19:34

    Excerpts from Kafka docs

    Deprecations in 0.9.0.0

    The kafka-consumer-offset-checker.sh (kafka.tools.ConsumerOffsetChecker) has been deprecated. Going forward, please use kafka-consumer-groups.sh (kafka.admin.ConsumerGroupCommand) for this functionality.

    I am running Kafka broker with SSL enabled for both server and client. Below command I use

    kafka-consumer-groups.sh --bootstrap-server Broker_IP:Port --list --command-config /tmp/ssl_config kafka-consumer-groups.sh --bootstrap-server Broker_IP:Port --command-config /tmp/ssl_config --describe --group group_name_x

    where /tmp/ssl_config is as below

    security.protocol=SSL
    ssl.truststore.location=truststore_file_path.jks
    ssl.truststore.password=truststore_password
    ssl.keystore.location=keystore_file_path.jks
    ssl.keystore.password=keystore_password
    ssl.key.password=key_password
    
    0 讨论(0)
提交回复
热议问题