kafka-console-consumer.sh: Read Topic Messages
kafka-console-consumer.sh --topic <name> --bootstrap-server <host:port> prints messages from a topic to stdout, optionally from the earliest offset.
To assert that your service published the expected event, read the topic back with the console consumer. The key flags in CI bound how long it waits and how many messages it takes.
What it does
kafka-console-consumer.sh subscribes to a topic and prints each record value to stdout. By default it starts at the latest offset and streams forever; --from-beginning reads existing messages and --max-messages plus --timeout-ms make it terminate so a script can move on.
Common usage
# read all existing messages then keep waiting
kafka-console-consumer.sh --topic orders \
--bootstrap-server localhost:9092 --from-beginning
# read exactly 5 messages then exit (good for assertions)
kafka-console-consumer.sh --topic orders \
--bootstrap-server localhost:9092 --from-beginning \
--max-messages 5 --timeout-ms 10000Options
| Flag | What it does |
|---|---|
| --topic <name> | Topic to consume from |
| --bootstrap-server <host:port> | Broker to connect to |
| --from-beginning | Start at the earliest offset instead of latest |
| --max-messages <n> | Exit after consuming n messages |
| --timeout-ms <ms> | Exit if no message arrives within this window |
| --group <id> | Join a specific consumer group |
| --property print.key=true | Also print the message key |
In CI
Without --from-beginning the consumer only sees messages produced after it starts, so an assertion on earlier data reads nothing; add --from-beginning. Always pair --max-messages with --timeout-ms so the step cannot hang the whole job when the expected message never arrives.
Common errors in CI
"Consumed 0 messages" then the process hangs means the messages predate the consumer and you forgot --from-beginning, or the topic is empty. "TimeoutException" after --timeout-ms means nothing was produced in time. "Connection to node -1 ... could not be established" is the broker being down.