Sobes.tech
Middle

Kuidas tehniliselt saata sõnum Kafka teemasse? Millist klassi selleks kasutatakse?

sobes.tech AI

Vastus AI-lt

To send a message to a Kafka topic, a producer is technically used. In Java, for example, this is the KafkaProducer class from the Apache Kafka library.

Example of 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);
ProducerRecord<String, String> record = new ProducerRecord<>("topicName", "key", "message value");
producer.send(record);
producer.close();

Thus, the KafkaProducer class is responsible for sending messages to Kafka topics.