Java Code Examples for org.apache.kafka.clients.admin.AdminClient#close()
The following examples show how to use
org.apache.kafka.clients.admin.AdminClient#close() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KafkaRegistryConfiguration.java From apicurio-registry with Apache License 2.0 | 6 votes |
@Produces @ApplicationScoped public CloseableSupplier<Boolean> livenessCheck( @RegistryProperties( value = {"registry.kafka.common", "registry.kafka.liveness-check"}, empties = {"ssl.endpoint.identification.algorithm="} ) Properties properties ) { AdminClient admin = AdminClient.create(properties); return new CloseableSupplier<Boolean>() { @Override public void close() { admin.close(); } @Override public Boolean get() { return (admin.listTopics() != null); } }; }
Example 2
Source File: KafkaTestEnvironmentImpl.java From flink with Apache License 2.0 | 6 votes |
@Override public void deleteTestTopic(String topic) { LOG.info("Deleting topic {}", topic); Properties props = getSecureProperties(); props.putAll(getStandardProperties()); String clientId = Long.toString(new Random().nextLong()); props.put("client.id", clientId); AdminClient adminClient = AdminClient.create(props); // We do not use a try-catch clause here so we can apply a timeout to the admin client closure. try { tryDelete(adminClient, topic); } catch (Exception e) { e.printStackTrace(); fail(String.format("Delete test topic : %s failed, %s", topic, e.getMessage())); } finally { adminClient.close(Duration.ofMillis(5000L)); maybePrintDanglingThreadStacktrace(clientId); } }
Example 3
Source File: FilterEvents.java From kafka-tutorials with Apache License 2.0 | 6 votes |
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(); }
Example 4
Source File: TumblingWindow.java From kafka-tutorials with Apache License 2.0 | 6 votes |
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<>(); Map<String, String> topicConfigs = new HashMap<>(); topicConfigs.put("retention.ms", Long.toString(Long.MAX_VALUE)); NewTopic ratings = new NewTopic(envProps.getProperty("rating.topic.name"), Integer.parseInt(envProps.getProperty("rating.topic.partitions")), Short.parseShort(envProps.getProperty("rating.topic.replication.factor"))); ratings.configs(topicConfigs); topics.add(ratings); NewTopic counts = new NewTopic(envProps.getProperty("rating.count.topic.name"), Integer.parseInt(envProps.getProperty("rating.count.topic.partitions")), Short.parseShort(envProps.getProperty("rating.count.topic.replication.factor"))); counts.configs(topicConfigs); topics.add(counts); client.createTopics(topics); client.close(); }
Example 5
Source File: AggregatingSum.java From kafka-tutorials with Apache License 2.0 | 6 votes |
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(); }
Example 6
Source File: FkJoinTableToTable.java From kafka-tutorials with Apache License 2.0 | 6 votes |
public void createTopics(final Properties envProps) { final Map<String, Object> config = new HashMap<>(); config.put("bootstrap.servers", envProps.getProperty("bootstrap.servers")); final AdminClient client = AdminClient.create(config); final List<NewTopic> topics = new ArrayList<>(); topics.add(new NewTopic( envProps.getProperty("album.topic.name"), Integer.parseInt(envProps.getProperty("album.topic.partitions")), Short.parseShort(envProps.getProperty("album.topic.replication.factor")))); topics.add(new NewTopic( envProps.getProperty("tracks.purchase.topic.name"), Integer.parseInt(envProps.getProperty("tracks.purchase.topic.partitions")), Short.parseShort(envProps.getProperty("tracks.purchase.topic.replication.factor")))); topics.add(new NewTopic( envProps.getProperty("music.interest.topic.name"), Integer.parseInt(envProps.getProperty("music.interest.topic.partitions")), Short.parseShort(envProps.getProperty("music.interest.topic.replication.factor")))); client.createTopics(topics); client.close(); }
Example 7
Source File: AggregatingCount.java From kafka-tutorials with Apache License 2.0 | 6 votes |
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(); }
Example 8
Source File: TransformStream.java From kafka-tutorials with Apache License 2.0 | 6 votes |
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(); }
Example 9
Source File: StreamsIngest.java From kafka-tutorials with Apache License 2.0 | 6 votes |
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(); }
Example 10
Source File: MergeStreams.java From kafka-tutorials with Apache License 2.0 | 6 votes |
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.rock.topic.name"), Integer.parseInt(envProps.getProperty("input.rock.topic.partitions")), Short.parseShort(envProps.getProperty("input.rock.topic.replication.factor")))); topics.add(new NewTopic( envProps.getProperty("input.classical.topic.name"), Integer.parseInt(envProps.getProperty("input.classical.topic.partitions")), Short.parseShort(envProps.getProperty("input.classical.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(); }
Example 11
Source File: BrokerServiceImpl.java From kafka-eagle with Apache License 2.0 | 5 votes |
/** Add topic partitions. */ public Map<String, Object> createTopicPartitions(String clusterAlias, String topic, int totalCount) { Map<String, Object> targets = new HashMap<String, Object>(); int existPartitions = (int) partitionNumbers(clusterAlias, topic); Properties prop = new Properties(); prop.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, kafkaService.getKafkaBrokerServer(clusterAlias)); if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.sasl.enable")) { kafkaService.sasl(prop, clusterAlias); } if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.ssl.enable")) { kafkaService.ssl(prop, clusterAlias); } AdminClient adminClient = null; try { adminClient = AdminClient.create(prop); Map<String, NewPartitions> newPartitions = new HashMap<String, NewPartitions>(); newPartitions.put(topic, NewPartitions.increaseTo(existPartitions + totalCount)); adminClient.createPartitions(newPartitions); targets.put("status", "success"); targets.put("info", "Add topic[" + topic + "], before partition[" + existPartitions + "], after partition[" + (existPartitions + totalCount) + "] has successed."); } catch (Exception e) { LOG.info("Add kafka topic partitions has error, msg is " + e.getMessage()); e.printStackTrace(); targets.put("status", "failed"); targets.put("info", "Add kafka topic partitions has error, msg is " + e.getMessage()); } finally { adminClient.close(); } return targets; }
Example 12
Source File: KafkaSender.java From zipkin-reporter-java with Apache License 2.0 | 5 votes |
@Override public synchronized void close() { if (closeCalled) return; KafkaProducer<byte[], byte[]> producer = this.producer; if (producer != null) producer.close(); AdminClient adminClient = this.adminClient; if (adminClient != null) adminClient.close(1, TimeUnit.SECONDS); closeCalled = true; }
Example 13
Source File: KafkaServiceImpl.java From kafka-eagle with Apache License 2.0 | 5 votes |
/** * Create topic to kafka cluster, it is worth noting that the backup number * must be less than or equal to brokers data. * * @param topicName * Create topic name. * @param partitions * Create topic partitions. * @param replic * Replic numbers. * @return Map. */ public Map<String, Object> create(String clusterAlias, String topicName, String partitions, String replic) { Map<String, Object> targets = new HashMap<String, Object>(); int brokers = getAllBrokersInfo(clusterAlias).size(); if (Integer.parseInt(replic) > brokers) { targets.put("status", "error"); targets.put("info", "replication factor: " + replic + " larger than available brokers: " + brokers); return targets; } Properties prop = new Properties(); prop.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, parseBrokerServer(clusterAlias)); if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.sasl.enable")) { sasl(prop, clusterAlias); } if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.ssl.enable")) { ssl(prop, clusterAlias); } AdminClient adminClient = null; try { adminClient = AdminClient.create(prop); NewTopic newTopic = new NewTopic(topicName, Integer.valueOf(partitions), Short.valueOf(replic)); adminClient.createTopics(Collections.singleton(newTopic)).all().get(); } catch (Exception e) { LOG.info("Create kafka topic has error, msg is " + e.getMessage()); e.printStackTrace(); } finally { adminClient.close(); } targets.put("status", "success"); targets.put("info", "Create topic[" + topicName + "] has successed,partitions numbers is [" + partitions + "],replication-factor numbers is [" + replic + "]"); return targets; }
Example 14
Source File: KafkaServiceImpl.java From kafka-eagle with Apache License 2.0 | 5 votes |
/** * Get kafka group consumer all topics lags. */ public long getKafkaLag(String clusterAlias, String group, String ketopic) { long lag = 0L; Properties prop = new Properties(); prop.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, parseBrokerServer(clusterAlias)); if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.sasl.enable")) { sasl(prop, clusterAlias); } if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.ssl.enable")) { ssl(prop, clusterAlias); } AdminClient adminClient = null; try { adminClient = AdminClient.create(prop); ListConsumerGroupOffsetsResult offsets = adminClient.listConsumerGroupOffsets(group); for (Entry<TopicPartition, OffsetAndMetadata> entry : offsets.partitionsToOffsetAndMetadata().get().entrySet()) { if (ketopic.equals(entry.getKey().topic())) { long logSize = getKafkaLogSize(clusterAlias, entry.getKey().topic(), entry.getKey().partition()); lag += logSize - entry.getValue().offset(); } } } catch (Exception e) { LOG.error("Get cluster[" + clusterAlias + "] group[" + group + "] topic[" + ketopic + "] consumer lag has error, msg is " + e.getMessage()); e.printStackTrace(); } finally { adminClient.close(); } return lag; }
Example 15
Source File: KafkaServiceImpl.java From kafka-eagle with Apache License 2.0 | 5 votes |
/** * Get kafka 0.10.x, 1.x, 2.x consumer groups. */ public int getKafkaConsumerGroups(String clusterAlias) { Properties prop = new Properties(); int counter = 0; prop.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, parseBrokerServer(clusterAlias)); if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.sasl.enable")) { sasl(prop, clusterAlias); } if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.ssl.enable")) { ssl(prop, clusterAlias); } AdminClient adminClient = null; try { adminClient = AdminClient.create(prop); ListConsumerGroupsResult consumerGroups = adminClient.listConsumerGroups(); java.util.Iterator<ConsumerGroupListing> groups = consumerGroups.all().get().iterator(); while (groups.hasNext()) { String groupId = groups.next().groupId(); if (!groupId.contains("kafka.eagle")) { counter++; } } } catch (Exception e) { LOG.info("Get kafka consumer group has error, msg is " + e.getMessage()); e.printStackTrace(); } finally { adminClient.close(); } return counter; }
Example 16
Source File: KafkaServiceImpl.java From kafka-eagle with Apache License 2.0 | 5 votes |
/** * Delete topic to kafka cluster. */ public Map<String, Object> delete(String clusterAlias, String topicName) { Map<String, Object> targets = new HashMap<String, Object>(); Properties prop = new Properties(); prop.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, parseBrokerServer(clusterAlias)); if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.sasl.enable")) { sasl(prop, clusterAlias); } if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.ssl.enable")) { ssl(prop, clusterAlias); } AdminClient adminClient = null; try { adminClient = AdminClient.create(prop); adminClient.deleteTopics(Collections.singleton(topicName)).all().get(); targets.put("status", "success"); } catch (Exception e) { LOG.info("Delete kafka topic has error, msg is " + e.getMessage()); e.printStackTrace(); targets.put("status", "failed"); } finally { adminClient.close(); } return targets; }
Example 17
Source File: SplitStream.java From kafka-tutorials with Apache License 2.0 | 5 votes |
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.drama.topic.name"), Integer.parseInt(envProps.getProperty("output.drama.topic.partitions")), Short.parseShort(envProps.getProperty("output.drama.topic.replication.factor")))); topics.add(new NewTopic( envProps.getProperty("output.fantasy.topic.name"), Integer.parseInt(envProps.getProperty("output.fantasy.topic.partitions")), Short.parseShort(envProps.getProperty("output.fantasy.topic.replication.factor")))); topics.add(new NewTopic( envProps.getProperty("output.other.topic.name"), Integer.parseInt(envProps.getProperty("output.other.topic.partitions")), Short.parseShort(envProps.getProperty("output.other.topic.replication.factor")))); client.createTopics(topics); client.close(); }
Example 18
Source File: KafkaServiceImpl.java From kafka-eagle with Apache License 2.0 | 5 votes |
/** * Get kafka 0.10.x, 1.x, 2.x offset from topic. */ public String getKafkaOffset(String clusterAlias) { Properties prop = new Properties(); prop.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, parseBrokerServer(clusterAlias)); if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.sasl.enable")) { sasl(prop, clusterAlias); } if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.ssl.enable")) { ssl(prop, clusterAlias); } JSONArray targets = new JSONArray(); AdminClient adminClient = null; try { adminClient = AdminClient.create(prop); ListConsumerGroupsResult consumerGroups = adminClient.listConsumerGroups(); java.util.Iterator<ConsumerGroupListing> groups = consumerGroups.all().get().iterator(); while (groups.hasNext()) { String groupId = groups.next().groupId(); if (!groupId.contains("kafka.eagle")) { ListConsumerGroupOffsetsResult offsets = adminClient.listConsumerGroupOffsets(groupId); for (Entry<TopicPartition, OffsetAndMetadata> entry : offsets.partitionsToOffsetAndMetadata().get().entrySet()) { JSONObject object = new JSONObject(); object.put("group", groupId); object.put("topic", entry.getKey().topic()); object.put("partition", entry.getKey().partition()); object.put("offset", entry.getValue().offset()); object.put("timestamp", CalendarUtils.getDate()); targets.add(object); } } } } catch (Exception e) { LOG.error("Get consumer offset has error, msg is " + e.getMessage()); e.printStackTrace(); } finally { adminClient.close(); } return targets.toJSONString(); }
Example 19
Source File: KafkaMetricsServiceImpl.java From kafka-eagle with Apache License 2.0 | 4 votes |
public JSONObject topicKafkaCapacity(String clusterAlias, String topic) { if (Kafka.CONSUMER_OFFSET_TOPIC.equals(topic)) { return new JSONObject(); } Properties prop = new Properties(); prop.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, parseBrokerServer(clusterAlias)); if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.sasl.enable")) { kafkaService.sasl(prop, clusterAlias); } if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.ssl.enable")) { kafkaService.ssl(prop, clusterAlias); } long sum = 0L; AdminClient adminClient = null; try { adminClient = AdminClient.create(prop); List<MetadataInfo> leaders = kafkaService.findKafkaLeader(clusterAlias, topic); Set<Integer> ids = new HashSet<>(); for (MetadataInfo metadata : leaders) { ids.add(metadata.getLeader()); } DescribeLogDirsResult logSizeBytes = adminClient.describeLogDirs(ids); Map<Integer, Map<String, DescribeLogDirsResponse.LogDirInfo>> tmp = logSizeBytes.all().get(); if (tmp == null) { return new JSONObject(); } for (Map.Entry<Integer, Map<String, DescribeLogDirsResponse.LogDirInfo>> entry : tmp.entrySet()) { Map<String, DescribeLogDirsResponse.LogDirInfo> logDirInfos = entry.getValue(); for (Map.Entry<String, DescribeLogDirsResponse.LogDirInfo> logDirInfo : logDirInfos.entrySet()) { DescribeLogDirsResponse.LogDirInfo info = logDirInfo.getValue(); Map<TopicPartition, DescribeLogDirsResponse.ReplicaInfo> replicaInfoMap = info.replicaInfos; for (Map.Entry<TopicPartition, DescribeLogDirsResponse.ReplicaInfo> replicas : replicaInfoMap.entrySet()) { if (topic.equals(replicas.getKey().topic())) { sum += replicas.getValue().size; } } } } } catch (Exception e) { LOG.error("Get topic capacity has error, msg is " + e.getCause().getMessage()); e.printStackTrace(); } finally { adminClient.close(); } return StrUtils.stringifyByObject(sum); }
Example 20
Source File: LogConfigCheckTool.java From singer with Apache License 2.0 | 4 votes |
public static void main(String[] args) throws InterruptedException, ExecutionException { if (args.length < 1) { System.err.println("Must specify the configuration directory"); System.exit(-1); } String configDirectory = args[0]; File confDir = new File(configDirectory); if (!confDir.isDirectory()) { System.out.println("Supplied path is not a directory:" + configDirectory); System.exit(-1); } for (String logFile : confDir.list()) { if (!logFile.matches(DirectorySingerConfigurator.CONFIG_FILE_PATTERN)) { System.err.println("Invalid configuration file name:" + logFile); System.exit(-1); } } Map<String, Set<String>> topicMap = new HashMap<>(); File[] listFiles = confDir.listFiles(); Arrays.sort(listFiles); for (File file : listFiles) { try { SingerLogConfig slc = LogConfigUtils.parseLogConfigFromFile(file); KafkaWriterConfig kafkaWriterConfig = slc.getLogStreamWriterConfig().getKafkaWriterConfig(); if ((slc.getLogStreamWriterConfig().getType() == WriterType.KAFKA || slc.getLogStreamWriterConfig().getType() == WriterType.KAFKA08)) { if (kafkaWriterConfig == null) { System.err .println("Kafka writer specified with missing Kafka config:" + file.getName()); System.exit(-1); } else { KafkaProducerConfig producerConfig = kafkaWriterConfig.getProducerConfig(); if (producerConfig.getBrokerLists().isEmpty()) { System.err .println("No brokers in the kafka configuration specified:" + file.getName()); System.exit(-1); } String broker = producerConfig.getBrokerLists().get(0); Set<String> topics = topicMap.get(broker); if (topics == null) { topics = new HashSet<>(); topicMap.put(broker, topics); } topics.add(kafkaWriterConfig.getTopic() + " " + file.getName()); } } } catch (Exception e) { e.printStackTrace(); System.err.println("Bad configuration file:" + file.getName()); System.exit(-1); } } for (Entry<String, Set<String>> entry : topicMap.entrySet()) { String bootstrapBroker = entry.getKey(); Properties properties = new Properties(); bootstrapBroker = bootstrapBroker.replace("9093", "9092"); properties.put("bootstrap.servers", bootstrapBroker); AdminClient cl = AdminClient.create(properties); Set<String> topicSet = cl.listTopics().names().get(); for (String topicFileMap : entry.getValue()) { String[] splits = topicFileMap.split(" "); String topic = splits[0]; if (!topicSet.contains(topic)) { System.err.println("Topic:" + topic + " doesn't exist for file:" + splits[1]); cl.close(); System.exit(-1); } } cl.close(); } }