Using Console Tools

1. Producing Messages

Tool Description Detail
kafka-console-producer Read stdin → topic One line = record
--topic Target topic Required
EOF Ctrl-D to end Stop

Example: Console produce

kafka-console-producer.sh --bootstrap-server localhost:9092 --topic orders

2. Consuming Messages

Tool Description Detail
kafka-console-consumer Topic → stdout Live tail
--topic Source topic Required
Default Latest offset New only

Example: Console consume

kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders

3. Reading from Beginning

Flag Description Detail
--from-beginning Earliest offset Full history
No Group Ignores committed Fresh read
--max-messages Limit output Stop after N

Example: From beginning

kafka-console-consumer.sh --bootstrap-server localhost:9092 \
  --topic orders --from-beginning --max-messages 10

4. Specifying Consumer Group

Flag Description Detail
--group Join named group Commit offsets
Resume From committed offset Continue
Share Load Multiple consumers Balanced

Example: With group

kafka-console-consumer.sh --bootstrap-server localhost:9092 \
  --topic orders --group debug-group

5. Setting Message Format

Flag Description Detail
--property print.key Show keys true/false
--property key.separator Key/value delimiter Default tab
--formatter Custom formatter class Avro etc.

Example: Print key and value

kafka-console-consumer.sh --bootstrap-server localhost:9092 \
  --topic orders --property print.key=true --property key.separator=" : "

6. Using Key Deserializer

Flag Description Detail
key.deserializer Decode keys --property
value.deserializer Decode values --property
Use Long/Avro keys Non-string

Example: Long key deserializer

kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders \
  --property print.key=true \
  --property key.deserializer=org.apache.kafka.common.serialization.LongDeserializer

7. Setting Partition

Flag Description Detail
--partition Read single partition Targeted
No Group Manual assignment Required
Use Debug specific partition Inspect

Example: Specific partition

kafka-console-consumer.sh --bootstrap-server localhost:9092 \
  --topic orders --partition 2 --from-beginning

8. Reading Specific Offset

Flag Description Detail
--offset Start offset Number/earliest/latest
Requires --partition set Together
Use Replay from point Debug

Example: Read from offset 500

kafka-console-consumer.sh --bootstrap-server localhost:9092 \
  --topic orders --partition 0 --offset 500

9. Displaying Message Timestamp

Flag Description Detail
print.timestamp Show record timestamp --property
print.partition Show partition --property
print.offset Show offset --property

Example: Show metadata

kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic orders \
  --property print.timestamp=true --property print.offset=true

10. Using Timeout

Flag Description Detail
--timeout-ms Exit if idle No new records
Use Scripted reads Bounded
Combine With --max-messages Safe

Example: Timeout

kafka-console-consumer.sh --bootstrap-server localhost:9092 \
  --topic orders --from-beginning --timeout-ms 5000