9

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?

Aditya
  • 1,693
  • 19
  • 27
Amritesh Kumar
  • 207
  • 1
  • 5
  • 16
  • 6
    You misspelled the property key `"bootstrap.servers"`. To avoid this mistake in the future, use the constant `ProducerConfig.BOOTSTRAP_SERVERS_CONFIG` instead. – Chin Huang Jun 05 '17 at 17:18
  • 2
    Why do you never, ever, accept any answers to your questions, OP? – timgeb May 28 '18 at 07:03

3 Answers3

11

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

Nauman Shah
  • 342
  • 2
  • 12
9

Just change the

prop.put("bootstrap.server","localhost:9092");

to

prop.put("bootstrap.servers","localhost:9092");

And in your code

prop.put("value.serializer",
         "org.apache.kafka.cpmmon.serialization.StringSerializer");

the common is not spelled correctly, the right package is
org.apache.kafka.common.serialization.StringSerializer

greybeard
  • 2,249
  • 8
  • 30
  • 66
dabaicai
  • 999
  • 8
  • 9
0

Here two observations found

1.

Replace

prop.put("bootstrap.server","localhost:9092");

by

prop.put("bootstrap.servers","localhost:9092");

bootstrap.server -- incorrect

bootstrap.servers -- correct

2.

Replace

org.apache.kafka.cpmmon.serialization.StringSerializer

By

org.apache.kafka.common.serialization.StringSerializer