Error creating Kafka topic :- replication factor larger than available brokers

Count picture Count · Jan 23, 2015 · Viewed 35.5k times · Source

I am trying to create a kafka topic via AdminCommand using below code Source

 ZkClient zkClient = new ZkClient(kafkaHost, 10000, 10000, ZKStringSerializer$.MODULE$);
    AdminUtils.createTopic(zkClient, "pa_reliancepoc_telecom_usageevent", 10, 2, new Properties());

But getting the below exception

Exception in thread "main" kafka.admin.AdminOperationException: replication factor: 1 larger than available brokers: 0
at kafka.admin.AdminUtils$.assignReplicasToBrokers(AdminUtils.scala:70)
at kafka.admin.AdminUtils$.createTopic(AdminUtils.scala:155)

However, I am able to create the topic using shell command .

Answer

Jaya Ananthram picture Jaya Ananthram · Jan 23, 2015

In your code,

 AdminUtils.createTopic(zkClient, "pa_reliancepoc_telecom_usageevent", 10, 2, new Properties());

The fourth argument is the replication factor. So you are trying to create a topic with a name of pa_reliancepoc_telecom_usageevent with partition count of 10 and replication of 2. So two kafka brokers should be available while creating the topic. If less than two is available then you will get the following exception.

Exception in thread "main" kafka.admin.AdminOperationException: replication factor: 1 larger than available brokers: 0
at kafka.admin.AdminUtils$.assignReplicasToBrokers(AdminUtils.scala:70)
at kafka.admin.AdminUtils$.createTopic(AdminUtils.scala:155)

Make sure that you are running kafka cluster with two broker nodes and the two nodes should be alive while creating the topic.

To run kafka in cluster refer Step 6 in this link