Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. import org.apache.kafka.clients.producer.KafkaProducer;
  2. import org.apache.kafka.clients.producer.ProducerRecord;
  3.  
  4. import java.util.Properties;
  5.  
  6. public class ProducerTopic {
  7.  
  8.     ProducerTopic(){}
  9.  
  10.     public static void set_in_topic(String topic, String string){
  11.         // create instance for properties to access producer configs
  12.         Properties props = new Properties();
  13.  
  14.         //Assign localhost id
  15.         props.put("bootstrap.servers", "localhost:9092");
  16.  
  17.         //Set acknowledgements for producer requests.
  18.         props.put("acks", "all");
  19.  
  20.         //If the request fails, the producer can automatically retry,
  21.         props.put("retries", 0);
  22.  
  23.         //Specify buffer size in config
  24.         props.put("batch.size", 16384);
  25.  
  26.         //Reduce the no of requests less than 0
  27.         props.put("linger.ms", 1);
  28.  
  29.         //The buffer.memory controls the total amount of memory available to the producer for buffering.
  30.         props.put("buffer.memory", 33554432);
  31.  
  32.         props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
  33.  
  34.         props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
  35.  
  36.         org.apache.kafka.clients.producer.Producer<String, String> producer = new KafkaProducer<>(props);
  37.  
  38.         producer.send(new ProducerRecord<String, String>(topic, string.split("#")[3] , string));
  39.  
  40.         System.out.println("Message sent successfully to topic " + topic);
  41.         producer.close();
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement