title: How to get latest offset/size of a Kafka topic using KafkaAdminClient (Java) for 2.x version
body: <p>Is there a more efficient/simpler way of getting the size / latest offsets of a topic/partitions using the newest <strong>Kafka client 2.4 APIs in Java</strong>?
  And then, calculate a Lag for a consumer group by comparing that group's offsets with the size of the topic...</p>
  
  <p>I know this question has been asked for older Kafka versions and there is also a way to get this info from JMX metrics exposed by Kafka, but I am stuck with a legacy app that needs to do it in Java but with latest 2.4 Kafka libs.</p>
  
  <p>The usual way of getting this info , as far as I understand is:</p>
  
  <ul>
  <li>The easiest part: get offsets for a topic/partitions for a consumer groupID using an API call on KafkaAdminClient like 
  <code>public ListConsumerGroupOffsetsResult listConsumerGroupOffsets(String groupId, ListConsumerGroupOffsetsOptions options)</code></li>
  <li>The hardest part: Determine the size of the topic for each partition:
  
  <ul>
  <li>create a new consumer and subscribe to the topic </li>
  <li>advance the consumer to the latest offset using <code>consumer.seekToEnd(...)</code></li>
  <li>get the position of the consumer for all partitions using <code>consumer.position(...)</code></li>
  </ul></li>
  <li>finally, do [size - current offset] to determine the lag of the consumer group for each partition</li>
  </ul>
  
  <p>Thus, determining the last offset is a pretty heavy operation ... 
  So my question is: is there a more efficient way of getting the last offsets for a topic without using the dummy consumer, maybe in the latest 2.4 APIs? The topic/partition size info is really independent of any consumers, so it seems logical to be able to get it without the use of consumers...</p>
  
  <p>Thank you!</p>
  
  <p>Marina</p>