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

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

    I had this same question and this is how I am doing it, from a KafkaConsumer, in Kotlin:

    val messageCount = consumer.listTopics().entries.filter { it.key == topicName }
        .map {
            it.value.map { topicInfo -> TopicPartition(topicInfo.topic(), topicInfo.partition()) }
        }.map { consumer.endOffsets(it).values.sum() - consumer.beginningOffsets(it).values.sum()}
        .first()
    

    Very rough code, as I just got this to work, but basically you want to subtract the topic's beginning offset from the ending offset and this will be the current message count for the topic.

    You can't just rely on the end offset because of other configurations (cleanup policy, retention-ms, etc.) that may end up causing the deletion old messages from your topic. Offsets only "move" forward, so it is the beggining offset that will move forward closer to the end offset (or eventually to the same value, if the topic contains no message right now).

    Basically the end offset represents the overall number of messages that went through that topic, and the difference between the two represent the number of messages that the topic contains right now.

提交回复
热议问题