kafka消費者之seek方法

2021-10-25 06:07:36 字數 1096 閱讀 8490

rotected static properties initconfig()

public static void main(string args)

for (topicpartition tp : assignment)

while (isrunning.get())

}}

上面的情形是我們知道具體的消費位置,如果我們不知道具體的消費位置呢?日常開發過程中,我們可能有從某乙個時間段開始消費的場景。比如:從昨天的某個時間點開始消費

kafka提供了乙個offsetfortimes方法獲取某乙個時間的訊息的偏移量和時間戳,我們獲取到偏移量,就可以使用seek方法從某個時間段開始消費了,示例如下:

public static void main(string args) 

mapmap = new hashmap<>();

for (topicpartition tp : assignment)

mapoffsets = kafkaconsumer.offsetsfortimes(map);

for (topicpartition topicpartition : offsets.keyset())

}while (isrunning.get())

}}

public static void main(string args) 

for (topicpartition tp : assignment)

while (isrunning.get())

long lastconsumedoffset = records.get(records.size() - 1).offset();

//儲存位移

storeoffsettodb(tp,lastconsumedoffset+1);}}

}private static void storeoffsettodb(topicpartition tp, long offset)

private static long getoffsetfromdb(topicpartition tp)

kafka消費者之seek方法

rotected static properties initconfig public static void main string args for topicpartition tp assignment while isrunning.get 上面的情形是我們知道具體的消費位置,如果我們不...

kafka消費者無法消費異常

今天被乙個kafka消費異常折磨了一天,頭差點炸了,還好最後解決了它 異常 伺服器 record is corrupt 記錄損壞 不明原因 有可能磁碟空間不足導致 導致消費者無法正常消費訊息 卡在某乙個offset 不能繼續消費 解決辦法 先停掉消費者程式 殺掉程序 不可關閉kafka服務 然後手動...

kafka 主動消費 Kafka消費者的使用和原理

publicstaticvoidmain string args finally 前兩步和生產者類似,配置引數然後根據引數建立例項,區別在於消費者使用的是反序列化器,以及多了乙個必填引數 group.id,用於指定消費者所屬的消費組。關於消費組的概念在 kafka中的基本概念 中介紹過了,消費組使得...