|
| 1 | +/* |
| 2 | + ** OKafka Java Client version 23.4. |
| 3 | + ** |
| 4 | + ** Copyright (c) 2019, 2024 Oracle and/or its affiliates. |
| 5 | + ** Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.oracle.okafka.examples; |
| 9 | + |
| 10 | +import java.io.FileNotFoundException; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.util.*; |
| 14 | +import java.util.concurrent.ExecutionException; |
| 15 | + |
| 16 | +import org.apache.kafka.clients.admin.Admin; |
| 17 | +import org.apache.kafka.clients.admin.CreateTopicsResult; |
| 18 | +import org.apache.kafka.clients.admin.DeleteTopicsResult; |
| 19 | +import org.apache.kafka.clients.admin.NewTopic; |
| 20 | +import org.apache.kafka.common.KafkaFuture; |
| 21 | + |
| 22 | +import org.oracle.okafka.clients.admin.AdminClient; |
| 23 | + |
| 24 | +public class OKafkaAdminTopic { |
| 25 | + |
| 26 | + public static void main(String[] args) { |
| 27 | + System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "INFO"); |
| 28 | + |
| 29 | + // Get application properties |
| 30 | + Properties appProperties = null; |
| 31 | + try { |
| 32 | + appProperties = getProperties(); |
| 33 | + if (appProperties == null) { |
| 34 | + System.out.println("Application properties not found!"); |
| 35 | + System.exit(-1); |
| 36 | + } |
| 37 | + } catch (Exception e) { |
| 38 | + System.out.println("Application properties not found!"); |
| 39 | + System.out.println("Exception: " + e); |
| 40 | + System.exit(-1); |
| 41 | + } |
| 42 | + |
| 43 | + if (args.length < 2) { |
| 44 | + System.out.println("Usage: java OKafkaAdminTopic [CREATE|DELETE] topic1 ... topicN"); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + ArrayList<String> topicsName = new ArrayList<>(); |
| 49 | + for (int i = 1; i < args.length; i++) { |
| 50 | + topicsName.add(args[i]); |
| 51 | + } |
| 52 | + |
| 53 | + String operation = args[0].toUpperCase(); |
| 54 | + switch (operation) { |
| 55 | + case "CREATE": |
| 56 | + createTopic(appProperties, topicsName); |
| 57 | + break; |
| 58 | + |
| 59 | + case "DELETE": |
| 60 | + deleteTopic(appProperties, topicsName); |
| 61 | + break; |
| 62 | + |
| 63 | + default: |
| 64 | + System.out.println("Error: Invalid operation."); |
| 65 | + } |
| 66 | + |
| 67 | + } |
| 68 | + |
| 69 | + private static void createTopic(Properties appProperties, ArrayList<String> topicsName) { |
| 70 | + try (Admin admin = AdminClient.create(appProperties)) { |
| 71 | + //Create Topic named TXEQ_1 and TXEQ_2 with 10 Partitions. |
| 72 | + |
| 73 | + ArrayList<NewTopic> topics = new ArrayList<>(); |
| 74 | + |
| 75 | + for (String topicName : topicsName) { |
| 76 | + NewTopic nt = new NewTopic(topicName, 10, (short)0); |
| 77 | + topics.add(nt); |
| 78 | + } |
| 79 | + |
| 80 | + CreateTopicsResult result = admin.createTopics(topics); |
| 81 | + try { |
| 82 | + KafkaFuture<Void> ftr = result.all(); |
| 83 | + ftr.get(); |
| 84 | + } catch ( InterruptedException | ExecutionException e ) { |
| 85 | + throw new IllegalStateException(e); |
| 86 | + } |
| 87 | + |
| 88 | + System.out.println("Topic(s) created. Closing OKafka admin now"); |
| 89 | + } |
| 90 | + catch(Exception e) |
| 91 | + { |
| 92 | + System.out.println("Exception while creating topic " + e); |
| 93 | + e.printStackTrace(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static void deleteTopic(Properties appProperties, ArrayList<String> topicsName) { |
| 98 | + try (Admin admin = AdminClient.create(appProperties)) { |
| 99 | + DeleteTopicsResult delResult = admin.deleteTopics(topicsName); |
| 100 | + Thread.sleep(5000); |
| 101 | + System.out.println("Closing OKafka admin now"); |
| 102 | + } |
| 103 | + catch(Exception e) |
| 104 | + { |
| 105 | + System.out.println("Exception while deleting topic " + e); |
| 106 | + e.printStackTrace(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private static Properties getProperties() throws IOException { |
| 111 | + InputStream inputStream = null; |
| 112 | + Properties appProperties = null; |
| 113 | + |
| 114 | + try { |
| 115 | + Properties prop = new Properties(); |
| 116 | + String propFileName = "config.properties"; |
| 117 | + inputStream = OKafkaAdminTopic.class.getClassLoader().getResourceAsStream(propFileName); |
| 118 | + if (inputStream != null) { |
| 119 | + prop.load(inputStream); |
| 120 | + } else { |
| 121 | + throw new FileNotFoundException("property file '" + propFileName + "' not found."); |
| 122 | + } |
| 123 | + appProperties = prop; |
| 124 | + |
| 125 | + } catch (Exception e) { |
| 126 | + System.out.println("Exception: " + e); |
| 127 | + throw e; |
| 128 | + } finally { |
| 129 | + inputStream.close(); |
| 130 | + } |
| 131 | + return appProperties; |
| 132 | + } |
| 133 | +} |
0 commit comments