简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM