Reputation: 2838
Yesterday I asked this question and I'm trying to follow some other answers I've found but they're leading me nowhere. I really can't understand how to properly set up Zookeeper and then Kafka server from code. What I did so far was this:
Properties prop = new Properties();
prop.setProperty("dataDir","C:\\kafka_2.12-2.2.0\\config\\zookeeper.properties");
prop.setProperty("bootstrap.servers", "localhost:2181");
QuorumPeerConfig quorumConfiguration = new QuorumPeerConfig();
try {
quorumConfiguration.parseProperties(prop);
} catch(Exception e) {
throw new RuntimeException(e);
}
ZooKeeperServerMain zookeeper = new ZooKeeperServerMain();
final ServerConfig configuration = new ServerConfig();
configuration.readFrom(quorumConfiguration);
new Thread() {
public void run() {
try {
zookeeper.runFromConfig(configuration);
} catch (IOException e) {
}
}
}.start();
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");
KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);
for (int i = 0; i < 100; i++)
producer.send(new ProducerRecord<String, String>("info", Integer.toString(i), Integer.toString(i)));
producer.close();
I couldn't find anything more than this.
Upvotes: 0
Views: 547
Reputation: 191738
Using the *-server-start
commands would be highly recommended... They setup the classpath correctly.
Otherwise, looks like you are trying to write just simple tests, and libraries exist already for Embedded Kafka. e.g. https://github.com/embeddedkafka/embedded-kafka or using Docker
Upvotes: 1