我们是否有任何选项,例如从 Kafka 主题获取最近 10/20 等消息。我可以看到 --from-beginning 选项从主题中获取所有消息,但如果我只想获取第一个、最后一个、中间或最新的几条消息 10. 我们有一些选择吗?
前 N 条消息
您可以使用--max-messages N
为了获取第一个N
某个主题的消息。
例如,要获取前 10 条消息,请运行
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 条消息,您需要定义特定的分区和偏移量:
bin/kafka-simple-consumer-shell.sh --bootstrap-server localhost:9092 --topic test--partition testPartition --offset yourOffset
M 到 N 条消息
同样,对于这种情况,您必须定义分区和偏移量。
例如,您可以运行以下命令来获取从您选择的偏移量开始的 N 条消息:
bin/kafka-simple-consumer-shell.sh --bootstrap-server localhost:9092 --topic test--partition testPartition --offset yourOffset --max-messages 10
如果您不想坚持使用二进制文件,我建议您使用kt https://github.com/fgeller/kt这是一个具有更多选项和功能的 Kafka 命令行工具。
更详细的内容可以参考文章如何在 Apache Kafka 中获取特定消息 https://betterprogramming.pub/how-to-fetch-specific-messages-in-apache-kafka-4133dad0b4b8
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)