Created
December 26, 2017 09:47
Used to fetch the lowest offset for each partition of a topic.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cn.sky_data.storeData.util; | |
import lombok.AllArgsConstructor; | |
import lombok.Data; | |
import org.apache.kafka.clients.consumer.ConsumerRecords; | |
import org.apache.kafka.clients.consumer.KafkaConsumer; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
public class KafkaOffsetRangeUtil { | |
public List<TopicPartitionInfo> fetch(Map<String, Object> config, List<String> topics) { | |
KafkaConsumer<String, byte[]> consumer = new KafkaConsumer<>(config); | |
consumer.subscribe(topics); | |
ConsumerRecords<String, byte[]> records = consumer.poll(1000); | |
return records.partitions().parallelStream().map(topicPartition -> { | |
consumer.seekToBeginning(Collections.singletonList(topicPartition)); | |
long offset = consumer.position(topicPartition); | |
return new TopicPartitionInfo(topicPartition.topic(), topicPartition.partition(), offset); | |
}).collect(Collectors.toList()); | |
} | |
@Data | |
@AllArgsConstructor | |
public static class TopicPartitionInfo { | |
private String topic; | |
private int partition; | |
private long offset; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment