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

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

    Using the Java client of Kafka 2.11-1.0.0, you can do the following thing :

        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        consumer.subscribe(Collections.singletonList("test"));
        while(true) {
            ConsumerRecords<String, String> records = consumer.poll(100);
            for (ConsumerRecord<String, String> record : records) {
                System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
    
                // after each message, query the number of messages of the topic
                Set<TopicPartition> partitions = consumer.assignment();
                Map<TopicPartition, Long> offsets = consumer.endOffsets(partitions);
                for(TopicPartition partition : offsets.keySet()) {
                    System.out.printf("partition %s is at %d\n", partition.topic(), offsets.get(partition));
                }
            }
        }
    

    Output is something like this :

    offset = 10, key = null, value = un
    partition test is at 13
    offset = 11, key = null, value = deux
    partition test is at 13
    offset = 12, key = null, value = trois
    partition test is at 13
    
    0 讨论(0)
  • 2020-11-30 19:40

    It is not java, but may be useful

    ./bin/kafka-run-class.sh kafka.tools.GetOffsetShell 
      --broker-list <broker>:  <port> 
      --topic <topic-name> --time -1 --offsets 1 
      | awk -F  ":" '{sum += $3} END {print sum}'
    
    0 讨论(0)
  • 2020-11-30 19:40

    You may use kafkatool. Please check this link -> http://www.kafkatool.com/download.html

    Kafka Tool is a GUI application for managing and using Apache Kafka clusters. It provides an intuitive UI that allows one to quickly view objects within a Kafka cluster as well as the messages stored in the topics of the cluster.

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

    To get all the messages stored for the topic you can seek the consumer to the beginning and end of the stream for each partition and sum the results

    List<TopicPartition> partitions = consumer.partitionsFor(topic).stream()
            .map(p -> new TopicPartition(topic, p.partition()))
            .collect(Collectors.toList());
        consumer.assign(partitions); 
        consumer.seekToEnd(Collections.emptySet());
    Map<TopicPartition, Long> endPartitions = partitions.stream()
            .collect(Collectors.toMap(Function.identity(), consumer::position));
        consumer.seekToBeginning(Collections.emptySet());
    System.out.println(partitions.stream().mapToLong(p -> endPartitions.get(p) - consumer.position(p)).sum());
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题