Java Kafka adminClient主题配置。配置值被覆盖

Java Kafka adminClient主题配置。配置值被覆盖,java,apache-kafka,Java,Apache Kafka,尝试使用java kafka adminClient配置新创建的kafka主题时,会覆盖值 ConfigResource resource = new ConfigResource(ConfigResource.Type.TOPIC, topicName); Map<ConfigResource, Config> updateConfig = new HashMap<>(); // update retention Bytes for this topic Config

尝试使用java kafka adminClient配置新创建的kafka主题时,会覆盖值

ConfigResource resource = new ConfigResource(ConfigResource.Type.TOPIC, topicName);
Map<ConfigResource, Config> updateConfig = new HashMap<>();

// update retention Bytes for this topic
ConfigEntry retentionBytesEntry = new ConfigEntry(TopicConfig.RETENTION_BYTES_CONFIG, String.valueOf(retentionBytes));
updateConfig.put(resource, new Config(Collections.singleton(retentionBytesEntry)));

// update retention ms for this topic
ConfigEntry retentionMsEntry = new ConfigEntry(TopicConfig.RETENTION_MS_CONFIG, String.valueOf(retentionMs));
updateConfig.put(resource, new Config(Collections.singleton(retentionMsEntry)));

// update segment Bytes for this topic
ConfigEntry segmentBytesEntry = new ConfigEntry(TopicConfig.SEGMENT_BYTES_CONFIG, String.valueOf(segmentbytes));
updateConfig.put(resource, new Config(Collections.singleton(segmentBytesEntry)));

// update segment ms for this topic
ConfigEntry segmentMsEntry = new ConfigEntry(TopicConfig.SEGMENT_MS_CONFIG, String.valueOf(segmentMs));
updateConfig.put(resource, new Config(Collections.singleton(segmentMsEntry)));

// Update the configuration
client.alterConfigs(updateConfig);
我曾尝试使用控制台命令设置相同的主题配置,但效果很好。不幸的是,当我尝试通过Java代码时,一些值发生冲突并被覆盖

ConfigResource resource = new ConfigResource(ConfigResource.Type.TOPIC, topicName);
Map<ConfigResource, Config> updateConfig = new HashMap<>();

// update retention Bytes for this topic
ConfigEntry retentionBytesEntry = new ConfigEntry(TopicConfig.RETENTION_BYTES_CONFIG, String.valueOf(retentionBytes));
updateConfig.put(resource, new Config(Collections.singleton(retentionBytesEntry)));

// update retention ms for this topic
ConfigEntry retentionMsEntry = new ConfigEntry(TopicConfig.RETENTION_MS_CONFIG, String.valueOf(retentionMs));
updateConfig.put(resource, new Config(Collections.singleton(retentionMsEntry)));

// update segment Bytes for this topic
ConfigEntry segmentBytesEntry = new ConfigEntry(TopicConfig.SEGMENT_BYTES_CONFIG, String.valueOf(segmentbytes));
updateConfig.put(resource, new Config(Collections.singleton(segmentBytesEntry)));

// update segment ms for this topic
ConfigEntry segmentMsEntry = new ConfigEntry(TopicConfig.SEGMENT_MS_CONFIG, String.valueOf(segmentMs));
updateConfig.put(resource, new Config(Collections.singleton(segmentMsEntry)));

// Update the configuration
client.alterConfigs(updateConfig);
ConfigResource resource=新的ConfigResource(ConfigResource.Type.TOPIC,topicName);
Map updateConfig=新HashMap();
//更新此主题的保留字节
ConfigEntry retentionBytesEntry=新的ConfigEntry(TopicConfig.RETENTION_BYTES_CONFIG,String.valueOf(retentionBytes));
updateConfig.put(资源,新配置(Collections.singleton(retentionBytesEntry));
//更新此主题的保留ms
ConfigEntry retentionMsEntry=新的ConfigEntry(TopicConfig.RETENTION\u MS\u CONFIG,String.valueOf(retentionMs));
updateConfig.put(资源,新配置(Collections.singleton(retentionMsEntry));
//更新此主题的段字节
ConfigEntry segmentBytesEntry=新的ConfigEntry(TopicConfig.SEGMENT_BYTES_CONFIG,String.valueOf(segmentbytes));
updateConfig.put(资源,新配置(Collections.singleton(segmentBytesEntry));
//更新此主题的ms段
ConfigEntry segmentry=新的ConfigEntry(TopicConfig.SEGMENT\u MS\u CONFIG,String.valueOf(segmentMs));
updateConfig.put(资源,新配置(Collections.singleton(segmentry));
//更新配置
client.alterConfigs(updateConfig);

我希望该主题正确地包含所有给定的配置值。

您的逻辑工作不正常,因为您使用同一个键多次调用
Map.put()
。因此,只保留最后一个条目

指定多个主题配置的正确方法是将它们添加到
ConfigEntry
对象中。仅在将
ConfigEntry
添加到
Map
之后

例如:

// Your Topic Resource
ConfigResource cr = new ConfigResource(Type.TOPIC, "mytopic");

// Create all your configurations
Collection<ConfigEntry> entries = new ArrayList<>();
entries.add(new ConfigEntry(TopicConfig.SEGMENT_BYTES_CONFIG, String.valueOf(segmentbytes)));
entries.add(new ConfigEntry(TopicConfig.RETENTION_BYTES_CONFIG, String.valueOf(retentionBytes)));
...

// Create the Map
Config config = new Config(entries);
Map<ConfigResource, Config> configs = new HashMap<>();
configs.put(cr, config);

// Call alterConfigs()
admin.alterConfigs(configs);
//您的主题资源
ConfigResource cr=新的ConfigResource(Type.TOPIC,“mytopic”);
//创建所有配置
集合项=新的ArrayList();
添加(新的ConfigEntry(TopicConfig.SEGMENT_BYTES_CONFIG,String.valueOf(segmentbytes));
添加(新的ConfigEntry(TopicConfig.RETENTION_BYTES_CONFIG,String.valueOf(retentionBytes));
...
//创建地图
配置=新配置(条目);
Map configs=new HashMap();
配置put(cr,config);
//调用alterConfigs()
管理员更改配置(配置);

哪些配置被覆盖?您所说的“配置被覆盖”是什么意思?我需要retention.ms和segment.ms具有特定的值(相同的值)。但我的代码仅将正确的值设置为在映射末尾设置的值,并覆盖首先设置的值。retentionMs和segmentMs都具有相同的值(60000),但只有segment.ms配置为60000。Retentionms获取一个默认值。如果我交换这些设置,则会发生相反的情况。