Java Code Examples for kafka.zk.KafkaZkClient#pathExists()
The following examples show how to use
kafka.zk.KafkaZkClient#pathExists() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KafkaMetricsServiceImpl.java From kafka-eagle with Apache License 2.0 | 6 votes |
private String describeTopicConfig(String clusterAlias, String topic) { String target = ""; try { KafkaZkClient kafkaZkCli = kafkaZKPool.getZkClient(clusterAlias); if (kafkaZkCli.pathExists(CONFIG_TOPIC_PATH + topic)) { Tuple2<Option<byte[]>, Stat> tuple = kafkaZkCli.getDataAndStat(CONFIG_TOPIC_PATH + topic); target = new String(tuple._1.get()); } if (kafkaZkCli != null) { kafkaZKPool.release(clusterAlias, kafkaZkCli); kafkaZkCli = null; } } catch (Exception e) { e.printStackTrace(); LOG.error("Describe topic[" + topic + "] config has error, msg is " + e.getMessage()); } return target; }
Example 2
Source File: BrokerServiceImpl.java From kafka-eagle with Apache License 2.0 | 6 votes |
/** Statistics topic partitions total used as page. */ public long partitionNumbers(String clusterAlias, String topic) { long count = 0L; if (Kafka.CONSUMER_OFFSET_TOPIC.equals(topic)) { return count; } KafkaZkClient zkc = kafkaZKPool.getZkClient(clusterAlias); try { if (zkc.pathExists(BROKER_TOPICS_PATH + "/" + topic + "/partitions")) { Seq<String> subBrokerTopicsPaths = zkc.getChildren(BROKER_TOPICS_PATH + "/" + topic + "/partitions"); count = JavaConversions.seqAsJavaList(subBrokerTopicsPaths).size(); } } catch (Exception e) { LOG.error("Get topic partition numbers has error, msg is " + e.getCause().getMessage()); e.printStackTrace(); } if (zkc != null) { kafkaZKPool.release(clusterAlias, zkc); zkc = null; } return count; }
Example 3
Source File: BrokerServiceImpl.java From kafka-eagle with Apache License 2.0 | 6 votes |
/** Get kafka broker numbers from zookeeper. */ public long brokerNumbers(String clusterAlias) { long count = 0; KafkaZkClient zkc = kafkaZKPool.getZkClient(clusterAlias); try { if (zkc.pathExists(BROKER_IDS_PATH)) { Seq<String> subBrokerIdsPaths = zkc.getChildren(BROKER_IDS_PATH); count = JavaConversions.seqAsJavaList(subBrokerIdsPaths).size(); } } catch (Exception e) { LOG.error("Get kafka broker numbers has error, msg is " + e.getCause().getMessage()); e.printStackTrace(); } if (zkc != null) { kafkaZKPool.release(clusterAlias, zkc); zkc = null; } return count; }
Example 4
Source File: BrokerServiceImpl.java From kafka-eagle with Apache License 2.0 | 6 votes |
/** Get topic list {@link #topicList()} include cgroups from zookeeper. */ public List<String> topicList(String clusterAlias) { List<String> topics = new ArrayList<>(); if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.sasl.cgroup.enable")) { topics = SystemConfigUtils.getPropertyArrayList(clusterAlias + ".kafka.eagle.sasl.cgroup.topics", ","); } else if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.ssl.cgroup.enable")) { topics = SystemConfigUtils.getPropertyArrayList(clusterAlias + ".kafka.eagle.ssl.cgroup.topics", ","); } else { KafkaZkClient zkc = kafkaZKPool.getZkClient(clusterAlias); try { if (zkc.pathExists(BROKER_TOPICS_PATH)) { Seq<String> subBrokerTopicsPaths = zkc.getChildren(BROKER_TOPICS_PATH); topics = JavaConversions.seqAsJavaList(subBrokerTopicsPaths); excludeTopic(topics); } } catch (Exception e) { LOG.error("Get topic list has error, msg is " + e.getCause().getMessage()); e.printStackTrace(); } if (zkc != null) { kafkaZKPool.release(clusterAlias, zkc); zkc = null; } } return topics; }
Example 5
Source File: BrokerServiceImpl.java From kafka-eagle with Apache License 2.0 | 6 votes |
/** Get broker id list. */ public List<Object> getBrokerIdList(String clusterAlias) { List<Object> brokerIds = new ArrayList<>(); KafkaZkClient zkc = kafkaZKPool.getZkClient(clusterAlias); try { if (zkc.pathExists(BROKER_IDS_PATH)) { Seq<String> subBrokerIdsPaths = zkc.getChildren(BROKER_IDS_PATH); for (String id : JavaConversions.seqAsJavaList(subBrokerIdsPaths)) { brokerIds.add(Integer.parseInt(id)); } } } catch (Exception e) { LOG.error("Get kafka broker id has error, msg is ", e); e.printStackTrace(); } if (zkc != null) { kafkaZKPool.release(clusterAlias, zkc); zkc = null; } return brokerIds; }
Example 6
Source File: ZkServiceImpl.java From kafka-eagle with Apache License 2.0 | 6 votes |
/** Zookeeper delete command. */ public String delete(String clusterAlias, String cmd) { String ret = ""; KafkaZkClient zkc = kafkaZKPool.getZkClient(clusterAlias); boolean status = zkc.pathExists(cmd); if (status) { if (zkc.deleteRecursive(cmd)) { ret = "[" + cmd + "] has delete success"; } else { ret = "[" + cmd + "] has delete failed"; } } if (zkc != null) { kafkaZKPool.release(clusterAlias, zkc); zkc = null; } return ret; }
Example 7
Source File: ZkServiceImpl.java From kafka-eagle with Apache License 2.0 | 6 votes |
/** Zookeeper get command. */ public String get(String clusterAlias, String cmd) { String ret = ""; KafkaZkClient zkc = kafkaZKPool.getZkClient(clusterAlias); if (zkc.pathExists(cmd)) { Tuple2<Option<byte[]>, Stat> tuple2 = zkc.getDataAndStat(cmd); ret += new String(tuple2._1.get()) + "\n"; ret += "cZxid = " + tuple2._2.getCzxid() + "\n"; ret += "ctime = " + tuple2._2.getCtime() + "\n"; ret += "mZxid = " + tuple2._2.getMzxid() + "\n"; ret += "mtime = " + tuple2._2.getMtime() + "\n"; ret += "pZxid = " + tuple2._2.getPzxid() + "\n"; ret += "cversion = " + tuple2._2.getCversion() + "\n"; ret += "dataVersion = " + tuple2._2.getVersion() + "\n"; ret += "aclVersion = " + tuple2._2.getAversion() + "\n"; ret += "ephemeralOwner = " + tuple2._2.getEphemeralOwner() + "\n"; ret += "dataLength = " + tuple2._2.getDataLength() + "\n"; ret += "numChildren = " + tuple2._2.getNumChildren() + "\n"; } if (zkc != null) { kafkaZKPool.release(clusterAlias, zkc); zkc = null; } return ret; }
Example 8
Source File: ZkServiceImpl.java From kafka-eagle with Apache License 2.0 | 5 votes |
/** Zookeeper ls command. */ public String ls(String clusterAlias, String cmd) { String target = ""; KafkaZkClient zkc = kafkaZKPool.getZkClient(clusterAlias); boolean status = zkc.pathExists(cmd); if (status) { Seq<String> seq = zkc.getChildren(cmd); target = JavaConversions.seqAsJavaList(seq).toString(); } if (zkc != null) { kafkaZKPool.release(clusterAlias, zkc); zkc = null; } return target; }
Example 9
Source File: KafkaServiceImpl.java From kafka-eagle with Apache License 2.0 | 5 votes |
/** * Find topic and group exist in zookeeper. * * @param topic * Filter topic. * @param group * Filter group * @return Boolean. */ public boolean findTopicAndGroupExist(String clusterAlias, String topic, String group) { KafkaZkClient zkc = kafkaZKPool.getZkClient(clusterAlias); String ownersPath = CONSUMERS_PATH + "/" + group + "/owners/" + topic; boolean status = zkc.pathExists(ownersPath); if (zkc != null) { kafkaZKPool.release(clusterAlias, zkc); zkc = null; } return status; }
Example 10
Source File: KafkaServiceImpl.java From kafka-eagle with Apache License 2.0 | 5 votes |
/** * Obtaining kafka consumer information from zookeeper. */ public Map<String, List<String>> getConsumers(String clusterAlias) { KafkaZkClient zkc = kafkaZKPool.getZkClient(clusterAlias); Map<String, List<String>> consumers = new HashMap<String, List<String>>(); try { Seq<String> subConsumerPaths = zkc.getChildren(CONSUMERS_PATH); List<String> groups = JavaConversions.seqAsJavaList(subConsumerPaths); for (String group : groups) { String path = CONSUMERS_PATH + "/" + group + "/owners"; if (zkc.pathExists(path)) { Seq<String> owners = zkc.getChildren(path); List<String> ownersSerialize = JavaConversions.seqAsJavaList(owners); consumers.put(group, ownersSerialize); } else { LOG.error("Consumer Path[" + path + "] is not exist."); } } } catch (Exception ex) { LOG.error(ex.getMessage()); } finally { if (zkc != null) { kafkaZKPool.release(clusterAlias, zkc); zkc = null; } } return consumers; }
Example 11
Source File: KafkaServiceImpl.java From kafka-eagle with Apache License 2.0 | 5 votes |
/** * Get kafka 0.10.x sasl topic metadata. */ public List<MetadataInfo> findKafkaLeader(String clusterAlias, String topic) { List<MetadataInfo> targets = new ArrayList<>(); KafkaZkClient zkc = kafkaZKPool.getZkClient(clusterAlias); if (zkc.pathExists(BROKER_TOPICS_PATH + "/" + topic)) { Tuple2<Option<byte[]>, Stat> tuple = zkc.getDataAndStat(BROKER_TOPICS_PATH + "/" + topic); String tupleString = new String(tuple._1.get()); JSONObject partitionObject = JSON.parseObject(tupleString).getJSONObject("partitions"); for (String partition : partitionObject.keySet()) { String path = String.format(TOPIC_ISR, topic, Integer.valueOf(partition)); Tuple2<Option<byte[]>, Stat> tuple2 = zkc.getDataAndStat(path); String tupleString2 = new String(tuple2._1.get()); JSONObject topicMetadata = JSON.parseObject(tupleString2); MetadataInfo metadate = new MetadataInfo(); try { metadate.setLeader(topicMetadata.getInteger("leader")); } catch (Exception e) { LOG.error("Parse string brokerid to int has error, brokerid[" + topicMetadata.getString("leader") + "]"); e.printStackTrace(); } metadate.setPartitionId(Integer.valueOf(partition)); targets.add(metadate); } } if (zkc != null) { kafkaZKPool.release(clusterAlias, zkc); zkc = null; } return targets; }
Example 12
Source File: KafkaServiceImpl.java From kafka-eagle with Apache License 2.0 | 5 votes |
/** * Get broker host and jmx_port info from ids. */ public String getBrokerJMXFromIds(String clusterAlias, int ids) { String jni = ""; KafkaZkClient zkc = kafkaZKPool.getZkClient(clusterAlias); if (zkc.pathExists(BROKER_IDS_PATH)) { try { Tuple2<Option<byte[]>, Stat> tuple = zkc.getDataAndStat(BROKER_IDS_PATH + "/" + ids); String tupleString = new String(tuple._1.get()); String host = ""; if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.sasl.enable") || SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.ssl.enable")) { String endpoints = JSON.parseObject(tupleString).getString("endpoints"); String tmp = endpoints.split("//")[1]; host = tmp.substring(0, tmp.length() - 2).split(":")[0]; } else { host = JSON.parseObject(tupleString).getString("host"); } int jmxPort = JSON.parseObject(tupleString).getInteger("jmx_port"); jni = host + ":" + jmxPort; } catch (Exception ex) { LOG.error("Get broker from ids has error, msg is " + ex.getCause().getMessage()); ex.printStackTrace(); } } if (zkc != null) { kafkaZKPool.release(clusterAlias, zkc); zkc = null; } return jni; }
Example 13
Source File: KafkaServiceImpl.java From kafka-eagle with Apache License 2.0 | 4 votes |
/** * Get all broker list from zookeeper. */ public List<BrokersInfo> getAllBrokersInfo(String clusterAlias) { KafkaZkClient zkc = kafkaZKPool.getZkClient(clusterAlias); List<BrokersInfo> targets = new ArrayList<BrokersInfo>(); if (zkc.pathExists(BROKER_IDS_PATH)) { Seq<String> subBrokerIdsPaths = zkc.getChildren(BROKER_IDS_PATH); List<String> brokerIdss = JavaConversions.seqAsJavaList(subBrokerIdsPaths); int id = 0; for (String ids : brokerIdss) { try { Tuple2<Option<byte[]>, Stat> tuple = zkc.getDataAndStat(BROKER_IDS_PATH + "/" + ids); BrokersInfo broker = new BrokersInfo(); broker.setCreated(CalendarUtils.convertUnixTime2Date(tuple._2.getCtime())); broker.setModify(CalendarUtils.convertUnixTime2Date(tuple._2.getMtime())); String tupleString = new String(tuple._1.get()); if (SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.sasl.enable") || SystemConfigUtils.getBooleanProperty(clusterAlias + ".kafka.eagle.ssl.enable")) { String endpoints = JSON.parseObject(tupleString).getString("endpoints"); String tmp = endpoints.split("//")[1]; broker.setHost(tmp.substring(0, tmp.length() - 2).split(":")[0]); broker.setPort(Integer.valueOf(tmp.substring(0, tmp.length() - 2).split(":")[1])); } else { String host = JSON.parseObject(tupleString).getString("host"); int port = JSON.parseObject(tupleString).getInteger("port"); broker.setHost(host); broker.setPort(port); } broker.setJmxPort(JSON.parseObject(tupleString).getInteger("jmx_port")); broker.setId(++id); broker.setIds(ids); targets.add(broker); } catch (Exception ex) { LOG.error(ex.getMessage()); } } } if (zkc != null) { kafkaZKPool.release(clusterAlias, zkc); zkc = null; } return targets; }
Example 14
Source File: KafkaServiceImpl.java From kafka-eagle with Apache License 2.0 | 4 votes |
/** * According to group, topic and partition to get offset from zookeeper. * * @param topic * Filter topic. * @param group * Filter group. * @param partition * Filter partition. * @return OffsetZkInfo. * @see org.smartloli.kafka.eagle.common.protocol.OffsetZkInfo */ public OffsetZkInfo getOffset(String clusterAlias, String topic, String group, int partition) { KafkaZkClient zkc = kafkaZKPool.getZkClient(clusterAlias); OffsetZkInfo offsetZk = new OffsetZkInfo(); String offsetPath = CONSUMERS_PATH + "/" + group + "/offsets/" + topic + "/" + partition; String ownersPath = CONSUMERS_PATH + "/" + group + "/owners/" + topic + "/" + partition; Tuple2<Option<byte[]>, Stat> tuple = null; try { if (zkc.pathExists(offsetPath)) { tuple = zkc.getDataAndStat(offsetPath); } else { LOG.info("Partition[" + partition + "],OffsetPath[" + offsetPath + "] is not exist!"); if (zkc != null) { kafkaZKPool.release(clusterAlias, zkc); zkc = null; } return offsetZk; } } catch (Exception ex) { LOG.error("Partition[" + partition + "],get offset has error,msg is " + ex.getMessage()); if (zkc != null) { kafkaZKPool.release(clusterAlias, zkc); zkc = null; } return offsetZk; } String tupleString = new String(tuple._1.get()); long offsetSize = Long.parseLong(tupleString); if (zkc.pathExists(ownersPath)) { Tuple2<Option<byte[]>, Stat> tuple2 = zkc.getDataAndStat(ownersPath); String tupleString2 = new String(tuple2._1.get()); offsetZk.setOwners(tupleString2 == null ? "" : tupleString2); } else { offsetZk.setOwners(""); } offsetZk.setOffset(offsetSize); offsetZk.setCreate(CalendarUtils.convertUnixTime2Date(tuple._2.getCtime())); offsetZk.setModify(CalendarUtils.convertUnixTime2Date(tuple._2.getMtime())); if (zkc != null) { kafkaZKPool.release(clusterAlias, zkc); zkc = null; } return offsetZk; }