Search in sources :

Example 1 with ListConsumerGroupOffsetsResult

use of org.apache.kafka.clients.admin.ListConsumerGroupOffsetsResult in project kafka by apache.

the class EosTestDriver method getCommittedOffsets.

private static Map<TopicPartition, Long> getCommittedOffsets(final Admin adminClient, final boolean withRepartitioning) {
    final Map<TopicPartition, OffsetAndMetadata> topicPartitionOffsetAndMetadataMap;
    try {
        final ListConsumerGroupOffsetsResult listConsumerGroupOffsetsResult = adminClient.listConsumerGroupOffsets(EosTestClient.APP_ID);
        topicPartitionOffsetAndMetadataMap = listConsumerGroupOffsetsResult.partitionsToOffsetAndMetadata().get(10, TimeUnit.SECONDS);
    } catch (final Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    final Map<TopicPartition, Long> committedOffsets = new HashMap<>();
    for (final Map.Entry<TopicPartition, OffsetAndMetadata> entry : topicPartitionOffsetAndMetadataMap.entrySet()) {
        final String topic = entry.getKey().topic();
        if (topic.equals("data") || withRepartitioning && topic.equals("repartition")) {
            committedOffsets.put(entry.getKey(), entry.getValue().offset());
        }
    }
    return committedOffsets;
}
Also used : HashMap(java.util.HashMap) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) ListConsumerGroupOffsetsResult(org.apache.kafka.clients.admin.ListConsumerGroupOffsetsResult) HashMap(java.util.HashMap) Map(java.util.Map) TimeoutException(org.apache.kafka.common.errors.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ListConsumerGroupOffsetsResult

use of org.apache.kafka.clients.admin.ListConsumerGroupOffsetsResult in project flink by apache.

the class KafkaTableTestBase method getConsumerOffset.

public Map<TopicPartition, OffsetAndMetadata> getConsumerOffset(String groupId) {
    Map<String, Object> properties = new HashMap<>();
    properties.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, getBootstrapServers());
    try (AdminClient admin = AdminClient.create(properties)) {
        ListConsumerGroupOffsetsResult result = admin.listConsumerGroupOffsets(groupId);
        return result.partitionsToOffsetAndMetadata().get(20, TimeUnit.SECONDS);
    } catch (Exception e) {
        throw new IllegalStateException(String.format("Fail to get consumer offsets with the group id [%s].", groupId), e);
    }
}
Also used : HashMap(java.util.HashMap) ListConsumerGroupOffsetsResult(org.apache.kafka.clients.admin.ListConsumerGroupOffsetsResult) AdminClient(org.apache.kafka.clients.admin.AdminClient)

Aggregations

HashMap (java.util.HashMap)2 ListConsumerGroupOffsetsResult (org.apache.kafka.clients.admin.ListConsumerGroupOffsetsResult)2 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1 AdminClient (org.apache.kafka.clients.admin.AdminClient)1 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 TimeoutException (org.apache.kafka.common.errors.TimeoutException)1