Sending multiline messages to Kafka

Published by in Kcat (Kafkacat), Apache Kafka, Multiline at https://rmoff.net/2018/09/04/sending-multiline-messages-to-kafka/

(SO answer repost)

You can use kafkacat to send messages to Kafka that include line breaks. To do this, use its -D operator to specify a custom message delimiter (in this example /):

kafkacat -b kafka:29092 \
        -t test_topic_01 \
        -D/ \
        -P <<EOF
this is a string message 
with a line break/this is 
another message with two 
line breaks!
EOF

Note that the delimiter must be a single byte - multi-byte chars will end up getting included in the resulting message See issue #140

Resulting messages, inspected also using kafkacat:

$ kafkacat -b kafka:29092 -C \
         -f '\nKey (%K bytes): %k\t\nValue (%S bytes): %s\n\Partition: %p\tOffset: %o\n--\n' \
         -t test_topic_01

Key (-1 bytes):
Value (43 bytes): this is a string message
with a line break
Partition: 0    Offset: 0
--

Key (-1 bytes):
Value (48 bytes): this is
another message with two
line breaks!

Partition: 0    Offset: 1
--
% Reached end of topic test_topic_01 [0] at offset 2

Inspecting using kafka-console-consumer:

$ kafka-console-consumer \
    --bootstrap-server kafka:29092 \
    --topic test_topic_01 \
    --from-beginning

this is a string message
with a line break
this is
another message with two
line breaks!

(thus illustrating why kafkacat is nicer to work with than kafka-console-consumer because of its optional verbosity :) )


Read more about kafkacat here