繁体   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