实现kafka的管理端操作API步骤如下:
<dependency>
<groupId>org.springframework.kafkagroupId>
<artifactId>spring-kafkaartifactId>
<version>1.1.0version>
dependency>
<dependency>
<groupId>org.apache.kafkagroupId>
<artifactId>kafka_2.12artifactId>
<version>1.1.0version>
dependency>
import com.com.wj.kafkaAPI.conf.KafkaConsumerConfig;
import kafka.admin.AdminClient;
import kafka.admin.AdminUtils;
import kafka.admin.RackAwareMode;
import kafka.cluster.Broker;
import kafka.cluster.Cluster;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.KafkaStream;
import kafka.coordinator.group.GroupOverview;
import kafka.javaapi.consumer.ConsumerConnector;
import kafka.javaapi.consumer.ZookeeperConsumerConnector;
import kafka.server.ConfigType;
import kafka.utils.ZkUtils;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.security.JaasUtils;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Component;
import scala.collection.JavaConversions;
import scala.collection.Map;
import scala.collection.Set;
import java.io.IOException;
import java.util.*;
public class ConsoleApi {
@Autowired
private KafkaConsumerConfig consumerConfig;
private ZkUtils zkUtils = null;
private ZooKeeper zooKeeper = null;
/**
* -获取集群信息(与getAllBrokersInCluster()只是返回类型不一致)
*/
public Cluster getCluster(){
zkUtils = ZkUtils.apply(consumerConfig.getZookeeperConn(), 30000, 30000, JaasUtils.isZkSecurityEnabled());
return zkUtils.getCluster();
}
public void getLeaderAndIsrForPartition(String topicName,int patition){
zkUtils = ZkUtils.apply(consumerConfig.getZookeeperConn(), 30000, 30000, JaasUtils.isZkSecurityEnabled());
System.out.println("打印:"+zkUtils.getLeaderAndIsrForPartition(topicName,patition));
}
public boolean createConsumer(String groupId,String topic) {
try {
Properties properties = new Properties();
properties.put("zookeeper.connect", consumerConfig.getZookeeperConn());//声明zk
properties.put("group.id", groupId);
ConsumerConnector consumer = new ZookeeperConsumerConnector(new ConsumerConfig(properties),true);
java.util.Map topicCountMap = new HashMap();
if (topic!=null &&!"".equals(topic)){
topicCountMap.put(topic, 1); // 一次从主题中获取一个数据
}else {
topicCountMap.put("topic", 1); // 一次从主题中获取一个数据
}
java.util.Mapbyte[], byte[]>>> messageStreams = consumer.createMessageStreams(topicCountMap);
return true;
}catch (RuntimeException e){
return false;
}
}
public boolean createConsumer(String groupId) {
return createConsumer(groupId,null);
}
public boolean deleteUselessConsumer(String group) {
return deleteUselessConsumer("-1", group);
}
/**
* -删除topic路径
* @return
*/
public String deleteTopicsPath(){
zkUtils = ZkUtils.apply(consumerConfig.getZookeeperConn(), 30000, 30000, JaasUtils.isZkSecurityEnabled());
return zkUtils.DeleteTopicsPath();
}
/**
* -根据brokerId获取broker的信息
* @param brokerId
*/
public Broker getBrokerInfo(int brokerId){
zkUtils = ZkUtils.apply(consumerConfig.getZookeeperConn(), 30000, 30000, JaasUtils.isZkSecurityEnabled());
return zkUtils.getBrokerInfo(brokerId).get();
}
/**
* -获取消费者的路径
* @return
*/
public String ConsumersPath(){
zkUtils = ZkUtils.apply(consumerConfig.getZookeeperConn(), 30000, 30000, JaasUtils.isZkSecurityEnabled());
return zkUtils.ConsumersPath();
}
/**
* 删除多个topic
* @param topicNames
* @return
*/
public String[] deleteTopics(final String...topicNames) {
if(topicNames==null || topicNames.length==0) return new String[0];
java.util.Set deleted = new LinkedHashSet();
for(String topicName: topicNames) {
if(topicName!=null || !topicName.trim().isEmpty()) {
deleteTopic(topicName);
deleted.add(topicName.trim());
}
}
return deleted.toArray(new String[deleted.size()]);
}
/**
* 获取所有的TopicList
* @return
*/
public List getTopicList() {
zkUtils = ZkUtils.apply(consumerConfig.getZookeeperConn(), 30000, 30000, JaasUtils.isZkSecurityEnabled());
List allTopicList = JavaConversions.seqAsJavaList(zkUtils.getAllTopics());
return allTopicList;
}
/**
* ~获取某个分组下的所有消费者
* @param groupName
* @return
*/
public List getConsumersInGroup(String groupName) {
zkUtils = ZkUtils.apply(consumerConfig.getZookeeperConn(), 30000, 30000, JaasUtils.isZkSecurityEnabled());
List allTopicList = JavaConversions.seqAsJavaList(zkUtils.getConsumersInGroup(groupName));
return allTopicList;
}
/**
* 判断某个topic是否存在
* @param topicName
* @return
*/
public boolean topicExists(String topicName) {
zkUtils = ZkUtils.apply(consumerConfig.getZookeeperConn(), 30000, 30000, JaasUtils.isZkSecurityEnabled());
boolean exists = AdminUtils.topicExists(zkUtils,topicName);
return exists;
}
/**
* ~
* @param groupName
* @return
*/
public boolean isConsumerGroupActive(String groupName) {
zkUtils = ZkUtils.apply(consumerConfig.getZookeeperConn(), 30000, 30000, JaasUtils.isZkSecurityEnabled());
boolean exists = AdminUtils.isConsumerGroupActive(zkUtils,groupName);
return exists;
}
/**
* 获取所有消费者组
* @return
*/
public List getConsumerGroups() {
zkUtils = ZkUtils.apply(consumerConfig.getZookeeperConn(), 30000, 30000, JaasUtils.isZkSecurityEnabled());
List set = JavaConversions.seqAsJavaList(zkUtils.getConsumerGroups()) ;
return set;
}
/**
* 根据消费者的名称获取topic
* @param groupName
* @return
*/
public List getTopicsByConsumerGroup(String groupName) {
zkUtils = ZkUtils.apply(consumerConfig.getZookeeperConn(), 30000, 30000, JaasUtils.isZkSecurityEnabled());
List set2 = JavaConversions.seqAsJavaList(zkUtils.getTopicsByConsumerGroup(groupName)) ;
return set2;
}
/**
* 获取排序的BrokerList
* @return
*/
public List
其中,使用到的”“consumerConfig.getZookeeperConn()”,可以直接使用所使用的kafka对应的zookeeper地址:比如:localhost:2181.
在开发过程中,遇到了一些坑,其中jar包版本不正确的情况最多(目前我基本采用的是最新的jar包),大家也要多多注意,不要掉进坑里了~
转载请标明出处:https://blog.csdn.net/wangjie919/article/details/80746914 谢谢~
关于kafka的学习,最近也是做了一些研究,欢迎大家与我探讨,共同进步哦~
可以直接留言,也可以发邮件:[email protected]