I have the following problem. I need to set the retention time in Kafka for certain selected topics. I found a solution where I can set it with the following command:
kafka-topics --zookeeper localhost:2181 --alter --topic topic-name --config retention.ms=-1
I checked in Kafka's Web UI and confirmed that it got changed.
If possible, I want to set the retention time in Java myself, but I can't seem to find the appropriate class/configuration to set the time. I thought I could get the information about the retention in the ProducerConfig class, but I couldn't find it there.
Is it even possible to set the retention time in Java and if possible, how can I get it done?
Thanks in advance!
This works for me :)
private void setRetentionTime(String topicName, int retentionTime) {
ConfigResource resource = new ConfigResource(Type.TOPIC, topicName);
Collection<ConfigEntry> entries = new ArrayList<>();
entries.add(new ConfigEntry(TopicConfig.RETENTION_MS_CONFIG, String.valueOf(retentionTime)));
Config config = new Config(entries);
Map<ConfigResource, Config> configs = new HashMap<>();
configs.put(resource, config);
AdminClient client = kafkaConfig.createAdminClient();
client.alterConfigs(configs);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With