如何从 Kafka 主题中获取最近的消息 [英] How to fetch recent messages from Kafka topic
问题描述
我们是否有任何选择,例如从 Kafka 主题中获取最近的 10/20/等消息.我可以看到 --from-beginning 选项以从主题中获取所有消息,但如果我只想获取第一条、最后一条、中间条或最新条 10 条消息.我们有一些选择吗?
Do we have any option like fetching recent 10/20/ etc., messages from Kafka topic. I can see --from-beginning option to fetch all messages from the topic but if I want to fetch only few messages first, last, middle or latest 10. do we have some options?
推荐答案
前 N 个消息
您可以使用 --max-messages N
来获取主题的前 N
条消息.
First N messages
You can use --max-messages N
in order to fetch the first N
messages of a topic.
例如,要获取前 10 条消息,请运行
For example, to get the first 10 messages, run
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning --max-messages 10
接下来的 N 条消息
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --max-messages 10
最近的 N 条消息
要获取最后N条消息,需要定义一个特定的分区和偏移量:
Last N messages
To get the last N messages, you need to define a specific partition and the offset:
bin/kafka-simple-consumer-shell.sh --bootstrap-server localhost:9092 --topic test--partition testPartition --offset yourOffset
M 到 N 条消息
同样,对于这种情况,您必须同时定义分区和偏移量.例如,您可以运行以下命令以从您选择的偏移量开始获取 N 条消息:
M to N messages
Again, for this case you'd have to define both the partition and the offset. For example, you can run the following in order to get N messages starting from an offset of your choice:
bin/kafka-simple-consumer-shell.sh --bootstrap-server localhost:9092 --topic test--partition testPartition --offset yourOffset --max-messages 10
如果你不想坚持二进制文件,我建议你使用 kt
这是一个具有更多选项和功能的 Kafka 命令行工具.
If you don't want to stick to the binaries, I would suggest you to use kt
which is a Kafka command line tool with more options and functionality.
有关更多详细信息,请参阅文章如何在 Apache Kafka 中获取特定消息
For more details refer to the article How to fetch specific messages in Apache Kafka
这篇关于如何从 Kafka 主题中获取最近的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!