从kafka offset中只获取一条记录

Ans*_*me2 2 apache-kafka kafka-consumer-api

我知道可以通过使用 kafka-console-consumer 中的 --offset 从特定偏移量的 kafka 读取数据,但这给出了从该特定偏移量到当前偏移量的记录。我怎样才能将其限制为只有一条记录。

ped*_*mss 5

如果您使用--helponkafka-console-consumer命令,您可以看到:

--max-messages <Integer: num_messages>   The maximum number of messages to      
                                           consume before exiting. If not set,  
                                           consumption is continual.            
--offset <String: consume offset>        The offset id to consume from (a non-  
                                           negative number), or 'earliest'      
                                           which means from beginning, or       
                                           'latest' which means from end        
                                           (default: latest)       
Run Code Online (Sandbox Code Playgroud)

--max-messages设置为1就是你想要的