kafka resets to latest offset offset

My brother recently used kafka to test a single consumer set to transmit data. Unexpectedly, the consumer hangs up the night before and restarts the consumer. Because auto.offset.reset defaults to latest, the consumer continues to consume from the data last night, because the difference After a night, the consumer will not be able to catch up with the producer for a while, and I need to display the data in real time, and I cannot restart the consumer and assign the group.id again every time. So you need to manually modify the offset to the latest.

Finally solved the problem with the following code

  

Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "test");
…………
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);

TopicPartition partition0 = new TopicPartition("topicName", 0);


consumer.assign(Arrays.asList(partition0,partition1,partition2));

// consumer.seek(partition0, 220);
consumer.seekToEnd(Arrays.asList(partition0));

  
}

solve!

Refer to the blog of the third degree of coolness: https://blog.csdn.net/yu280265067/article/details/69390094

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326117282&siteId=291194637