Skip to content

Simple Kafka Product/Consumer

Here’s a basic Kafka tutorial in Java:

Step 1: Set up a Kafka cluster

  • Download and extract the Kafka binaries from the Apache Kafka website.
  • Start the ZooKeeper service by running the following command
    • bin/zookeeper-server-start.sh config/zookeeper.properties
  • Start the Kafka broker service by running the following command
    • bin/kafka-server-start.sh config/server.properties

Step 2: Create a Kafka topic

  • Run the following command to create a topic named “test” with a single partition and one replica factor
    • bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test

Step 3: Produce messages to the Kafka topic

  • Create a Java project in your IDE of choice and add the Kafka client library as a dependency.
  • Use the following code to create a Kafka producer and send messages to the “test” topic

Step 4: Consume messages from the Kafka topic

  • Use the following code to create a Kafka consumer and receive messages from the “test” topic

That’s it! You now have a basic Kafka setup that can produce and consume messages in Java. Note that there are many more advanced features and configuration options available in Kafka, but this should be enough to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *