0
votes

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 :)

2
Where did you get that example code from? - OneCricketeer

2 Answers

1
votes

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...

0
votes

I think your producer configuration is wrong. Here is an example from Kafka official documentation:

 Properties props = new Properties();
 props.put("bootstrap.servers", "localhost:9092");
 props.put("acks", "all");
 props.put("retries", 0);
 props.put("batch.size", 16384);
 props.put("linger.ms", 1);
 props.put("buffer.memory", 33554432);
 props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
 props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

Just try smaller values for batch.size and buffer.memory.