How to read data using Kafka Consumer API from beginning?

前端 未结 10 2053
清歌不尽
清歌不尽 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:18

    To always read from offset 0 without creating new groupId everytime.

        // ... Assuming the props have been set properly.
        // ... enable.auto.commit and auto.offset.reset as default
    
        KafkaConsumer consumer = new KafkaConsumer<>(props);
        consumer.subscribe(Collections.singletonList(topic));
        consumer.poll(0);  // without this, the assignment will be empty. 
        consumer.assignment().forEach(t -> {
            System.out.printf("Set %s to offset 0%n", t.toString());
            consumer.seek(t, 0);
        });
        while (true) {
         // ... consumer polls messages as usual.
        }
    

提交回复
热议问题