Can a Kafka producer create topics and partitions?

前端 未结 4 2516
一向
一向 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:41

    When you are starting your kafka broker you can define a bunch of properties in conf/server.properties file. One of the property is auto.create.topics.enable if you set this to true (by default) kafka will automatically create a topic when you send a message to a non existing topic. The partition number will be defined by the default settings in this same file.

    Disadvantages : as far as I know, topics created this way will always have the same default settings (partitions, replicas ...).

    0 讨论(0)
  • For any messaging system, i don't think it is recommended way to create topic/partition or any queue dynamically by producer.

    For you use case, you can probably use device_id as your as partition key to distinguish the messages.That way you can use one topic.

    0 讨论(0)
  • 2021-02-20 01:56

    Updated:

    The kafka broker has a property: auto.create.topics.enable

    If you set that to true if the producer publishes a message to the topic with the new topic name it will automatically create a topic for you.

    The Confluent Team recommends not doing this because the explosion of topics, depending on your environment can become unwieldy, and the topic creation will always have the same defaults when created. It's important to have a replication-factor of at least 3 to ensure durability of your topics in the event of disk failure.

    0 讨论(0)
  • 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 = <some-int-value>;
        int connectionTimeoutMs = <some-int-value>;
    
        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.

    0 讨论(0)
提交回复
热议问题