Menu

Put a test message on a Kafka topic without writing a producer first
📰

Put a test message on a Kafka topic without writing a producer first

DEV Community: java·Yushin Kim·about 19 hours ago
#np5VuGRF
#devtojava#devto#dev#headers#message
Reading 0:00
15s threshold

You wrote a consumer and want to see it handle one message. Writing a throwaway producer app for that is slow. Here are three ways to do it, from fastest to most realistic.

1. One message with kcat

kcat reads from stdin and produces to a topic. -K: tells it the key and value are separated by a colon, and -H adds a header.

echo 'user_1001:{"eventId":"evt_001","userId":"user_1001","status":"ACTIVE"}' \ | kcat -b localhost:9092 -t user.events -P -K: \ -H "eventType=USER_CREATED" -H "version=v1"

Enter fullscreen mode

Exit fullscreen mode

Two things trip people up:

The delimiter splits at its first occurrence. With -K: a key such as user:1001 is cut at the first colon. Pick a delimiter that cannot appear in the key, for example -K'|' .

Quote the JSON with single quotes so the shell leaves the double quotes alone.

2.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.