consumer.subscribe(topicA);
consumer.poll(100);//正常订阅topic和poll消息
Set<TopicPartition> assignments = consumer.assignment();//获取consumer所分配的分区信息
Map<TopicPartition, Long> query = new HashMap<>();//构造offsetsForTimes参数,通过时间戳找到offset
for (TopicPartition topicPartition : assignments) {
System.out.println(topicPartition);
query.put(topicPartition, 1550804131000L);
}
Map<TopicPartition, OffsetAndTimestamp> result = consumer.offsetsForTimes(query);
for (Map.Entry<TopicPartition, OffsetAndTimestamp> entry : result.entrySet()) {
System.out.println(entry);
consumer.seek(entry.getKey(), entry.getValue().offset());//每个topic的partition都seek到执行的offset
}
网友评论