簡體   English   中英

我怎么知道我已經消耗了所有的卡夫卡主題?

[英]How can I know that I have consumed all of a Kafka Topic?

我正在使用Flink v1.4.0。 我正在按照以下代碼使用Kafka FLink Consumer使用來自Kafka主題的數據:

Properties properties = new Properties();
properties.setProperty("bootstrap.servers", "localhost:9092");
// only required for Kafka 0.8
properties.setProperty("zookeeper.connect", "localhost:2181");
properties.setProperty("group.id", "test");
DataStream<String> stream = env
.addSource(new FlinkKafkaConsumer08<>("topic", new SimpleStringSchema(), properties));

final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

FlinkKafkaConsumer08<String> myConsumer = new FlinkKafkaConsumer08<>(...);
myConsumer.setStartFromEarliest();     // start from the earliest record possible
myConsumer.setStartFromLatest();       // start from the latest record
myConsumer.setStartFromGroupOffsets(); // the default behaviour

DataStream<String> stream = env.addSource(myConsumer);
...

有沒有辦法知道我是否已消耗掉整個主題? 如何監控偏移量? (這是確認我已使用Kafka主題中所有數據的適當方法嗎?)

由於Kafka通常用於連續的數據流,因此消耗主題的“全部”可能是有意義的概念,也可能不是有意義的概念。 我建議您查看有關Flink如何公開Kafka指標文檔 ,其中包括以下說明:

The difference between the committed offset and the most recent offset in 
each partition is called the consumer lag. If the Flink topology is consuming 
the data slower from the topic than new data is added, the lag will increase 
and the consumer will fall behind. For large production deployments we 
recommend monitoring that metric to avoid increasing latency.

因此,如果消費者滯后時間為零,那么您就被趕上了。 就是說,您可能希望自己能夠比較偏移量,但是我不知道這樣做的簡單方法。

Kafka它用作流媒體源,並且流媒體沒有盡頭。

如果我沒記錯,則Flink的Kafka連接器每隔X毫秒就會從主題中提取一次數據,因為所有kafka使用者都是Active使用者,Kafka不會在主題中有新數據時通知您

因此,在您的情況下,只需設置一個超時時間,如果您在這段時間內不讀取數據,那么您已經讀取了主題內的所有數據。

無論如何,如果您需要讀取一批有限數據,則可以使用Flink的某些Windows或在Kafka主題內引入某種標記,以界定批處理的開始和結束。

暫無
暫無

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

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