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 .
The thing is, you cannot create the Kafka topic with replication factor greater than your broker.
Increasing the replication factor can be done via the kafka-reassign-partitions tool. Specify the extra replicas in the custom reassignment json file and use it with the --execute option to increase the replication factor of the specified partitions.
Kafka Replication Factor refers to the multiple copies of data stored across several Kafka brokers. Setting the Kafka Replication Factor allows Kafka to provide high availability of data and prevent data loss if the broker goes down or cannot handle the request.
Apache Kafka Replication: What will happen if you set the replication factor as more than 1 while creating new topic in a single broker cluster? The Kafka cluster will automatically create more brokers to meet the replication factor. The topic will get created with the set replication factor.
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
configure your local machine to have multiple brokers up and running in case you decide to keep your replication_factor > 1
.
You can do that by simply having multiple copies of the
server.properties
file. e.g server-1.properties
& server-2.properties
You need to then specify different broker.id
& port
in each of those files to make them unique ..
config/server-1.properties:
broker.id=1
port=9093
log.dir=/tmp/kafka-logs-1
config/server-2.properties:
broker.id=2
port=9094
log.dir=/tmp/kafka-logs-2
And then start multiple instances with following commands
> bin/kafka-server-start.sh config/server-1.properties &
> bin/kafka-server-start.sh config/server-2.properties &
For more details check Step 6: Setting up a multi-broker cluster
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With