0
votes

I'm creating a new Kafka consumer like this in Java (some code omitted for brevity)

    final Properties props = new Properties();
    props.put(ConsumerConfig.GROUP_ID_CONFIG, "group2");

    final Consumer<Long, String> consumer = new KafkaConsumer<>(props);
    consumer.subscribe(Collections.singletonList("topicname"));

This creates also the consumer group automatically, in case it does not yet exist. The problem is that the offset of this consumer group is not at the beginning of the topic, but at the end.

How can I ensure that the offset is at 0 when group is created (but not otherwise)? I don't want to manually track the offset, just set it to 0 when creating the consumer if the consumer group does not yet exist.

1

1 Answers

1
votes

If you didn't specify any value for auto.offset.reset in the consumer config, it is default to "latest" offset.

You need to set it to "earliest" if you want to consume from offset 0:

props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");