Search code examples
javaapache-kafkakafka-producer-api

Kafka Producer in Java Error


I am pretty new to Kafka. I have my Zookeeper server running on port 2181 and Kafka server on port 9092. I have written a Simple Producer in java. But whenever run the program, it shows me the following error:

    USAGE: java [options] KafkaServer server.properties [--override property=value]*
    Option      Description                           
    ------      -----------                           
    --override  Optional property that should override values set in server.properties file

I am using Netbeans IDE with JDK 8 and have included all the Kafka jar files in the Library. I believe there's no error in the library files because the code builds correctly but doesn't run.

Here is the Simple Producer code:

package kafka;

import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;
import java.util.Properties;

public class Kafka {
    private static Producer<Integer, String> producer;
    private final Properties properties = new Properties();
    public Kafka() {
        properties.put("metadata.broker.list", "localhost:9092");
        properties.put("serializer.class", "kafka.serializer.StringEncoder");
        properties.put("request.required.acks", "1");
        producer = new Producer<>(new ProducerConfig(properties));
    }
    public static void main(String args[]) {
        Kafka k = new Kafka();
        String topic = "test";
        String msg = "hello world";
        KeyedMessage<Integer, String> data = new KeyedMessage<>(topic, msg);
        producer.send(data);
        producer.close();
    }
}

Kindly help :)


Solution

  • It looks like that Netbeans executes wrong class - not your kafka.Kafka class, but KafkaServer (it looks like this is a main class of Kafka itself). Please configure Netbeans to execute correct class.

    I would recommend to start with existing sample of Producer from Confluent Examples, and re-use the Maven project...