How to read data using Kafka Consumer API from beginning?

前端 未结 10 2041
清歌不尽
清歌不尽 2020-12-05 02:06

Please can anyone tell me how to read messages using the Kafka Consumer API from the beginning every time when I run the consumer.

10条回答
  •  天命终不由人
    2020-12-05 02:23

    One possible solution is to use an implementation of ConsumerRebalanceListener while subscribing to one or more topics. The ConsumerRebalanceListener contains callback methods when new partitions are assigned or removed from a consumer. The following code sample illustrates this :

    public class SkillsConsumer {
    
    private String topic;
    
    private KafkaConsumer consumer;
    
    private static final int POLL_TIMEOUT = 5000;
    
    public SkillsConsumer(String topic) {
        this.topic = topic;
        Properties properties = ConsumerUtil.getConsumerProperties();
        properties.put("group.id", "consumer-skills");
        this.consumer = new KafkaConsumer<>(properties);
        this.consumer.subscribe(Collections.singletonList(this.topic),
                new PartitionOffsetAssignerListener(this.consumer));
        }
    }
    
    public class PartitionOffsetAssignerListener implements ConsumerRebalanceListener {
    
    private KafkaConsumer consumer;
    
    public PartitionOffsetAssignerListener(KafkaConsumer kafkaConsumer) {
        this.consumer = kafkaConsumer;
    }
    
    @Override
    public void onPartitionsRevoked(Collection partitions) {
    
    }
    
    @Override
    public void onPartitionsAssigned(Collection partitions) {
        //reading all partitions from the beginning
        for(TopicPartition partition : partitions)
            consumer.seekToBeginning(partition);
    }
    

    }

    Now whenever the partitions are assigned to the consumer, each partition will be read from the beginning.

提交回复
热议问题