Apache Kafka is a distributed streaming platform that is widely used for building real-time data pipelines and streaming applications. It is designed to handle high throughput, fault tolerance, and scalability.
Apache Kafka is an open-source stream processing platform developed by LinkedIn and donated to the Apache Software Foundation. It is used to build real-time data pipelines and streaming applications that can process data in real-time.
Below is a basic example of how to produce and consume messages using Kafka in Java. This example assumes you have Kafka running on localhost and default port 9092.
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;
import java.util.Properties;
public class KafkaProducerExample {
public static void main(String[] args) {
// Set up the Kafka producer configuration
Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
// Create the Kafka producer
KafkaProducer<String, String> producer = new KafkaProducer<>(props);
// Create and send a message
ProducerRecord<String, String> record = new ProducerRecord<>("my_topic", "key", "value");
producer.send(record);
// Close the producer
producer.close();
}
}
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import java.util.Collections;
import java.util.Properties;
public class KafkaConsumerExample {
public static void main(String[] args) {
// Set up the Kafka consumer configuration
Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
props.put(ConsumerConfig.GROUP_ID_CONFIG, "my_group");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
// Create the Kafka consumer
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
// Subscribe to the topic
consumer.subscribe(Collections.singletonList("my_topic"));
// Poll for new messages
while (true) {
for (ConsumerRecord<String, String> record : consumer.poll(1000)) {
System.out.printf("Received record: key=%s, value=%s%n", record.key(), record.value());
}
}
}
}
Apache Kafka is a powerful tool for handling real-time data streams. Its architecture allows for high scalability and fault tolerance, making it suitable for various use cases including real-time analytics and event-driven architectures. Understanding the core concepts and how to interact with Kafka through producers and consumers is essential for leveraging its capabilities effectively.