I want to push a text consisting of multiple lines as one message into a kafka topic.
After I enter:
kafka-console-producer --broker-list localhost:9092 --topic myTopic
and copy my text:
My Text consists of:
two lines instead of one
I get two messages in the kafka topic, but I want to have just one. Any ideas how to achieve that? Thanks
You can use kafkacat
for this, with 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 :) )