代码如下:
类一:
public class BasicInfo {
public TopicDescription topicExistence(String ipPort, String topicName){
BasicInfo basicInfo = new BasicInfo();
AdminClient adminClient = basicInfo.adminClient(ipPort);
DescribeTopicsResult topicsResult = adminClient.describeTopics(Collections.singleton(topicName));
TopicDescription topicDescription;
try {
topicDescription = topicsResult.values().get(topicName).get();
}catch (InterruptedException | ExecutionException e){
topicDescription=null;
}
return topicDescription;
}
public AdminClient adminClient(String ipPort) {
AdminClient adminClient;
Properties props = new Properties();
props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,ipPort);
//超时限制10秒
props.put(AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG,10000);
//设置重试次数
props.put(AdminClientConfig.RETRIES_CONFIG,3);
//设置重试间隔时间
props.put(AdminClientConfig.RETRY_BACKOFF_MS_CONFIG,1000);
adminClient = AdminClient.create(props);
return adminClient;
}
}
类二:
public class TopicApply {
public void applyTopic(String ipPort,String topicName,int numPartitions,short replicationFactor,String retentionMS) {
BasicInfo basicInfo = new BasicInfo();
AdminClient adminClient = basicInfo.adminClient(ipPort);
short numBroker=0;
try{
numBroker = (short) adminClient.describeCluster().nodes().get().size();
}catch (InterruptedException|ExecutionException e){
System.out.println("获取集群broker个数失败!");
}
TopicDescription topicDescription = basicInfo.topicExistence(ipPort, topicName);
if (topicDescription != null) {
System.out.println("topic " + topicName + " Existed");
} else
if (numPartitions < 3) {
System.out.println("分区数必须 >=3!");
} else if (replicationFactor < 1) {
System.out.println("副本数必须 >=1!");
} else if (replicationFactor > numBroker){
System.out.println("副本数不能大于集群broker数量");
} else {
NewTopic newTopic = new NewTopic(topicName, numPartitions, replicationFactor); //创建主题的配置
HashMap<String, String> retentionMs = new HashMap<>();
//数据保留时间
retentionMs.put(TopicConfig.RETENTION_MS_CONFIG,retentionMS);
//设置清理策略为删除retentionMs.put(TopicConfig.CLEANUP_POLICY_CONFIG,TopicConfig.CLEANUP_POLICY_DELETE);
newTopic.configs(retentionMs);
adminClient.createTopics(Collections.singleton(newTopic));
//等待主题创建完成
try {
adminClient.listTopics().names().thenApply(topicNames -> {
if (topicNames.contains(topicName)) {
System.out.println("topic " + topicName + " created successfully");
} else {
System.out.println("Failed to create topic " + topicName);
}
return null;
}).get();//等待任务完成
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String ipPort="IP:port";
String topic = scanner.next();
int numPartitions=3;
short replicationFactor=1;
String retentionMS="7200000";
TopicApply topicApply = new TopicApply();
topicApply.applyTopic(ipPort, topic, numPartitions, replicationFactor, retentionMS);
scanner.close();
}
}