I have a kafka topic where I have to find number of messages present in a topic and number of partitions present in the topic using Java
https://stackoverflow.com/a/60124779/11609323
Above is example of using the metrics() api to get the lag, the lag represents the endOffsets minus currentOffset , you could get the startOffset as well as a metric() , calculating the endOffset minus startOffset will get you the number of records currently in topic's partition, sum the number of records in each partition to get total number of records in the topic,
Notice StartOffset not always be 0, as you might have retention setting pruning old messages
P.S this is how we show this information to developers in our company , we found it as better way then setting dummy consumer group for beginning and reset it