Kafka之consumer
1 pause
2 resume
3 wakeup 会抛出异常,正好可以退出循环
4 auto.offset.reset = latest,earlist,none none当没有消费记录时 抛出异常
5 seek 指定offset 在使用seek之前要先使用poll保证该consumer能够被分配某一个partition,然后才能够执行seek。也可以执行assignment
6 endOffsets 注意该方法返回的是即将写入的位点,而不是最后的那个数据的位点。beginningOffset,同时还有seekToEnd seekToBeginning
7 public Map<TopicPartition, OffsetAndTimestamp> offsetsForTimes(Map<TopicPartition, Long> timestampsToSearch)
Map<TopicPartition , Long> timestampToSearch = new HashMap<>() ; for (TopicPartition tp : assignment ) { timestampToSearch.put(tp , System.currentTimeMillis() - 1*24*3600*1000); Map<TopicPartition , OffsetAndTimestamp> offsets = consumer.offsetsForTimes(timestampToSearch) ; for (TopicPartition tp : assignment) { OffsetAndTimestamp offsetAndTimestamp = offsets.get(tp) ; if (offsetAndTimestamp ! = null) { consumer.seek (tp, off setAndTimestamp.offset() };
8 想退出consumer的poll,最好最安全的做法就是通过另外的线程调用wakeup,抛出异常是捕获,同时在finally中close
9 指定想消费的某一个partition 可以这么写
Set<TopicPartition> topicPartitionSet = new HashSet<>(); topicPartitionSet.add(new TopicPartition(topic,0)); consumer.assign(topicPartitionSet);