Search code examples
apache-kafkakafka-consumer-apikafka-producer-api

How to send 1 Kafka message with a key?


I am using this command: bin/kafka-console-producer.sh --bootstrap-server url --topic Notifications --property "parse.key=true" --property "key.separator=:"

I enter this command, and enter:

1: {"producer": 4, "message": {"created_date": "2020-08-04 15:03:19.063196"}}

Then it immediately expects a second line. But I only want to send 1 message, so I hit enter again, and get

org.apache.kafka.common.KafkaException: No key found on line 2: q at kafka.tools.ConsoleProducer$LineMessageReader.readMessage(ConsoleProducer.scala:289) at kafka.tools.ConsoleProducer$.main(ConsoleProducer.scala:51) at kafka.tools.ConsoleProducer.main(ConsoleProducer.scala)


Solution

  • I think that the message is actually sent. kafka-console-producer accepts the input line by line. Every line (when you enter a newline character by hitting [Enter]) submits a message to the topic. After that kafka-console-producer continues to wait for the next input, should you send more messages. However, because for the next input you attempt to send an empty string, it doesn't parse indeed and the error is produced. This error, though, is for the second entry only. The first message should have been, normally, already sent by that time.

    As @Patrick Kelly suggest in the comments, you may confirm this assumption by running a consumer on the topic in question and see what's in there. For example, run the kafka-console-consumer whilst entering the messages in the kafka-console-producer prompt.