java批量創建刪除Kafka的topic
一、前言
參考資料:Kafka 0.11客戶端集羣管理工具AdminClient
https://blog.csdn.net/u012501054/article/details/80594374
2.批量創建刪除Kafka的topic
2.集羣管理工具AdminClient (新版本kafka)
較爲新的版本:Kafka 0.11以上版本上測試的,新的API
集羣管理工具AdminClient
package zktest.navinfo;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.CreateTopicsResult;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.common.KafkaFuture;
import java.util.ArrayList;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
/** 批量創建kafka topic| 批量刪除kafka topic
* @author fangchangtan
* @date 2019-11-28 16:12
*/
public class BatchCreateTopicUtils {
public static AdminClient client;
public static void main(String[] args) {
Properties props = new Properties();
props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "172.xx.xx.62:9192,172.xx.xx.66:9192,172.xx.xx.67:9192");
try (AdminClient client = AdminClient.create(props)) {
//創建topic
// createTopics(client);
//刪除topic
deleteTopics(client);
} catch (Exception e) {
e.printStackTrace();
}
}
/**批量創建kafka的topic
* create multiple topics
* @param client
*/
public static void createTopics(AdminClient client) throws ExecutionException, InterruptedException {
//批量創建kafka的topic ,請指定分區數:numPartitions
NewTopic newTopic1 = new NewTopic("topic-good500", 10, (short)3);
NewTopic newTopic2 = new NewTopic("topic-good501", 11, (short)3);
NewTopic newTopic3 = new NewTopic("topic-good502", 12, (short)3);
final ArrayList<NewTopic> newTopicsList = new ArrayList<>();
newTopicsList.add(newTopic1);
newTopicsList.add(newTopic2);
newTopicsList.add(newTopic3);
CreateTopicsResult ret = client.createTopics(newTopicsList);
ret.all().get();
System.out.println("[info]: Create Topic success!");
}
/**
* delete the given topics
* @param client
*/
public static void deleteTopics(AdminClient client) throws ExecutionException, InterruptedException {
//批量創建kafka的topic
final ArrayList<String> listTopic = new ArrayList<>();
listTopic.add("topic-good500");
listTopic.add("topic-good501");
listTopic.add("topic-good502");
KafkaFuture<Void> futures = client.deleteTopics(listTopic).all();
futures.get();
System.out.println("[info]: delete Topic success!");
}
}
2.2 老版本,批量創建和刪除topic的api如下:
package zktest.navinfo;
import kafka.admin.AdminUtils;
import kafka.server.ConfigType;
import kafka.utils.ZkUtils;
import org.apache.kafka.common.security.JaasUtils;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
/** 批量創建kafkaTopic
* @author fangchangtan
* @date 2019-11-28 9:10
*/
public class CreateKakfaTopicUtil {
public static void main(String[] args) {
//zookeeper地址:端口號
String ZkStr = "172.xx.xx.64:21817,172.xx.xx.65:21817,172.xx.xx.66:21817/test-kafka-cluster";
// String ZkStr = "172.19.32.62:2181";
//topic對象
KafkaTopicBean topicBean = new KafkaTopicBean();
topicBean.setTopicName("topic-fang600"); //topic名稱
topicBean.setPartition(6); //分區數量設置爲1
topicBean.setReplication(2); //副本數量設置爲1
//創建topic
createKafaTopic(ZkStr,topicBean);
//刪除topic
// deleteKafaTopic(ZkStr,topicBean);
}
/**
* 創建kafka的topic
* @param ZkStr
* @param topicBean
*/
public static void createKafaTopic(String ZkStr,KafkaTopicBean topicBean) {
ZkUtils zkUtils = ZkUtils.apply(ZkStr, 30000, 30000, JaasUtils.isZkSecurityEnabled());
System.out.println("ZkStr: "+ZkStr);
AdminUtils.createTopic(zkUtils, topicBean.getTopicName(), topicBean.getPartition(),
topicBean.getReplication(), new Properties(), AdminUtils.createTopic$default$6());
System.out.println("messages:successful create!");
zkUtils.close();
}
/**
* 刪除topic
* @param ZkStr
* @param topicBean
*/
public static void deleteKafaTopic(String ZkStr,KafkaTopicBean topicBean) {
ZkUtils zkUtils = ZkUtils.
apply(ZkStr, 30000, 30000,JaasUtils.isZkSecurityEnabled());
AdminUtils.deleteTopic(zkUtils, topicBean.getTopicName());
zkUtils.close();
}
/**
* 查詢topic
*/
public static void queryTopic(String ZkStr,KafkaTopicBean topicBean) {
ZkUtils zkUtils = ZkUtils.apply(ZkStr, 30000, 30000, JaasUtils.isZkSecurityEnabled());// 獲取topic 'test'的topic屬性屬性
Properties props = AdminUtils.fetchEntityConfig(zkUtils, ConfigType.Topic(), topicBean.getTopicName());// 查詢topic-level屬性
Iterator it = props.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry=(Map.Entry)it.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(key + " = " + value);
}
zkUtils.close();
}
}
相關的bean
/**
* @author fangchangtan
* @date 2019-11-28 9:08
*/
public class KafkaTopicBean {
private String topicName; // topic 名稱
private Integer partition; // partition 分區數量
private Integer replication; // replication 副本數量
private String descrbe;
public String getTopicName() {
return topicName;
}
public void setTopicName(String topicName) {
this.topicName = topicName;
}
public Integer getPartition() {
return partition;
}
public void setPartition(Integer partition) {
this.partition = partition;
}
public Integer getReplication() {
return replication;
}
public void setReplication(Integer replication) {
this.replication = replication;
}
public String getDescrbe() {
return descrbe;
}
public void setDescrbe(String descrbe) {
this.descrbe = descrbe;
}
@Override
public String toString() {
return "KafkaTopicBean [topicName=" + topicName + ", partition=" + partition
+ ", replication=" + replication + ", descrbe=" + descrbe +"]";
}
}