kafkacat is really useful tool to deal with Kafka. In this lab, we will setup Kafkacat and use it
15 minutes
On ubuntu systems:
$ sudo apt update
$ sudo apt install -y kafkacat
$ kafkacat -L -b localhost:9092
On terminal-1 start Kafkacat in producer mode
$ kafkacat -P -b localhost:9092 -t test
On another terminal(2) start kafkacat in consumer mode
# wait for new messages
$ kafkacat -C -b localhost:9092 -t test
Then go ahead and type some messages in terminal-1 (producer) and observe the output on consumer terminal (2)
Close the kafkacat consumer by hitting Ctrl+C on terminal-2
And try the following:
$ kafkacat -q -C -b localhost:9092 -t test -f 'Partition %t[%p], offset: %o, key: %k, value: %s\n'
Produce messages, read from STDIN to topic test
$ kafkacat -P -b localhost:9092 -t test
Or you can pipe a file into a topic
$ cat README.txt | kafkacat -P -b localhost:9092 -t test
Producing with key-values
$ kafkacat -P -b localhost:9092 -t test -K :
# Then type
# k1:v1
# k2:v2
Produce from a file. Say our file data.txt
has data in this format:
k1:v1
k2:v2
k3:v3
Send it like this
$ kafkacat -P -b localhost:9092 -t test -K: -l data.txt
# quiet mode
$ kafkacat -q -C -b localhost:9092 -t test
# wait for new messages
$ kafkacat -C -b localhost:9092 -t test
# exit after reading all messages
$ kafkacat -C -b localhost:9092 -t test -e
# only read last 10 messages and exit
$ kafkacat -C -b localhost:9092 -t test -o -10 -e
More formatting options
# print key, value
$ kafkacat -C -b localhost:9092 -t test -f 'key: %k, value: %s \n'
# more detailed output
$ kafkacat -C -b localhost:9092 -t test -f 'Topic %t[%p], offset: %o, key: %k, value: %s, (length: %S bytes) \n'
Print timestamp of messages (%T)
$ kafkacat -q -C -b localhost:9092 -t test -f 'Partition %t[%p], offset: %o, timestamp: %T, key: %k, value: %s\n'
More usage details see Kafkacat page