简体   繁体   English

Kafka使用者组,在创建使用者组时将偏移设置为0

[英]Kafka consumer group, set offset to 0 when consumer group is created

I'm creating a new Kafka consumer like this in Java (some code omitted for brevity) 我正在用Java创建一个新的Kafka使用者(为简洁起见,省略了一些代码)

    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)? 创建组时,如何确保偏移量为0(但不是)? 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. 我不想手动跟踪偏移量,如果消费者组尚不存在,则在创建消费者时将其设置为0即可。

If you didn't specify any value for auto.offset.reset in the consumer config , it is default to "latest" offset. 如果未在使用者配置中auto.offset.reset指定任何值,则默认为“最新”偏移量。

You need to set it to "earliest" if you want to consume from offset 0: 如果要从偏移量0开始消耗,则需要将其设置为“最早”:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM