Skip to content
Advertisement

How to verify if a Kafka Topic is not empty meaning has at least 1 message?

I am writing a little Kafka metrics exporter (Yes there are loads available like prometheus etc but I want a light weight custom one. Kindly excuse me on this).

As part of this I would like to know as soon as first message is received (or topic has messages) in a Kafka topic. I am using Spring Boot and Kafka.

I have the below code which gives the name of the topic and number of partitions. I want to know if the topic has messages? Kindly let me know how can I get this stat. Any lead is much appreciated!

JavaScript

Advertisement

Answer

Create a KafkaConsumer and call endOffsets (the consumer does not need to be subscribed to the topic(s)).

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement