Apache Kafka Java Example
Apache Kafka: A Java Primer
Apache Kafka has become indispensable for building scalable, distributed, and real-time data streaming applications. If you’re a Java developer looking to integrate Kafka into your projects, this blog is for you! We’ll explore Kafka’s core concepts and provide a hands-on Java example to get you started.
What is Apache Kafka?
At its heart, Apache Kafka is a distributed publish-subscribe messaging system designed for high throughput and fault tolerance. Let’s break down some key terms:
- Topics: Kafka organizes data streams into logical categories called topics.
- Producers: Applications that send messages (data) to Kafka topics.
- Consumers: Applications that read messages from Kafka topics.
- Brokers: Kafka servers that manage the storage and replication of messages.
- Partitions: Topics are divided into partitions for scalability and parallelism.
Why Kafka?
- Scalability: Kafka’s distributed architecture smoothly handles massive volumes of data.
- High Throughput: Kafka delivers messages with extremely low latency, enabling real-time data processing.
- Fault Tolerance: Data replication across brokers ensures the system remains operational even during failures.
- Persistence: Kafka stores messages reliably on disk, preventing data loss.
Setting Up a Basic Java Example
Now, let’s dive into a simple Java example. We’ll set up a producer to send messages and a consumer to receive them.
- Dependencies: Include the Kafka Client library in your Java project. Using Maven, add the following to your pom.xml:
- XML
- <dependency>
- <groupId>org.apache.kafka</groupId>
- <artifactId>kafka-clients</artifactId>
- <version>3.3.1</version> </dependency>
- Use code
- content_copy
- Kafka Producer:
- Java
- import java.util.Properties;
- import org.apache.kafka.clients.producer.KafkaProducer;
- Import org.apache.kafka.clients.producer.ProducerRecord;
- public class SimpleProducer {
- public static void main(String[] args) {
- Properties props = new Properties();
- props.put(“bootstrap.servers”, “localhost:9092”);
- props. Put (“key. serializer”, “org.apache.kafka.common.serialization.StringSerializer”);
- Props. put(“value. serializer”, “org.apache.kafka.common.serialization.StringSerializer”);
- KafkaProducer<String, String> producer = new KafkaProducer<>(props);
- ProducerRecord<String, String> record = new ProducerRecord<>(“my-topic,” “key,” “Hello, Kafka!”);
- producer.send(record);
- producer.close();
- }
- }
- Use code
- content_copy
- Kafka Consumer:
- Java
- import java.util.Properties;
- import java. Time.Duration;
- import java. Util.Arrays;
- import org.apache.kafka.clients.consumer.KafkaConsumer;
- import org.apache.kafka.clients.consumer.ConsumerRecords;
- Import org.apache.kafka.clients.consumer.ConsumerRecord;
- public class SimpleConsumer {
- public static void main(String[] args) {
- Properties props = new Properties();
- props.put(“bootstrap.servers”, “localhost:9092”);
- props.put(“group.id”, “test-group”);
- props. Put (“key. deserializer”, “org.apache.kafka.common.serialization.StringDeserializer”);
- Props. put(“value. deserializer”, “org.apache.kafka.common.serialization.StringDeserializer”);
- KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
- consumer.subscribe(Arrays.asList(“my-topic”));
- while (true) {
- ConsumerRecords<String, String> records = consumer.poll(Duration.of Millis(100));
- for (ConsumerRecord<String, String> record : records) {
- System. out.print(“Received message: key = %s, value = %s%n”, record.key(), record.value());
- }
- }
- }
- }
- Use code
- content_copy
Remember: Start a local Kafka server before running this example.
Next Steps
This example is the starting point. Kafka offers far more! Consider exploring:
- Kafka Streams library for powerful stream processing
- Spring Kafka for simplified integration with Spring projects
- Error Handling, Security, and Advanced Configuration
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