【问题标题】:After upgrade Kafka to 2.5.0 ZkUtils is deprecated [duplicate]将 Kafka 升级到 2.5.0 后,不推荐使用 ZkUtils [重复]
【发布时间】:2020-11-11 18:23:29
【问题描述】:

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();
    }
}

【问题讨论】:

    标签: apache-kafka


    【解决方案1】:

    您现在需要使用 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();
    

    【讨论】:

    猜你喜欢
    • 2018-05-09
    • 1970-01-01
    • 2018-01-29
    • 2020-10-05
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    相关资源
    最近更新 更多