Send and Receive message in Kafka

How to Publish and Subscribe messages (with key) using Kafka?

Learn how you can publish and subscribe to messages in the easiest ways.

Task

→ We are going to publish all the records, start with the same character should go to the same partition. So that consumers can read the messages in the order they will publish to the topic.

Using Key: A
Note: The same key will resolve messages to the same partitions.

Without Key (earlier method)

kafka-console-producer.bat --broker-list localhost:9092 --topic test-topic

With Key

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

Sending and Receiving messages with Key

How to instantiate a Console Producer with Key?

Step 1: let’s start our Kafka producer

.\bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic test-topic --property "key.separator=-" --property "parse.key=true"
Producer — Creating and sending messages to consumers. (with key — )

How to instantiate a Console Consumer?

Step 2: let’s start our Kafka consumer

.\bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test-topic --from-beginning -property "key.separator= - " --property "print.key=true"
Order is maintained which we sent as earlier.

More Kafka Tutorials…

Next →

Previous ←

Software Engineer at HCL | Technical Content Writing | Follow me on LinkedIn https://www.linkedin.com/in/sagarkudu/