从kafka偏移量中只获取一条记录

get only one record from kafka offset

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

如果你在 kafka-console-consumer 命令上使用 --help 你可以看到:

--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)       

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