Senior
Which library do you use for working with Kafka?
sobes.tech AI
Answer from AI
The official Apache Kafka Client library, provided by the Apache Kafka project itself, is most commonly used for working with Kafka in Java. It supports producers, consumers, topic administration, and other features.
Example of creating a producer and sending a message:
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);
producer.send(new ProducerRecord<>("my-topic", "key1", "value1"));
producer.close();
For easier work and integration with Spring, Spring Kafka is often used, which wraps the official client and adds convenient abstractions.