Sunday, 3 November 2019

Kafka: What is the default number of partitions for a topic?


If you do not specify number of partitions to a topic, then kafka create a topic with one partition by default. You can confirm the same by checking the property 'num.partitions' in config/server.properties file.
# The default number of log partitions per topic. More partitions allow greater
# parallelism for consumption, but this will also result in more files across
# the brokers.
num.partitions=1

Let’s create new topic and confirm the same.
$kafka-console-producer.sh --broker-list localhost:9092 --topic myThirdTopic
>Hey Kafka, I am sending message to a topic which is not exist
[2019-10-05 20:20:39,496] WARN [Producer clientId=console-producer] Error while fetching metadata with correlation id 3 : {myThirdTopic=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
>^C$
$
$kafka-topics.sh --bootstrap-server localhost:9092 --topic myThirdTopic --describe
Topic:myThirdTopic PartitionCount:1 ReplicationFactor:1 Configs:segment.bytes=1073741824
 Topic: myThirdTopic Partition: 0 Leader: 0 Replicas: 0 Isr: 0

How to change the default partitions per topic?
Change the value of the property ‘num.partitions’ in config/server.properties file.

Example
num.partitions=3


After setting the property stop and restart the kafka service. 

Let’s create new topic again.
$kafka-console-producer.sh --broker-list localhost:9092 --topic myFourthTopic
>Hey Kafka, I am sending message to a topic which is not exist
[2019-10-05 20:25:09,265] WARN [Producer clientId=console-producer] Error while fetching metadata with correlation id 3 : {myFourthTopic=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
>^C$
$
$kafka-topics.sh --bootstrap-server localhost:9092 --topic myFourthTopic --describe
Topic:myFourthTopic PartitionCount:3 ReplicationFactor:1 Configs:segment.bytes=1073741824
 Topic: myFourthTopic Partition: 0 Leader: 0 Replicas: 0 Isr: 0
 Topic: myFourthTopic Partition: 1 Leader: 0 Replicas: 0 Isr: 0
 Topic: myFourthTopic Partition: 2 Leader: 0 Replicas: 0 Isr: 0

As you see the properties of ‘myFourthTopic’, you can see number of partitions are 3.



Previous                                                    Next                                                    Home

No comments:

Post a Comment