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

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

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.

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:

确定