How can we consume custom messages from multiple or a single partition in a Kafka topic?
How can I read only 1 message when I consume messages from multiple partitions in a Kafka topic?
You can set the max.poll.records
consumer property to set an upper bound on the number of records returned in a single poll.
An alternative (and more general) approach would be to read any number of messages, but only commit the offset up to a certain point, so that a fewer messages get 'consumed'. Remember, in Kafka messages never actually get consumed in the sense that they don't get purged when read — there is an illusion of consumption by persisting the topic-partition offsets on a consumer-group basis.
kafka-console-consumer --topic foo --bootstrap-servers kafka:9092 --partition=0 --offset=0 --max-messages=1
– OneCricketeer