Search code examples
python-3.xapache-kafkaapache-kafka-streamskafka-producer-api

How to create or set number of partitions for a topic?


I'm using python 3.6.8 and kafka-python=2.0.2

Is there a way to set the number of partitions for a topic from python code ?

My producer code looks:

producer = KafkaProducer(bootstrap_servers=['localhost:9092'],
                             value_serializer=lambda x:
                             dumps(x).encode('utf-8'))

producer.send("RANDOM_NEW_TOPIC", value={'test'})

I checked the manuals but can't see how can I update the number of partitions for a topic


Solution

  • We could use Kafka Admin CreatePartitions API to increase the number of partitions. The below show how to increase the partitions number to 4 for topic topic1

    from kafka import KafkaAdminClient
    from kafka.admin.new_partitions import NewPartitions
    
    client = KafkaAdminClient(bootstrap_servers='localhost:9092')
    
    rsp = client.create_partitions({
        'topic1': NewPartitions(4)
    })
    print(rsp)