5

I am using name : kafka_2.12 version : 2.3.0. Based on the traffic/load I want to change the maximum partition number for a topic. Is it possible to make this kind of change once Kafka is up and can it be done by code?

Abhishek Gharai
  • 227
  • 3
  • 15
  • Possible duplicate of [Is it possible to add partitions to an existing topic in Kafka 0.8.2](https://stackoverflow.com/questions/33677871/is-it-possible-to-add-partitions-to-an-existing-topic-in-kafka-0-8-2) – mazaneicha Oct 22 '19 at 14:58
  • 1
    For the Java part of your question, check Kafka Admin API (https://kafka.apache.org/22/javadoc/org/apache/kafka/clients/admin/AdminClient.html#createPartitions-java.util.Map-org.apache.kafka.clients.admin.CreatePartitionsOptions-), and come back if you have any specific issues. – mazaneicha Oct 22 '19 at 15:04

1 Answers1

6

Yes you could increase partition by code. Use AdminClient.createPartitions method.

AdminClients.createPartitions method API document

public abstract CreatePartitionsResult createPartitions(java.util.Map<java.lang.String,NewPartitions> newPartitions,CreatePartitionsOptions options)

Increase the number of partitions of the topics given as the keys of newPartitions according to the corresponding values. If partitions are increased for a topic that has a key, the partition logic or ordering of the messages will be affected.

This operation is not transactional so it may succeed for some topics while fail for others.

It may take several seconds after this method returns success for all the brokers to become aware that the partitions have been created. During this time, describeTopics(Collection) may not return information about the new partitions.

How to use:

public static void createPartitions(String topicName, int numPartitions) {
    Properties props = new Properties();
    props.put("bootstrap.servers","localhost:9092");
    AdminClient adminClient = AdminClient.create(props);

    Map<String, NewPartitions> newPartitionSet = new HashMap<>();
    newPartitionSet.put(topicName, NewPartitions.increaseTo(numPartitions));
    adminClient.createPartitions(newPartitionSet);
    adminClient.close();
}
Anderson Choi
  • 353
  • 1
  • 4
  • 25
  • Can you please provide a code snippet or a link which I can follow up. Thanks – Abhishek Gharai Oct 23 '19 at 10:29
  • @AbhishekGharai I modified the answer. Please refer to the sample code. – Anderson Choi Oct 24 '19 at 03:50
  • Anderson Choi thanks for your help. I have used your code but while using this to increase my partition I am getting following response **The broker does not support CREATE_PARTITIONS**. Also my partition is not increasing – Abhishek Gharai Nov 04 '19 at 12:47
  • 1
    @AbhishekGharai createPartitions() method which minimum broker version required is 1.0.0. Checkout your broker version. And please refer to the link. [KAFKA-5856](https://issues.apache.org/jira/browse/KAFKA-5856) [KIP-195](https://cwiki.apache.org/confluence/display/KAFKA/KIP-195%3A+AdminClient.createPartitions) – Anderson Choi Nov 05 '19 at 02:41
  • Thanks Anderson. I have another query related to Kafka here [Is it possible to consume kafka messages using key and partition?](https://stackoverflow.com/questions/58724645/is-it-possible-to-consume-kafka-messages-using-key-and-partition). Can you please have a look and share your feedback? Your assistance in this matter is greatly appreciated. – Abhishek Gharai Nov 06 '19 at 07:04