如何在以下Java代码中将配置清理策略添加为“compact”到输入主题?

huangapple go评论111阅读模式
英文:

How to add config clean up policy as "compact" to input topic in below Java code?

问题

我想在输入主题中添加配置
如何在此代码中添加配置?cleanup.policy。

英文:
  1. //I want to add configuration to input topic
  2. public void createTopics(Properties envProps) {
  3. Map<String, Object> config = new HashMap<>();
  4. config.put("bootstrap.servers", envProps.getProperty("bootstrap.servers"));
  5. AdminClient client = AdminClient.create(config);
  6. List<NewTopic> topics = new ArrayList<>();
  7. topics.add(new NewTopic(
  8. envProps.getProperty("input.topic.name") ,
  9. Integer.parseInt(envProps.getProperty("input.topic.partitions")),
  10. Short.parseShort(envProps.getProperty("input.topic.replication.factor"))));
  11. topics.add(new NewTopic(
  12. envProps.getProperty("output.topic.name"),
  13. Integer.parseInt(envProps.getProperty("output.topic.partitions")),
  14. Short.parseShort(envProps.getProperty("output.topic.replication.factor"))));
  15. client.createTopics(topics);
  16. client.close();
  17. }
  18. public Properties loadEnvProperties(String fileName) throws IOException {
  19. Properties envProps = new Properties();
  20. FileInputStream input = new FileInputStream(fileName);
  21. envProps.load(input);
  22. input.close();
  23. return envProps;
  24. }

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.

huangapple
  • 本文由 发表于 2020年7月30日 19:18:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63172045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定