Kafka消费者从一开始就没有消费
Kafka consumer not consuming from beginning
我在本地机器上安装了 Kafka,并启动了 zookeeper 和单个代理服务器。
现在我有一个主题,描述如下:
~/Documents/backups/kafka_2.12-2.2.0/data/kafka$ kafka-topics.sh --zookeeper 127.0.0.1:2181 --topic edu-topic --describe
Topic:edu-topic PartitionCount:3 ReplicationFactor:1 Configs:
Topic: edu-topic Partition: 0 Leader: 0 Replicas: 0 Isr: 0
Topic: edu-topic Partition: 1 Leader: 0 Replicas: 0 Isr: 0
Topic: edu-topic Partition: 2 Leader: 0 Replicas: 0 Isr: 0
我有一个生产者,它在消费者启动之前产生了一些消息,如下所示:
~/Documents/backups/kafka_2.12-2.2.0/data/kafka$ kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic edu-topic
>book
>pen
>pencil
>marker
>
当我使用 --from-beginning 选项启动消费者时,它没有显示生产者产生的所有消息:
~/Documents/backups/kafka_2.12-2.2.0/data/kafka$ kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic edu-topic --group edu-service --from-beginning
但是,它显示的是新添加的消息。
我这是怎么了?有什么帮助吗?
旗帜
--from-begining
将在第一次 started/created 时影响您的 GroupConsumer 的行为,或者存储的(最后提交的消费)偏移量已过期(或者可能在您尝试重置存储的偏移量时)。
否则 GroupConsumer 将只继续存储的(最后提交的)偏移量。
请考虑从手册中获取更多信息。
因为你用的是老消费群。 --from-beginning
只对新的消费组有效,该消费组的组名还没有记录在Kafka集群上
要从头开始重新消费,您可以:
- 使用标志
--from-beginning
启动一个新的消费者组(更改组名)
- 重置该消费者组的偏移量。我还没试过,但你可以测试一下 here
--from-beginning: If the consumer does not already have an established offset to consume from, start with the earliest message
present in the log rather than the latest message.
Kafka 消费者第一次使用 --from-beginning 如果你重试,我怀疑你做了,它将从它离开的地方开始。您可以使用以下任何选项再次使用该消息
- 使用以下方法重置消费者组偏移量
kafka-streams-application-reset.sh --application-id edu-service
--input-topics edu-topic --bootstrap-servers localhost:9092 --zookeeper 127.0.0.1:2181
然后从头重试
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic edu-topic --group edu-service --from-beginning
- 使用新的consumer id,从起点开始消费
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic edu-topic --group new-edu-service --from-beginning
- 您也可以使用偏移量来消费来自分区的下 N 条消息
kafka-console-consumer.sh --bootstrap-server localhost:9092 --offset 0 --partition 0 --topic edu-topic
--offset <String: consume offset> : The offset id to consume from (a non- negative number), or 'earliest' which means from beginning, or
'latest' which means from end (default: latest)
--partition <Integer: partition> : The partition to consume from Consumption starts from the end of the partition unless '--offset'
is specified.
只需添加
--from-beginning
But do know that messages from the beginning would not be in order
如果您为同一主题使用了多个分区。
仅在分区级别保证顺序。 (对于同一分区)
我在本地机器上安装了 Kafka,并启动了 zookeeper 和单个代理服务器。
现在我有一个主题,描述如下:
~/Documents/backups/kafka_2.12-2.2.0/data/kafka$ kafka-topics.sh --zookeeper 127.0.0.1:2181 --topic edu-topic --describe
Topic:edu-topic PartitionCount:3 ReplicationFactor:1 Configs:
Topic: edu-topic Partition: 0 Leader: 0 Replicas: 0 Isr: 0
Topic: edu-topic Partition: 1 Leader: 0 Replicas: 0 Isr: 0
Topic: edu-topic Partition: 2 Leader: 0 Replicas: 0 Isr: 0
我有一个生产者,它在消费者启动之前产生了一些消息,如下所示:
~/Documents/backups/kafka_2.12-2.2.0/data/kafka$ kafka-console-producer.sh --broker-list 127.0.0.1:9092 --topic edu-topic
>book
>pen
>pencil
>marker
>
当我使用 --from-beginning 选项启动消费者时,它没有显示生产者产生的所有消息:
~/Documents/backups/kafka_2.12-2.2.0/data/kafka$ kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic edu-topic --group edu-service --from-beginning
但是,它显示的是新添加的消息。
我这是怎么了?有什么帮助吗?
旗帜
--from-begining
将在第一次 started/created 时影响您的 GroupConsumer 的行为,或者存储的(最后提交的消费)偏移量已过期(或者可能在您尝试重置存储的偏移量时)。
否则 GroupConsumer 将只继续存储的(最后提交的)偏移量。
请考虑从手册中获取更多信息。
因为你用的是老消费群。 --from-beginning
只对新的消费组有效,该消费组的组名还没有记录在Kafka集群上
要从头开始重新消费,您可以:
- 使用标志
--from-beginning
启动一个新的消费者组(更改组名)
- 重置该消费者组的偏移量。我还没试过,但你可以测试一下 here
--from-beginning: If the consumer does not already have an established offset to consume from, start with the earliest message present in the log rather than the latest message.
Kafka 消费者第一次使用 --from-beginning 如果你重试,我怀疑你做了,它将从它离开的地方开始。您可以使用以下任何选项再次使用该消息
- 使用以下方法重置消费者组偏移量
kafka-streams-application-reset.sh --application-id edu-service --input-topics edu-topic --bootstrap-servers localhost:9092 --zookeeper 127.0.0.1:2181
然后从头重试
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic edu-topic --group edu-service --from-beginning
- 使用新的consumer id,从起点开始消费
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic edu-topic --group new-edu-service --from-beginning
- 您也可以使用偏移量来消费来自分区的下 N 条消息
kafka-console-consumer.sh --bootstrap-server localhost:9092 --offset 0 --partition 0 --topic edu-topic
--offset <String: consume offset> : The offset id to consume from (a non- negative number), or 'earliest' which means from beginning, or 'latest' which means from end (default: latest)
--partition <Integer: partition> : The partition to consume from Consumption starts from the end of the partition unless '--offset' is specified.
只需添加 --from-beginning
But do know that messages from the beginning would not be in order
如果您为同一主题使用了多个分区。 仅在分区级别保证顺序。 (对于同一分区)