Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kafka 0.8, is it possible to create topic with partition and replication using java code?

Tags:

apache-kafka

In Kafka 0.8beta a topic can be created using a command like below as mentioned here

    bin/kafka-create-topic.sh --zookeeper localhost:2181 --replica 2 --partition 3 --topic test

the above command will create a topic named "test" with 3 partitions and 2 replicas per partition.

Can I do the same thing using Java ?

So far what I found is using Java we can create a producer as seen below

    Producer<String, String> producer = new Producer<String, String>(config);
    producer.send(new KeyedMessage<String, String>("mytopic", msg));

This will create a topic named "mytopic" with the number of partition specified using the "num.partitions" attribute and start producing.

But is there a way to define the partition and replication also ? I couldn't find any such example. If we can't then does that mean we always need to create topic with partitions and replication (as per our requirement) before and then use the producer to produce message within that topic. For example will it be possible if I want to create the "mytopic" the same way but with different number of partition (overriding the num.partitions attribute) ?

like image 722
Hild Avatar asked Aug 14 '13 07:08

Hild


People also ask

Are Kafka partitions replicated?

In Kafka, replication happens at the partition level i.e. copies of the partition are maintained at multiple broker instances. When we say a topic has a replication factor of 3, this means we will be having three copies of each of its partitions.

Can we increase partition in Kafka topic?

If you want to change the number of partitions or replicas of your Kafka topic, you can use a streaming transformation to automatically stream all of the messages from the original topic into a new Kafka topic that has the desired number of partitions or replicas.

What is difference between partition and replica of a topic in Kafka cluster?

Partitions are the way that Kafka provides redundancy.Kafka keeps more than one copy of the same partition across multiple brokers. This redundant copy is called a replica. If a broker fails, Kafka can still serve consumers with the replicas of partitions that failed broker owned.


1 Answers

Note: My answer covers Kafka 0.8.1+, i.e. the latest stable version available as of April 2014.

Yes, you can create a topic programatically via the Kafka API. And yes, you can specify the desired number of partitions as well as the replication factor for the topic.

Note that the recently released Kafka 0.8.1+ provides a slightly different API than Kafka 0.8.0 (which was used by Biks in his linked reply). I added a code example to create a topic in Kafka 0.8.1+ to my reply to the question How Can we create a topic in Kafka from the IDE using API that Biks was referring to above.

like image 114
Michael G. Noll Avatar answered Oct 24 '22 17:10

Michael G. Noll