kafka之五 kafkaAdmin API

 一:kafka官文API中只提供了消费者和生产者的API,但对于创建TOPIC的API没有介绍。项目中需要整合kafka的admin功能时候,就没有参考的方法。通过浏览kafka的源码,你会发现其中的创建topic的代码逻辑;下面就是笔者总结获取的;

二 :创建topic javaAPI实现

    

[java]  view plain  copy
  1. public class KafkaTopicBean {  
  2.   
  3.     // topic name  
  4.     private String topic;  
  5.     // partition num  
  6.     private Integer partition;  
  7.     // replication num  
  8.     private Integer replication;  
  9.     private String descrbe;  
  10.     // 操作类型  
  11.     private Integer operationType;  
  12.   
  13.     public Integer getOperationType() {  
  14.         return operationType;  
  15.     }  
  16.   
  17.     public void setOperationType(Integer operationType) {  
  18.         this.operationType = operationType;  
  19.     }  
  20.   
  21.     public String getTopic() {  
  22.         return topic;  
  23.     }  
  24.   
  25.     public void setTopic(String topic) {  
  26.         this.topic = topic;  
  27.     }  
  28.   
  29.     public Integer getPartition() {  
  30.         return partition;  
  31.     }  
  32.   
  33.     public void setPartition(Integer partition) {  
  34.         this.partition = partition;  
  35.     }  
  36.   
  37.     public Integer getReplication() {  
  38.         return replication;  
  39.     }  
  40.   
  41.     public void setReplication(Integer replication) {  
  42.         this.replication = replication;  
  43.     }  
  44.   
  45.     public String getDescrbe() {  
  46.         return descrbe;  
  47.     }  
  48.   
  49.     public void setDescrbe(String descrbe) {  
  50.         this.descrbe = descrbe;  
  51.     }  
  52.   
  53.     @Override  
  54.     public String toString() {  
  55.         return "KafkaTopicBean [topic=" + topic + ", partition=" + partition  
  56.                 + ", replication=" + replication + ", descrbe=" + descrbe  
  57.                 + ", operationType=" + operationType + "]";  
  58.     }  
  59.   
  60. }  

  <dependency>
           <groupId>org.apache.kafka</groupId>
           <artifactId>kafka_2.10</artifactId>
            <version>0.10.0.1</version>
    </dependency>

    1:实现kafka创建topic

[java]  view plain  copy
  1.   
[java]  view plain  copy
  1.  public static void createKafaTopic() {  
  2.           
  3.         ZkUtils zkUtils = ZkUtils.apply(  
  4.                 "172.30.251.331:2181,172.30.251.341:2181"3000030000,  
  5.                 JaasUtils.isZkSecurityEnabled());  
  6.                 Properties conf = new Properties();  
  7.         AdminUtils.createTopic(zkUtils, bean.getTopic(),  
  8.                         bean.getPartition(), bean.getReplication(),  
  9.                         new Properties(), new RackAwareMode.Enforced$());  
  10. }  
 

 
2: 实现kafka删除topic      
[java]  view plain  copy
  1. private static ResultDTO deleteTopic(ZkUtils zkUtils2, KafkaTopicBean bean) {  
  2.     ResultDTO DAO = null;  
  3.     boolean isCheck = checkDeleteTopic(bean);  
  4.     if (isCheck) {  
  5.             if (Topic.isInternal(bean.getTopic())) {  
  6.                 DAO = new ResultDTO(  
  7.                         Global_Constant.DTO_ERROR_CODE,  
  8.                         "Topic %s is a kafka internal topic and is not allowed to be marked for deletion");  
  9.             } else {  
  10.                 zkUtils.createPersistentPath(  
  11.                         zkUtils.getDeleteTopicPath(bean.getTopic()), null,  
  12.                         zkUtils.DefaultAcls());  
  13.                 DAO = new ResultDTO(Global_Constant.DTO_SUCCESS_CODE,  
  14.                         "success in delete topic");  
  15.             }  
  16.     } else {  
  17.             DAO = new ResultDTO(Global_Constant.DTO_ERROR_CODE,  
  18.                     "delete topic but topic name is empty");  
  19.     }  
  20.     return DAO;  
[java]  view plain  copy
  1. }  
[java]  view plain  copy
  1. private static boolean checkDeleteTopic(KafkaTopicBean bean) {  
  2. <span style="white-space:pre;"> </span>if (null == bean.getTopic() || bean.getTopic().isEmpty()  
  3.                 || "".equals(bean.getTopic().trim())) {  
  4.             return false;  
  5.     } else {  
  6.         Seq<String> topics = zkUtils.getAllTopics();  
  7.         if (topics.contains(bean.getTopic())) {  
  8.             return true;  
  9.         } else {  
  10.             return false;  
  11.         }  
  12.     }  
  13. }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值