|
| 1 | +package com.baeldung.kafka.admin; |
| 2 | + |
| 3 | +import org.apache.kafka.clients.admin.Admin; |
| 4 | +import org.apache.kafka.clients.admin.AdminClientConfig; |
| 5 | +import org.apache.kafka.clients.admin.CreateTopicsOptions; |
| 6 | +import org.apache.kafka.clients.admin.CreateTopicsResult; |
| 7 | +import org.apache.kafka.clients.admin.NewTopic; |
| 8 | +import org.apache.kafka.common.KafkaFuture; |
| 9 | +import org.apache.kafka.common.config.TopicConfig; |
| 10 | + |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.HashMap; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Properties; |
| 15 | + |
| 16 | +public class KafkaTopicApplication { |
| 17 | + |
| 18 | + private final Properties properties; |
| 19 | + |
| 20 | + public KafkaTopicApplication(Properties properties) { |
| 21 | + this.properties = properties; |
| 22 | + } |
| 23 | + |
| 24 | + public void createTopic(String topicName) throws Exception { |
| 25 | + try (Admin admin = Admin.create(properties)) { |
| 26 | + int partitions = 1; |
| 27 | + short replicationFactor = 1; |
| 28 | + NewTopic newTopic = new NewTopic(topicName, partitions, replicationFactor); |
| 29 | + |
| 30 | + CreateTopicsResult result = admin.createTopics( |
| 31 | + Collections.singleton(newTopic)); |
| 32 | + |
| 33 | + // get the async result for the new topic creation |
| 34 | + KafkaFuture<Void> future = result.values().get(topicName); |
| 35 | + |
| 36 | + // call get() to block until topic creation has completed or failed |
| 37 | + future.get(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public void createTopicWithOptions(String topicName) throws Exception { |
| 42 | + Properties props = new Properties(); |
| 43 | + props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); |
| 44 | + |
| 45 | + try (Admin admin = Admin.create(props)) { |
| 46 | + int partitions = 1; |
| 47 | + short replicationFactor = 1; |
| 48 | + NewTopic newTopic = new NewTopic(topicName, partitions, replicationFactor); |
| 49 | + |
| 50 | + CreateTopicsOptions topicOptions = new CreateTopicsOptions() |
| 51 | + .validateOnly(true) |
| 52 | + .retryOnQuotaViolation(true); |
| 53 | + |
| 54 | + CreateTopicsResult result = admin.createTopics( |
| 55 | + Collections.singleton(newTopic), topicOptions |
| 56 | + ); |
| 57 | + |
| 58 | + KafkaFuture<Void> future = result.values().get(topicName); |
| 59 | + future.get(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public void createCompactedTopicWithCompression(String topicName) throws Exception { |
| 64 | + Properties props = new Properties(); |
| 65 | + props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); |
| 66 | + |
| 67 | + try (Admin admin = Admin.create(props)) { |
| 68 | + int partitions = 1; |
| 69 | + short replicationFactor = 1; |
| 70 | + |
| 71 | + // Create a compacted topic with 'lz4' compression codec |
| 72 | + Map<String, String> newTopicConfig = new HashMap<>(); |
| 73 | + newTopicConfig.put(TopicConfig.CLEANUP_POLICY_CONFIG, TopicConfig.CLEANUP_POLICY_COMPACT); |
| 74 | + newTopicConfig.put(TopicConfig.COMPRESSION_TYPE_CONFIG, "lz4"); |
| 75 | + NewTopic newTopic = new NewTopic(topicName, partitions, replicationFactor) |
| 76 | + .configs(newTopicConfig); |
| 77 | + |
| 78 | + CreateTopicsResult result = admin.createTopics( |
| 79 | + Collections.singleton(newTopic) |
| 80 | + ); |
| 81 | + |
| 82 | + KafkaFuture<Void> future = result.values().get(topicName); |
| 83 | + future.get(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments