您的位置:首页 > 其它

zookeeper客户端读取子节点的信息

2017-10-23 16:18 459 查看
在zookeeper目录下启动zookeeper客户端:./zkCli.sh(在bin目录下)



用 ls /命令来显示该节点下所有的子节点,get /MsgBusMonitor显示该节点下的所有详细信息。



Object[][] result = null;

//连接服务器的IP地址和端口号
String connectString = "10.0.0.100:2181";
//超时时间
int sessionTimeout = 5000;

try {
zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
//不做操作
}
});
// 获取任务实例example,task_schedule是子节点
List<String> example = zk.getChildren("/task_schedule", true);
List<String> list = null;
int number = 0;int allCount = 0;
for (int j = 0; j < example.size(); j++) {
List<String> runNode = zk.getChildren("/task_schedule/" + example.get(j), true);
boolean numberFlag = true;
if (runNode != null || !runNode.equals("")) {// 节点存在的时候
for (int i = 0; i < runNode.size(); i++) {
number++;
numberFlag = false;
}
}
if(numberFlag==true){
allCount++;
}
}
result = new Object[number+allCount][3
4000
];
int a = 0;
for (int j = 0; j < example.size(); j++) {
boolean flag = true;
// 获取运行节点runNode
List<String> runNode = zk.getChildren("/task_schedule/" + example.get(j), true);

if (runNode != null || !runNode.equals("")) {// 节点存在的时候
for (int i = 0; i < runNode.size(); i++) {
byte[] data = zk.getData("/task_schedule/" + example.get(j) + "/" + runNode.get(i), null,
null);
result[a][0] = example.get(j);
result[a][1] = runNode.get(i);
if(Character.isDigit(new String(data).charAt(0))){
result[a][2] = new String(data);
}else{
result[a][2] = "任务获取失败";
}

a++;
flag = false;
}
}
if (flag == true) {
result[a][0] = example.get(j);
result[a][1] = "";
result[a][2] = "";
a++;
}
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: