Java Kafka Example

Share

Java Kafka Example

Absolutely! Here’s a blog about a Java Kafka example, including explanations and code snippets:

Title: Getting Started with Apache Kafka in Java: A Hands-On Example

Introduction

Apache Kafka is a powerful distributed streaming platform for building real-time data pipelines, messaging systems, and more. Its high throughput, scalability, and fault tolerance make it perfect for handling massive volumes of data. In this blog post, we’ll dive into a simple Java example to demonstrate how to produce and consume messages using Kafka.

Prerequisites

  • Basic understanding of Java programming
  • A running Kafka cluster (you can download it from Apache Kafka’s website

Setting Up

  1. Create a Java Project: Create a new Maven or Gradle project in your favorite IDE.
  2. Add Kafka Dependencies: Include the following dependency in your project’s build file:
  3. XML
  4. <dependency>
  5.     <groupId>org.apache.kafka</groupId>
  6.     <artifactId>kafka-clients</artifactId>
  7.     <version>3.3.1</version> </dependency>
  8. Use code 
  9. content_copy

Creating a Kafka Producer

Let’s create a simple Kafka producer to send messages to a Kafka topic:

Java

import java.util.Properties;

import org.apache.kafka.clients.producer.KafkaProducer;

import org.apache.kafka.clients.producer.ProducerConfig;

import org.apache.kafka.clients.producer.ProducerRecord;

public class SimpleProducer {

    public static void main(String[] args) {

        // Configure properties

        Properties props = new Properties();

        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, “localhost:9092”);

        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, “org.apache.kafka.common.serialization.StringSerializer”);

        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, “org.apache.kafka.common.serialization.StringSerializer”);

        // Create a producer

        try (KafkaProducer<String, String> producer = new KafkaProducer<>(props)) {

            // Send messages

            for (int i = 0; i < 10; i++) {

                String message = “Test message – ” + i;

                ProducerRecord<String, String> record = new ProducerRecord<>(“my-topic”, message);

                producer.send(record);

            }

            System.out.println(“Messages sent successfully!”);

        } 

    }

}

Use code 

content_copy

Explanation

  • Properties: We configure connection properties, including the Kafka bootstrap servers, key serializer, and value serializer.
  • KafkaProducer: We create a KafkaProducer instance using the properties.
  • ProducerRecord: We create ProducerRecord objects representing messages to send, specifying the topic name and the message itself.
  • Producer.send(): The send method asynchronously sends messages to the Kafka cluster.

Creating a Kafka Consumer

Now, let’s create a consumer to read the messages from the topic:

Java

import java. Time.Duration;

import java. Util.Collections;

import java.util.Properties;

import org.apache.kafka.clients.consumer.ConsumerConfig;

import org.apache.kafka.clients.consumer.ConsumerRecord;

import org.apache.kafka.clients.consumer.ConsumerRecords;

import org.apache.kafka.clients.consumer.KafkaConsumer;

public class SimpleConsumer {

    public static void main(String[] args) {

        // Configure properties

        Properties props = new Properties();

        // … (Similar configuration as producer) …

        props.put(ConsumerConfig.GROUP_ID_CONFIG, “my-consumer-group”);

        // Create a consumer

        try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props)) {

            consumer.subscribe(Collections.singleton list(“my-topic”));

            // Poll for messages

            while (true) {

                ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));

                for (ConsumerRecord<String, String> record : records) {

                    System.out.println(“Received message: ” + record.value());

                }

            }

        } 

    }

}

 

 

You can find more information about  Apache Kafka  in this Apache Kafka

 

Conclusion:

Unogeeks is the No.1 IT Training Institute for Apache kafka Training. Anyone Disagree? Please drop in a comment

You can check out our other latest blogs on  Apache Kafka  here –  Apache kafka Blogs

You can check out our Best In Class Apache Kafka Details here –  Apache kafka Training

Follow & Connect with us:

———————————-

For Training inquiries:

Call/Whatsapp: +91 73960 33555

Mail us at: info@unogeeks.com

Our Website ➜ https://unogeeks.com

Follow us:

Instagram: https://www.instagram.com/unogeeks

Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute

Twitter: https://twitter.com/unogeek


Share

Leave a Reply

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