工具类提供,方法名见名知意。使用kafka admin
import org.apache.kafka.clients.admin.*;
import org.apache.kafka.common.KafkaFuture;import java.util.*;
import java.util.concurrent.ExecutionException;import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.ListTopicsOptions;
import org.apache.kafka.clients.admin.TopicDescription;
import org.apache.kafka.common.KafkaFuture;
import org.apache.kafka.common.TopicPartitionInfo;import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutionException;
public class KafkaTopicInfo {final static String ip="127.0.0.1:9090";public static void main(String[] args) {getListDetail();}public static void createTopic(String topicName) throws ExecutionException, InterruptedException {Properties props = new Properties();props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, ip);try (AdminClient adminClient = AdminClient.create(props)) {int numPartitions = 2;short replicationFactor = 1;NewTopic newTopic = new NewTopic(topicName, numPartitions, replicationFactor);adminClient.createTopics(Collections.singletonList(newTopic)).all().get();System.out.println("Topic created successfully: " + topicName);} catch (Exception e) {e.printStackTrace();}}public static void deleteTopic(String topicName) {Properties props = new Properties();props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, ip);try (AdminClient adminClient = AdminClient.create(props)) {DeleteTopicsResult deleteResult = adminClient.deleteTopics(Collections.singletonList(topicName));deleteResult.all().get();System.out.println("Topic deleted successfully: " + topicName);} catch (ExecutionException | InterruptedException e) {e.printStackTrace();}}public static void getList() {Properties props = new Properties();props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, ip);try (AdminClient adminClient = AdminClient.create(props)) {ListTopicsOptions options = new ListTopicsOptions();options.listInternal(true);ListTopicsResult topicsResult = adminClient.listTopics(options);Set<String> topics = topicsResult.names().get();System.out.println("Existing topics:");for (String topic : topics) {System.out.println(topic);}} catch (ExecutionException | InterruptedException e) {e.printStackTrace();}}public static void getListDetail() {Properties props = new Properties();props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, ip);try (AdminClient adminClient = AdminClient.create(props)) {ListTopicsOptions options = new ListTopicsOptions();options.listInternal(true);KafkaFuture<Set<String>> topics = adminClient.listTopics(options).names();System.out.println("Existing topics:");for (String topic : topics.get()) {System.out.println(topic);Set<String> topicSet = new HashSet<>();topicSet.add(topic);KafkaFuture<TopicDescription> topicDescriptionFuture = adminClient.describeTopics(topicSet).values().get(topic);TopicDescription topicDescription = topicDescriptionFuture.get();printTopicDetails(topicDescription);}} catch (ExecutionException | InterruptedException e) {e.printStackTrace();}}private static void printTopicDetails(TopicDescription topicDescription) {System.out.println("Topic: " + topicDescription.name());System.out.println("Partitions:");for (TopicPartitionInfo partition : topicDescription.partitions()) {System.out.printf(" Partition %d, Leader: %d, Replicas: %s, Isrs: %s%n",partition.partition(),partition.leader().id(),partition.replicas(),partition.isr());}System.out.println();}
}