简体   繁体   English

当在Kafka中使用邮件时,为什么不更新偏移量

[英]Why doesn't offset get updated when messages are consumed in Kafka

I am implementing Kafka consumer class to receive messages. 我正在实现Kafka消费者类以接收消息。 I wanted to only get the new messages every time. 我只想每次都收到新消息。 Therefore, I set enable.auto.commit true. 因此,我将enable.auto.commit设置enable.auto.commit true。 However the offset does not seem to change at all. 但是,偏移量似乎根本没有改变。 Even though the topic, consumer group and partition has been always the same. 即使主题,消费者组和分区始终相同。

Here is my consumer code: 这是我的消费者代码:

    consumerConfig.put("bootstrap.servers", bootstrap);
    consumerConfig.put("group.id", KafkaTestConstants.KAFKA_GROUP);
    consumerConfig.put("enable.auto.commit", "true");
    consumerConfig.put("auto.offset.reset", "earliest");
    consumerConfig.put("auto.commit.interval", 1000);
    consumerConfig.put("key.deserializer", StringDeserializer.class.getName());
    consumerConfig.put("value.deserializer", StringDeserializer.class.getName());
    StringDeserializer deserializer = new StringDeserializer();
    KafkaConsumer<String, String> kafkaConsumer = new KafkaConsumer<>(consumerConfig, deserializer, deserializer);

    TopicPartition tp = new TopicPartition(KafkaTestConstants.KAFKA_TOPIC, 0);
    List<TopicPartition> tps = Arrays.asList(tp);
    kafkaConsumer.assign(tps);
    long offset = kafkaConsumer.position(tp);
    System.out.println("Offset of partition: " + offset);

    List<String> messages = new ArrayList<String>();
    ConsumerRecords<String, String> records = kafkaConsumer.poll(100);

    for (ConsumerRecord<String, String> record : records) {
        System.out.println("Message received: " + record.value());
        messages.add(record.value());
    }

    consumer.commitAsync();
    System.out.println("Offset commited.\n");
    consumer.close();

No matter how many times I run it, it always shows the offset is 0. Therefore, it always receive all messages from the very beginning. 无论我运行多少次,它始终显示偏移量为0。因此,它始终从一开始就接收所有消息。 What am I missing? 我想念什么?

EDIT: Based on Matthias's answer, I decided to manually commit the offset. 编辑:基于Matthias的答案,我决定手动提交偏移量。 However commitSync() would hang. 但是commitSync()会挂起。 commitAsync() sort of works. commitAsync()类的工作。 I will explain the "sort of" later. 稍后我将解释“排序”。 Here is what the code does: 代码是这样的:

producer send 2 messages;
consumer initiates;
print out current position;
consumer.poll();
print received messages;
consumer.commitAsync();

This is how this code behaves. 这段代码就是这样。 Say I have 100 messages. 假设我有100条消息。 Now producers sends 2 new messages. 现在,生产者发送了2条新消息。 Before consumer poll, it would show current offset position as 102 which is supposed to be 100. Therefore, no new messages will be printed out. 在进行消费者调查之前,它将显示当前偏移位置为102,应该为100。因此,不会打印出新消息。 It is almost like the offset is updated after producer sent the messages. 几乎就像偏移量是在生产者发送消息后更新的一样。

Auto commit only works if you use consumer group management, and for this, you need to "subscribe" to a topic, but not "assign" partitions manually. 自动提交仅在使用使用者组管理时有效,为此,您需要“订阅”一个主题,而不能手动“分配”分区。

Compare the JavaDocs of KafkaConsumer . 比较KafkaConsumer的JavaDocs。 It's a long read, but required to understand the subtle details on how the use the consumer correctly: https://kafka.apache.org/0102/javadoc/index.html?org/apache/kafka/clients/consumer/KafkaConsumer.html 这是一本很长的书,但是需要了解有关如何正确使用消费者的细微细节: https//kafka.apache.org/0102/javadoc/index.html? org / apache / kafka / clients / consumer / KafkaConsumer。 html

Furthermore, if auto-commit is enabled, it will commit within poll (ie, a call to poll() might commit the messages return from the previous call to poll() ) and not when you iterate through the returne messages. 此外,如果启用了自动提交,它将在poll提交(即,对poll()的调用可能会提交从上一次对poll()调用返回的消息),而不是在您遍历返回的消息时提交。 This also means, that your commits will "jump" forward, like from committed offset 0 to 100 (if you received 100 messages by poll for a single partition). 这也意味着您的提交将向前“跳跃”,例如从提交的偏移量0到100(如果您通过轮询收到了单个分区的100条消息)。

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

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