简体   繁体   English

springboot中Kafka主题配置更新

[英]Kafka topic configuration update in springboot

how to update kakfa topic configs in springboot如何在 springboot 中更新 kakfa 主题配置

    public void updateTopicById(AdminClient adminClient,  String topicName) {
      //  Map<ConfigResource, KafkaFutureImpl<Void>> allFutures = new HashMap();
       // Collection<ConfigResource> unifiedRequestResources = new ArrayList();
        ConfigResource cr = new ConfigResource(ConfigResource.Type.TOPIC,topicName);
        ConfigEntry ce = new ConfigEntry(topicName,null);
        AlterConfigOp aco=new AlterConfigOp(ConfigEntry, AlterConfigOp.OpType.SET);
       Map<ConfigResource, Collection<AlterConfigOp>> mapp1=new HashMap<>();
        AlterConfigsOptions var1=new AlterConfigsOptions();
        adminClient.incrementalAlterConfigs(mapp1,var1);
        LOGGER.info("Topics '{}' deleted from kafka.", topicName);
    }```

The above sample code is not directly related to spring Kafka/SpringBoot.以上示例代码与spring Kafka/SpringBoot没有直接关系。 Spring has its own Kafka client library . Spring 有自己的 Kafka 客户端

But your implementation is with the Apache Kafka client.但是您的实现是使用 Apache Kafka 客户端。 This implementation can be improvised to work as below:这个实现可以即兴发挥如下作用:

ConfigResource topicRes = new ConfigResource(ConfigResource.Type.TOPIC, "topicName1");

//update the config entry name as per use case
AlterConfigOp alterConfigOp = new AlterConfigOp(
        new ConfigEntry("delete.retention.ms", "10000"), AlterConfigOp.OpType.APPEND);

List<AlterConfigOp> alt = new ArrayList<>();
alt.add(alterConfigOp);
final Map<ConfigResource, Collection<AlterConfigOp>> configsMap = new HashMap<>();
configsMap.put(topicRes, alt);

adminClient.incrementalAlterConfigs(configsMap); 
      //  Map<ConfigResource, KafkaFutureImpl<Void>> allFutures = new HashMap();
       // Collection<ConfigResource> unifiedRequestResources = new ArrayList();
        ConfigResource cr = new ConfigResource(ConfigResource.Type.TOPIC,topic.getName());
        Map<String,String> topicConfigMap= topic.getConfigs();
        for (Map.Entry<String, String> entry : topicConfigMap.entrySet()) {
            System.out.println(entry.getKey() + "/" + entry.getValue());
            //String topicConfigKey= entry.getKey().replaceAll("\\.","_").toUpperCase().concat("_CONFIG");
            ConfigEntry ce = new ConfigEntry(entry.getKey(), topic.getConfigs().get(entry.getKey()));
            AlterConfigOp aco=new AlterConfigOp(ce, AlterConfigOp.OpType.SET);
            Map<ConfigResource, Collection<AlterConfigOp>> conf=new HashMap<>();
            conf.put(cr,Arrays.asList(aco));
            AlterConfigsOptions var1=new AlterConfigsOptions();
            adminClient.incrementalAlterConfigs(conf,var1);
           // System.out.println("repla:"+topicConfigKey);
        }

This is resolving the issue, I am able to update any config.这正在解决问题,我可以更新任何配置。 Topic is parameter coming from update service主题是来自更新服务的参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM