簡體   English   中英

卡夫卡消費者從一開始就沒有消費

[英]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-beginning :如果消費者還沒有確定的消費偏移量,則從日志中最早的消息而不是最新消息開始。

Kafka 消費者第一次使用--from-beginning如果你重試我懷疑你做了,它將從它離開的地方開始。 您可以使用以下任何選項再次使用該消息

  1. 使用以下重置消費者組偏移量

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

  1. 使用將從起點開始消費的新消費者 ID

kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic edu-topic --group new-edu-service --from-beginning

  1. 您也可以使用偏移量來使用分區中的下 N 條消息

kafka-console-consumer.sh --bootstrap-server localhost:9092 --offset 0 --partition 0 --topic edu-topic

--offset <String: consume offset>: 要消費的偏移id(一個非負數),或者'earliest'表示從頭開始,或者'latest'表示從結束(默認值:latest)
--partition <Integer: partition>:除非指定了'--offset',否則從Consumption消費的分區從分區的末尾開始。

國旗

--from-begining

將影響您的 GroupConsumer 第一次啟動/創建時的行為,或者存儲的(上次提交的消費)偏移量已過期(或者當您嘗試重置存儲的偏移量時)。

否則 GroupConsumer 將繼續在存儲的(最后提交的)偏移處。

請考慮從手冊中獲取更多信息。

因為您使用的是舊的消費者組。 --from-beginning僅適用於其組名尚未記錄在 Kafka 集群上的新消費者組。

要從頭開始重新消費,您可以:

  • 使用標志--from-beginning啟動一個新的消費者組(更改組名)
  • 重置此消費者組的偏移量。 我還沒試過,但你可以在這里測試

只需添加 --from-beginning

但要知道,從一開始的消息就不是有序的

如果您為同一主題使用了多個分區。 訂單僅在分區級別得到保證。 (對於同一個分區)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM