java批量创建删除Kafka的topic

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值