Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache kafka 如何从卡夫卡主题获取最近的消息_Apache Kafka_Kafka Consumer Api - Fatal编程技术网

Apache kafka 如何从卡夫卡主题获取最近的消息

Apache kafka 如何从卡夫卡主题获取最近的消息,apache-kafka,kafka-consumer-api,Apache Kafka,Kafka Consumer Api,我们是否有任何选择,比如从卡夫卡主题获取最近10/20/等消息。我可以看到--from start选项来获取主题中的所有消息,但如果我只想获取少量消息,则可以选择first、last、middle或latest 10。我们有一些选择吗?前N条消息 您可以使用--max messages N来获取主题的第一条N消息 例如,要获取前10条消息,请运行 bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic tes

我们是否有任何选择,比如从卡夫卡主题获取最近10/20/等消息。我可以看到--from start选项来获取主题中的所有消息,但如果我只想获取少量消息,则可以选择first、last、middle或latest 10。我们有一些选择吗?

前N条消息 您可以使用
--max messages N
来获取主题的第一条
N
消息

例如,要获取前10条消息,请运行

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test --from-beginning  --max-messages 10
接下来的N条消息 最后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
如果您不想坚持使用二进制文件,我建议您使用一个具有更多选项和功能的Kafka命令行工具



有关更多详细信息,请参阅文章

,在不指定偏移量和分区的情况下,您只能使用下一个N或前一个N。要使用无界流的“中间”,您需要给出偏移量

除了控制台消费者,还有卡夫卡卡特

前二十

kafkacat-C-b-t主题-o最早-C 20

和前二十个(从零分区开始)


kafkacat-C-b-t topic-p0-o-20

您使用的是什么客户端?这是您正在编写的程序的一部分,还是您只想从命令行浏览数据?请阅读本文档以了解
kafka偏移量
。如果像前两种情况一样,假设根据Offset参数获取消息,是否会替换默认的Offset?我的意思是,如果我们通过提到一个特定的偏移量值来获取消息,然后如果我想从最初的偏移量值获取消息,不备份上一个偏移量是可能的吗?使用kafkacat比控制台生产者/消费者有什么优势?在机器上运行kafkacat的基本要求是什么?kafkacat是一个C库,因此您不需要像其他Kafka工具一样使用Java,如果您查看其github页面,您可以在@Prabhu中找到其他优势
bin/kafka-simple-consumer-shell.sh --bootstrap-server localhost:9092 --topic test--partition testPartition --offset yourOffset --max-messages 10