How Can we create a topic in Kafka from the IDE using API because when I do this:
bin/kafka-create-topic.sh --topic mytopic --replica 3 --zookeeper localhost:2181
I get the error:
bash: bin/kafka-create-topic.sh: No such file or directory
And I followed the developer setup as it is.
A few ways your call wouldn't work.
If your Kafka cluster didn't have enough nodes to support a replication value of 3.
If there is a chroot path prefix you have to append it after the zookeeper port
You arent in the Kafka install directory when running (This is the most likely)
In Kafka 0.8.1+ -- the latest version of Kafka as of today -- you can programmatically create a new topic via
AdminCommand
. The functionality ofCreateTopicCommand
(part of the older Kafka 0.8.0) that was mentioned in one of the previous answers to this question was moved toAdminCommand
.Scala example for Kafka 0.8.1:
Build dependencies, using sbt as example:
EDIT: Added Java example for Kafka 0.9.0.0 (latest version as of Jan 2016).
Maven dependencies:
Code:
EDIT 2: Added Java example for Kafka 0.10.2.0 (latest version as of April 2017).
Maven dependencies:
Code:
You can try with kafka.admin.CreateTopicCommand scala class to create Topic from Java code...providng the necessary arguments.
NB: You should add the maven dependencies for
jopt-simple-4.5
&zkclient-0.1
From Kafka 0.8 Producer Example the sample below will create a topic named
page_visits
and also start producing if theauto.create.topics.enable
attribute is set totrue
(default) in the Kafka Broker config fileFrom which IDE are your trying ?
Please provide complete path , below are the command from terminal which will create a topic
cd kafka/bin
./kafka-create-topic.sh --topic test --zookeeper localhost:2181
As of Kafka 0.10.1 the ZKStringSerializer mentioned by Michael is private (for Scala). You can use the factory methods createZkClient or createZkClientAndConnection in ZkUtils.
Scala example for Kafka 0.10.1:
Then just create the topic as Michael suggested: