Tell me about Kafka: the main components on the Java side.
sobes.tech AI
Answer from AI
The main components of Apache Kafka from the Java side include:
-
Producer — a client that publishes (sends) messages to Kafka topics. In Java, the
KafkaProducerclass is used, which is configured through properties and sends records (ProducerRecord). -
Consumer — a client that reads messages from topics. In Java, this is
KafkaConsumer, which subscribes to topics and receives records (ConsumerRecord). -
Kafka Streams — a library for processing data streams over Kafka, allowing the creation of applications for transformation, aggregation, and stream analysis.
-
AdminClient — an API for managing Kafka resources, such as creating topics and changing configurations.
Example of a simple producer in Java:
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", "key1", "value1");
producer.send(record);
producer.close();
Thus, the main components are classes for sending and receiving messages, as well as tools for managing and processing data streams.