-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProducerDemo.java
39 lines (27 loc) · 1.22 KB
/
ProducerDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.github.simple;
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 ProducerDemo {
public static void main(String[] args) {
String bootstrapServers="127.0.0.1:9092";
// Create Producer properties
Properties properties = new Properties();
properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
// Create Producer
KafkaProducer<String, String> producer = new KafkaProducer<>(properties);
// Create a producer record
ProducerRecord<String, String> record =
new ProducerRecord<>("first_topic", "Henlo");
// send data
producer.send(record);
// flush data
producer.flush();
// flush and close program
producer.close();
}
}