final Map<TopicPartition, OffsetAndMetadata> partitionToOffsetAndMetadata = result.partitionsToOffsetAndMetadata().get();
protected List<String> getConsumerTopics(String group) { try { return adminClient.listConsumerGroupOffsets(group) .partitionsToOffsetAndMetadata() .get() .keySet() .stream() .map(TopicPartition::topic) .collect(Collectors.toList()); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new StreamRuntimeException(e); } catch (ExecutionException e) { throw new StreamRuntimeException(e); } }
.partitionsToOffsetAndMetadata() .get() .entrySet()
/** * Returns consumer offsets for requested consumer group id. * @param consumerGroupId id to retrieve offsets for. * @return ConsumerGroupOffsets */ public ConsumerGroupOffsets getConsumerGroupOffsets(final String consumerGroupId) { try { // Make request final ListConsumerGroupOffsetsResult results = adminClient.listConsumerGroupOffsets(consumerGroupId); final List<PartitionOffset> offsetList = new ArrayList<>(); String topic = null; // Iterate over results final Map<org.apache.kafka.common.TopicPartition, OffsetAndMetadata> partitionsToOffsets = results .partitionsToOffsetAndMetadata() .get(); for (final Map.Entry<org.apache.kafka.common.TopicPartition, OffsetAndMetadata> entry : partitionsToOffsets.entrySet()) { offsetList.add(new PartitionOffset( entry.getKey().partition(), entry.getValue().offset() )); if (topic == null) { topic = entry.getKey().topic(); } } return new ConsumerGroupOffsets(consumerGroupId, topic, offsetList); } catch (final InterruptedException | ExecutionException e) { throw new RuntimeException(e.getMessage(), e); } }
forEach(group -> { try { Map<TopicPartition, OffsetAndMetadata> offsets = admin.listConsumerGroupOffsets(group.groupId()).partitionsToOffsetAndMetadata().get(pollingIntervalSeconds, TimeUnit.SECONDS); for (Map.Entry<TopicPartition, OffsetAndMetadata> entry : offsets.entrySet()) { TopicPartition tp = entry.getKey();