|
@@ -1,78 +0,0 @@
|
|
|
-package producer;
|
|
|
-
|
|
|
-import org.apache.kafka.clients.producer.KafkaProducer;
|
|
|
-import org.apache.kafka.clients.producer.ProducerConfig;
|
|
|
-import org.apache.kafka.clients.producer.ProducerRecord;
|
|
|
-
|
|
|
-import java.time.Instant;
|
|
|
-import java.util.Properties;
|
|
|
-
|
|
|
-public class KafkaProducerApp {
|
|
|
-
|
|
|
- public static final String STRING_SERIALIZER = "org.apache.kafka.common.serialization.StringSerializer";
|
|
|
- public static final String TOPIC = "my-topic";
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- Properties props = new Properties();
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092, BROKER-1:9092, BROKER-2:9093");
|
|
|
-
|
|
|
-
|
|
|
- props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, STRING_SERIALIZER);
|
|
|
- props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, STRING_SERIALIZER);
|
|
|
-
|
|
|
- KafkaProducer<String, String> myProducer = new KafkaProducer<String, String>(props);
|
|
|
-
|
|
|
- try {
|
|
|
- for (int i = 0; i < 100; i++) {
|
|
|
- Instant ts = Instant.now();
|
|
|
- Double ss = ts.toEpochMilli() + ts.getNano() / 1E9;
|
|
|
- ProducerRecord myMessage = new ProducerRecord(TOPIC, String.format("%3d : %09.3f", i, ss));
|
|
|
- myProducer.send(myMessage);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- myProducer.close();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static void createMessagesExample() {
|
|
|
-
|
|
|
- ProducerRecord myMessage = new ProducerRecord(
|
|
|
- TOPIC,
|
|
|
- "My Message 1"
|
|
|
- );
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- ProducerRecord myPartitionedMessage = new ProducerRecord(
|
|
|
- TOPIC,
|
|
|
- 1,
|
|
|
- "My Message 1"
|
|
|
- );
|
|
|
-
|
|
|
- ProducerRecord myKeyedMessage = new ProducerRecord(
|
|
|
- TOPIC,
|
|
|
- "Course-001",
|
|
|
- "My Message 1"
|
|
|
- );
|
|
|
-
|
|
|
-
|
|
|
- ProducerRecord msg3 = new ProducerRecord(
|
|
|
- TOPIC,
|
|
|
- 1,
|
|
|
- 124535353325L,
|
|
|
- "Course-001",
|
|
|
-
|
|
|
- "My Message 1"
|
|
|
- );
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
-}
|