private ZNRecord readZkChild(String zkPath, ZkClient zkClient) { ZNRecord result = null; // read data and stat Stat stat = new Stat(); ZNRecord data = zkClient.readDataAndStat(zkPath, stat, true); if (data != null) { result = data; } else { result = new ZNRecord(""); } // read childrenList List<String> children = zkClient.getChildren(zkPath); if (children != null && children.size() > 0) { result.setSimpleField("numChildren", "" + children.size()); result.setListField("childrenList", children); } else { result.setSimpleField("numChildren", "" + 0); } return result; }
public static List<ZNRecord> getChildren(ZkClient client, String path) { // parent watch will be set by zkClient List<String> children = client.getChildren(path); if (children == null || children.size() == 0) { return Collections.emptyList(); } List<ZNRecord> childRecords = new ArrayList<ZNRecord>(); for (String child : children) { String childPath = path + "/" + child; Stat newStat = new Stat(); ZNRecord record = client.readDataAndStat(childPath, newStat, true); if (record != null) { record.setVersion(newStat.getVersion()); record.setCreationTime(newStat.getCtime()); record.setModifiedTime(newStat.getMtime()); childRecords.add(record); } } return childRecords; }
private ZNRecord readZkDataStatAndChild(String zkPath, ZkClient zkClient) { ZNRecord result = null; // read data and stat Stat stat = new Stat(); ZNRecord data = zkClient.readDataAndStat(zkPath, stat, true); if (data != null) { result = data; } else { result = new ZNRecord(""); } result.setSimpleField("zkPath", zkPath); result.setSimpleField("stat", stat.toString()); result.setSimpleField("numChildren", "" + stat.getNumChildren()); result.setSimpleField("ctime", "" + new Date(stat.getCtime())); result.setSimpleField("mtime", "" + new Date(stat.getMtime())); result.setSimpleField("dataLength", "" + stat.getDataLength()); // read childrenList List<String> children = zkClient.getChildren(zkPath); if (children != null && children.size() > 0) { result.setListField("children", children); } return result; }
Assert.assertEquals((long) beanServer.getAttribute(idealStatename, "ReadBytesCounter"), TEST_DATA_SIZE); zkClient.readDataAndStat(TEST_PATH, new Stat(), true); Assert.assertEquals((long) beanServer.getAttribute(rootname, "ReadCounter"), 5);
try { Stat stat = new Stat(); ZNRecord record = zkClient.<ZNRecord> readDataAndStat(path, stat, true);