java批量创建删除Kafka的topic

一、前言

参考资料:Kafka 0.11客户端集群管理工具AdminClient

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 +"]";
    }

}