apache-kafkakafka-consumer-apikafka-producer-api

How to send key, value messages with the kafka console producer


I got a use case where I need to send key value messages with Kafka Console Producer. So how to achieve this through the kafka-console-producer command?


Solution

  • I found out the solution after some research and the solution is here.

    kafka-console-producer command

    kafka-console-producer.sh --broker-list localhost:9092 --topic topic-name --property "parse.key=true" --property "key.separator=:"
    

    After running this command you will enter in producer console and from there you can send key, value messages.

    For example

    key1:value1
    key2:value2
    key3:value3
    

    For more clarity, I am providing sample key-value message here, emp_info is a key and JSON object is a value.

    emp_info: {"emp_id":100,"first_name":"Keshav","last_name":"Lodhi","designation":"DataEngineer"}
    

    Note: Simply sending lines of text will result in messages with null keys. In order to send messages with both keys and values you must set the parse.key and key.separator properties on the command line when running the producer.