英文:
How to add config clean up policy as "compact" to input topic in below Java code?
问题
我想在输入主题中添加配置
如何在此代码中添加配置?cleanup.policy。
英文:
//I want to add configuration to input topic
public void createTopics(Properties envProps) {
Map<String, Object> config = new HashMap<>();
config.put("bootstrap.servers", envProps.getProperty("bootstrap.servers"));
AdminClient client = AdminClient.create(config);
List<NewTopic> topics = new ArrayList<>();
topics.add(new NewTopic(
envProps.getProperty("input.topic.name") ,
Integer.parseInt(envProps.getProperty("input.topic.partitions")),
Short.parseShort(envProps.getProperty("input.topic.replication.factor"))));
topics.add(new NewTopic(
envProps.getProperty("output.topic.name"),
Integer.parseInt(envProps.getProperty("output.topic.partitions")),
Short.parseShort(envProps.getProperty("output.topic.replication.factor"))));
client.createTopics(topics);
client.close();
}
public Properties loadEnvProperties(String fileName) throws IOException {
Properties envProps = new Properties();
FileInputStream input = new FileInputStream(fileName);
envProps.load(input);
input.close();
return envProps;
}
I am able to create the topics. But, I want to configure topic in this code ie., cleanup.policy. How can I incorporate this here?
答案1
得分: 1
你可以在这里找到 kafka-topics --create
的源代码
正如你所看到的,它调用了 NewTopic#configs
方法,传入了一个 Map
在这个 Map 中,你会有 (cleanup.policy, compact)
你也可以自行调用 TopicCommand.main
,而不必重写大部分现有的代码。
英文:
You can find the source code for kafka-topics --create
here
As you can see, it calls NewTopic#configs
with a Map
In that map, you would have (cleanup.policy, compact)
You could also call TopicCommand.main
on your own, rather than re-write most of the existing code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论