Sending and Receiving messages

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

Before starting this tutorial, please create a Topic first. Learn How to create a Topic in Kafka.

Without key

  • The partitioner first checks whether the key is present as part of the message or not. In this example, we are not sending any key, so in this case, the partitioner will use the Round Robin algorithm to send the messages.
  • In the above approach, there is no guarantee the consumer will able to read the messages in the same order because the consumer pulls the messages from all the partitions at the same time.

Sending and Receiving messages

  • make sure you in c:\kafka directory (root)

How to instantiate a Console Producer?

Step 1: let's start our Kafka producer

  • open new cmd to send messages
.\bin\windows\kafka-console-producer.bat --broker-list localhost:9092 --topic test-topic

→ Type your message in a single line

Producer — Creating and sending messages to consumers.

Now we have sent messages till now, now Kafka consumer is waiting to consume these messages.

How to instantiate a Console Consumer?

Step 2: let's start our Kafka consumer

  • open new cmd to receive messages
.\bin\windows\kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test-topic --from-beginning

→ Type your message in a single line e.g

Consumer- Receiving the messages sent by the Producer.

→ Now we have received a message till now, which is sent by Kafka Producer and it will continuously wait in queue for the new message from the producer.

Note: The --from-beginning is very important, and if not used-

  • it will be not able to read older which was sent by the Producer i.e it is going to read only those messages which are just sent.
  • If there are multiple partitions say --partitions 4then the order will not be maintained and messages will go into other partitions.
  • So in order to post messages in the same order then make sure you are posting to the same partition, so for getting the same order, we need to pass the same key, then messages will be retrieved in the same order.

Note: you can also check for the server console for logs or what is activities going here, so you see what is clean-up policy, what are the segments available, how many partitions are there, data retention time period, etc.

Kafka Tutorials

Next →

Previous ←

--

--

--

I am Full Stack Java Developer @ Tata Strive | Get blogs and tutorials related to the (React | Kafka | DevOps) | Follow me on LinkedIn https://www.linkedin.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

4 Really Small Things That Say a Lot about You Hint: It’s not what you say.

Estimation is Evil: Overcoming the Estimation Obsession

Cover from PragPub magazine, February 2013

How the PVS-Studio analyzer began to find even more errors in Unity projects

So you think you can code? General Assembly bootcamp — Week One

Two Sum, Three Sum, Four Sum (and how to extend them to K-Sum): Part 1

Join LinkedIn Group easily in python

Data Center Design: 6 Important Tips to Consider — RackSolutions

CS371p Spring 2022: Lilia Li Blog #6

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sagar Kudu

Sagar Kudu

I am Full Stack Java Developer @ Tata Strive | Get blogs and tutorials related to the (React | Kafka | DevOps) | Follow me on LinkedIn https://www.linkedin.com

More from Medium

Mock S3 service for local development

Transform your SDLC with Continuous Integration & Continuous Deployment

Transform your SDLC with Continuous Integration & Continuous Deployment

How to test your secured GCP cloud functions

The decision process on provisioning your web application to Google Cloud