Can a Kafka producer create topics and partitions?

前端 未结 4 2534
一向
一向 2021-02-20 00:55

currently I am evaluating different Messaging Systems. There is a question related to Apache Kafka which I could not answer myself.

Is it possible for a Kafka producer t

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-20 01:57

    From java you can create a topic, if needed. Whether it's recommended or not, depends on the use-case. E.g. if your topic name is a function of the incoming payload to the producer, it might be useful. Following is the code snippet that works in kafka 0.10.x

    void createTopic(String zookeeperConnect, String topicName) throws InterruptedException {
        int sessionTimeoutMs = ;
        int connectionTimeoutMs = ;
    
        ZkClient zkClient = new ZkClient(zookeeperConnect, sessionTimeoutMs, connectionTimeoutMs, ZKStringSerializer$.MODULE$);
    
        boolean isSecureKafkaCluster = false;
        ZkUtils zkUtils = new ZkUtils(zkClient, new  ZkConnection(zookeeperConnect), isSecureKafkaCluster);
    
        Properties topicConfig = new Properties();
        try {
          AdminUtils.createTopic(zkUtils, topicName, 1, 1, topicConfig,
          RackAwareMode.Disabled$.MODULE$);
        } catch (TopicExistsException ex) {
        //log it 
        }
        zkClient.close();
    }
    

    Note: It's only allowed to increase no. of partitions.

提交回复
热议问题