将Kafka升级到2.5.0后,不推荐使用ZkUtils

时间:2020-07-22 08:47:04

标签: apache-kafka

Kafka从2.3.0升级到2.5.0的新变化之一就是删除ZkUtils(请参见https://issues.apache.org/jira/browse/KAFKA-8545

删除该用途的最佳做法是什么,我应该使用哪个软件包

public void createTopic(String topicName, int partitions, int replicationFactor) {
    DataExportConfig conf = ApplicationContextProvider.getApplicationContext().getBean("dataExportConfig", DataExportConfig.class);
    String zKaddress = conf.getZkHost();
    boolean isSecureKafkaCluster = false;

    ZkUtils zkUtils ZkUtils.apply(zKaddress, zkSessionTimeoutMs, zkConnectionTimeOutInMs, isSecureKafkaCluster);
    try {
        if (!AdminUtils.topicExists(zkUtils, topicName)) {
            AdminUtils.createTopic(zkUtils, topicName, partitions, replicationFactor, new Properties(), RackAwareMode.Enforced$.MODULE$);
            //log
        } else {
            //log
        }

        List<String> topicList = Arrays.asList(topicName);
        // checking real partition size for topic
        Integer topicPartitionSizeInZooKeeper = JavaConversions.mapAsJavaMap(zkUtils.getPartitionAssignmentForTopics(JavaConversions.asScalaBuffer(topicList))).get(topicName).size();
        if (topicPartitionSizeInZooKeeper != partitions) {
            //log
    } catch (Exception ex) {
            //log
        throw ex;
    } finally {
        //close zookeeper client after all topics are created
        zkUtils.zkClient().close();
    }
}

1 个答案:

答案 0 :(得分:0)

您现在需要使用Admin API createTopics()方法来创建主题:

int partitions = 1;
short replicationFactor = 3;

Properties props = new Properties();
props.put("bootstrap.servers", "localshot:9092");
AdminClient admin = AdminClient.create(props);

NewTopic topic = new NewTopic("mytopic", partitions, replicationFactor);
Collection<NewTopic> newTopics = Arrays.asList(topic);
CreateTopicsResult ctr = admin.createTopics(newTopics);
ctr.all().get();