We don't have an official client api for creating topics yet. You can try
instantiating kafka.admin.CreateTopicCommand$ and use the following method:

createTopic(zkClient: ZkClient, topic: String, numPartitions: Int = 1,
replicationFactor: Int = 1, replicaAssignmentStr: String = "")

To instantiate zkClient, do
 zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer)

Thanks,

Jun


On Tue, Nov 5, 2013 at 11:24 AM, <sggra...@nc.rr.com> wrote:

> Hi All:
> I am trying to programmatically create Topics from a Java client.
>
> I am using a suggestion from
> http://stackoverflow.com/questions/16946778/how-can-we-create-a-topic-in-kafka-from-the-ide-using-api/18480684#18480684
>
> Essentially invoking the CreateTopicCommand.main().
>
>         String [] arguments = new String[8];
>         arguments[0] = "--zookeeper";
>         arguments[1] = "localhost:2181";
>         arguments[2] = "--replica";
>         arguments[3] = "1";
>         arguments[4] = "--partition";
>         arguments[5] = "5";
>         arguments[6] = "--topic";
>         arguments[7] = "someTopicName";
>
>         try{
>                 CreateTopicCommand.main(arguments);
>         }catch(Exception e){
>                 System.out.println("Topic someTopicName exists.");
>         }
>
> This code works fine the first time it is run (i.e. the Topic does not yet
> exist).
>
> Unfortunately, when this is run a second time, an exception is thrown from
> Scala that the Java catch block DOES NOT CATCH!
>
> I tried using the ListTopicsCommand, but there is no boolean response to
> verify if the topic already exists.
>
> Any suggestions?
>
> thanks
> sgg
>

Reply via email to