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

后端 未结 17 1495
不思量自难忘°
不思量自难忘° 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: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 partitions = consumer.partitionsFor(topic).stream()
            .map(p -> new TopicPartition(topic, p.partition()))
            .collect(Collectors.toList());
        consumer.assign(partitions); 
        consumer.seekToEnd(Collections.emptySet());
    Map 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());
    

提交回复
热议问题