Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.apache.kafka.common.config.ConfigException: Missing required configuration "bootstrap.servers" which has no default value

I'm getting this error when running my producer class in Eclipse: org.apache.kafka.common.config.ConfigException: Missing required configuration "bootstrap.servers" which has no default value

Here is my producer class:

public class SimpleProducer {

  public static void main(String[] args) throws Exception {

    try {
        String topicName = "mytopic";
        String key = "key1";
        String value = "Value-1";

        Properties prop = new Properties();
        prop.put("bootstrap.server","localhost:9092");
        prop.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        prop.put("value.serializer","org.apache.kafka.cpmmon.serialization.StringSerializer");

        Producer<String, String> producer = new KafkaProducer<>(prop);

        ProducerRecord<String, String> record = new ProducerRecord<>(topicName,key,value);
        producer.send(record);
        producer.close();
        System.out.println("SimpleProducer Completed.");
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
}

Any pointers on how to fix it?

like image 586
Amritesh Kumar Avatar asked Jun 05 '17 17:06

Amritesh Kumar


1 Answers

Use the following and avoid using hardcoded values

For

prop.put("bootstrap.server","localhost:9092");
prop.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
prop.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");

Use

prop.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
prop.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
prop.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

ProducerConfig will be found in org.apache.kafka.clients.producer package

like image 54
Nauman Shah Avatar answered Sep 25 '22 19:09

Nauman Shah