kafka-console-consumer CLI reads data from Kafka topics and outputs it to standard output. It’s essential for testing, debugging, and verifying message delivery.
What you’ll learn:
- How to consume messages from a topic
- How to read historical vs real-time messages
- How to display keys, timestamps, and metadata
- Common consumer options and troubleshooting
How to consume data in a Kafka topic using the CLI?
To consume data from a Kafka topic, we need to provide the mandatory parameters:- Find your Kafka hostname and port e.g.,
localhost:9092 - Provide the mandatory parameters: topic name
- If you need to read historical data, use the
--from-beginningoption - Otherwise, you will only be reading future data
- Use the
kafka-console-consumer.shCLI
Example
Make sure you have followed the Kafka producer CLI tutorial to create and send data to a Kafka topic first. Consuming only the future messages of a Kafka topic--from-beginning option
Ctrl+C.
Command output
String).
By default, the Kafka console consumer does not show the key, or any partition information.
If you do not see any output but you know your Kafka topic has data in it, don’t forget to use the --from-beginning option.
Gotchas
Here are the common mistakes and caveats with thekafka-console-consumer.sh command:
Messages by default will not display the key or metadata information (see below for how to do it).
- When you start a
kafka-console-consumer, unless specifying the--from-beginningoption, only future messages will be displayed and read - If the topic does not exist, the console consumer will automatically create it with default settings
- You can consume multiple topics at a time with a comma-delimited list or a pattern
- If a consumer group id is not specified, the
kafka-console-consumergenerates a random consumer group - If messages do not appear in order, remember that the order is at the partition level, not at the topic level
Extra important options you can set (advanced)
--from-beginning
We won’t repeat this one enough. To read all historical messages
--formatter
To display messages in a particular format (example below to display keys)
--consumer-property
To pass in any consumer property, such as the allow.auto.create.topics setting
--group
By default a random consumer group ID is chosen, but you can override it with this option. See the demo in the Kafka consumers in group CLI tutorial.
--max-messages
Number of messages to consume before exiting
--partition
If you want to only consume from a specific partition.
How to consume a Kafka topic and show both the key and value using the Kafka console consumer CLI?
By default, the console consumer will show only the value of the Kafka record. Using this command you can show both the key and value. Using theformatter kafka.tools.DefaultMessageFormatter and using the properties print.timestamp=true print.key=true print.value=true:
print.partitionprint.offsetprint.headerskey.separatorline.separatorheaders.separator
Quick reference
| Operation | Command |
|---|---|
| Consume new messages | kafka-console-consumer --bootstrap-server localhost:9092 --topic NAME |
| Consume all messages | ... --from-beginning |
| Show keys | ... --property print.key=true |
| Show all metadata | ... --property print.timestamp=true --property print.key=true --property print.partition=true --property print.offset=true |
| Limit messages | ... --max-messages 10 |
| Specific partition | ... --partition 0 |
See it in practice with ConduktorConduktor Console provides a visual interface for consuming messages with built-in deserialization, filtering, and search capabilities.
Next steps
- Learn about consumer groups for parallel consumption
- Manage consumer group offsets
- Understand consumer concepts in depth