本文整理汇总了Java中org.apache.zookeeper.ZooKeeper.getChildren方法的典型用法代码示例。如果您正苦于以下问题:Java ZooKeeper.getChildren方法的具体用法?Java ZooKeeper.getChildren怎么用?Java ZooKeeper.getChildren使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.zookeeper.ZooKeeper
的用法示例。
在下文中一共展示了ZooKeeper.getChildren方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getTree
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public static String[] getTree(ZooKeeper zk,String path) throws Exception{
if(zk.exists(path, false) == null){
return new String[0];
}
List<String> dealList = new ArrayList<String>();
dealList.add(path);
int index =0;
while(index < dealList.size()){
String tempPath = dealList.get(index);
List<String> children = zk.getChildren(tempPath, false);
if(tempPath.equalsIgnoreCase("/") == false){
tempPath = tempPath +"/";
}
Collections.sort(children);
for(int i = children.size() -1;i>=0;i--){
dealList.add(index+1, tempPath + children.get(i));
}
index++;
}
return (String[])dealList.toArray(new String[0]);
}
示例2: testNodeChildrenChanged
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testNodeChildrenChanged() throws Exception {
QuorumUtil qu = new QuorumUtil(1);
qu.startAll();
EventsWatcher watcher = new EventsWatcher();
ZooKeeper zk1 = createClient(qu, 1, watcher);
ZooKeeper zk2 = createClient(qu, 2);
String path = "/test-children-changed";
zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.getChildren(path, watcher);
qu.shutdown(1);
zk2.create(path + "/children-1", new byte[2],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
qu.start(1);
watcher.waitForConnected(TIMEOUT * 1000L);
watcher.assertEvent(TIMEOUT, EventType.NodeChildrenChanged);
qu.shutdownAll();
}
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:24,代码来源:WatchEventWhenAutoResetTest.java
示例3: getTree
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
private static String[] getTree(ZooKeeper zk, String path) throws Exception{
if(zk.exists(path, false) == null){
return new String[0];
}
List<String> dealList = new ArrayList<String>();
dealList.add(path);
int index =0;
while(index < dealList.size()){
String tempPath = dealList.get(index);
List<String> children = zk.getChildren(tempPath, false);
if(!"/".equalsIgnoreCase(tempPath)){
tempPath = tempPath +"/";
}
Collections.sort(children);
for (int i = children.size() - 1; i >= 0; i--) {
dealList.add(index + 1, tempPath + children.get(i));
}
index++;
}
return dealList.toArray(new String[0]);
}
示例4: init
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@PostConstruct
public void init() {
ZooKeeper zkClient = zookeeperConnManager.getZkClient();
try {
/* 如果根节点不存在则创建 */
if (null == zkClient.exists(nodePath, false)) {
zkClient.create(nodePath, new byte[] {}, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
/* 注册节点 */
zkClient.getChildren(nodePath, this);
} catch (KeeperException | InterruptedException e) {
log.error("error", e);
}
}
示例5: delQuota
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* this method deletes quota for a node.
*
* @param zk the zookeeper client
* @param path the path to delete quota for
* @param bytes true if number of bytes needs to be unset
* @param numNodes true if number of nodes needs to be unset
* @return true if quota deletion is successful
* @throws KeeperException
* @throws IOException
* @throws InterruptedException
*/
public static boolean delQuota(ZooKeeper zk, String path,
boolean bytes, boolean numNodes)
throws KeeperException, IOException, InterruptedException, MalformedPathException {
String parentPath = Quotas.quotaZookeeper + path;
String quotaPath = Quotas.quotaZookeeper + path + "/" +
Quotas.limitNode;
if (zk.exists(quotaPath, false) == null) {
System.out.println("Quota does not exist for " + path);
return true;
}
byte[] data = null;
try {
data = zk.getData(quotaPath, false, new Stat());
} catch (IllegalArgumentException ex) {
throw new MalformedPathException(ex.getMessage());
} catch (KeeperException.NoNodeException ne) {
System.err.println("quota does not exist for " + path);
return true;
}
StatsTrack strack = new StatsTrack(new String(data));
if (bytes && !numNodes) {
strack.setBytes(-1L);
zk.setData(quotaPath, strack.toString().getBytes(), -1);
} else if (!bytes && numNodes) {
strack.setCount(-1);
zk.setData(quotaPath, strack.toString().getBytes(), -1);
} else if (bytes && numNodes) {
// delete till you can find a node with more than
// one child
List<String> children = zk.getChildren(parentPath, false);
/// delete the direct children first
for (String child : children) {
zk.delete(parentPath + "/" + child, -1);
}
// cut the tree till their is more than one child
trimProcQuotas(zk, parentPath);
}
return true;
}
示例6: testSequentialNodeData
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testSequentialNodeData() throws Exception {
ZooKeeper zk= null;
String queue_handle = "/queue";
try {
zk = createClient();
zk.create(queue_handle, new byte[0], Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk.create(queue_handle + "/element", "0".getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
zk.create(queue_handle + "/element", "1".getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
List<String> children = zk.getChildren(queue_handle, true);
Assert.assertEquals(children.size(), 2);
String child1 = children.get(0);
String child2 = children.get(1);
int compareResult = child1.compareTo(child2);
Assert.assertNotSame(compareResult, 0);
if (compareResult < 0) {
} else {
String temp = child1;
child1 = child2;
child2 = temp;
}
String child1data = new String(zk.getData(queue_handle
+ "/" + child1, false, null));
String child2data = new String(zk.getData(queue_handle
+ "/" + child2, false, null));
Assert.assertEquals(child1data, "0");
Assert.assertEquals(child2data, "1");
} finally {
if (zk != null) {
zk.close();
}
}
}
示例7: run
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
public void run() throws IOException, InterruptedException, KeeperException {
zk = new ZooKeeper(zkHostPort, sessTimeout, this);
mknod(assignmentsNode, CreateMode.PERSISTENT);
mknod(statusNode, CreateMode.EPHEMERAL);
mknod(reportsNode, CreateMode.PERSISTENT);
// Now we just start watching the assignments directory
zk.getChildren(assignmentsNode, true, this, null);
}
示例8: doVerify
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Override
boolean doVerify() throws Exception {
final ZooKeeper zk = new ZooKeeper(String.format("%s:%d", getHost(), getPort()), 3000,
watchedEvent -> LOGGER.trace("got event"));
final List<String> children = zk.getChildren("/brokers/topics", false);
zk.close();
for (final String child : children) {
// checkpoint topic is created if the samza job is up and running
// checkpointing must be enabled for the samza job
if (child.contains("checkpoint")) {
return true;
}
}
return false;
}
示例9: testNodeDeleted
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testNodeDeleted() throws Exception {
QuorumUtil qu = new QuorumUtil(1);
qu.startAll();
EventsWatcher watcher = new EventsWatcher();
ZooKeeper zk1 = createClient(qu, 1, watcher);
ZooKeeper zk2 = createClient(qu, 2);
String path = "/test-deleted";
zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.getData(path, watcher, null);
qu.shutdown(1);
zk2.delete(path, -1);
qu.start(1);
watcher.waitForConnected(TIMEOUT * 1000L);
watcher.assertEvent(TIMEOUT, EventType.NodeDeleted);
zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.exists(path, watcher);
qu.shutdown(1);
zk2.delete(path, -1);
qu.start(1);
watcher.waitForConnected(TIMEOUT * 1000L);
watcher.assertEvent(TIMEOUT, EventType.NodeDeleted);
zk1.create(path, new byte[1], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.getChildren(path, watcher);
qu.shutdown(1);
zk2.delete(path, -1);
qu.start(1);
watcher.waitForConnected(TIMEOUT * 1000L);
watcher.assertEvent(TIMEOUT, EventType.NodeDeleted);
qu.shutdownAll();
}
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:41,代码来源:WatchEventWhenAutoResetTest.java
示例10: testUpgrade
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* test the upgrade
* @throws Exception
*/
@Test
public void testUpgrade() throws Exception {
File upgradeDir = new File(testData, "upgrade");
UpgradeMain upgrade = new UpgradeMain(upgradeDir, upgradeDir);
upgrade.runUpgrade();
ZooKeeperServer zks = new ZooKeeperServer(upgradeDir, upgradeDir, 3000);
SyncRequestProcessor.setSnapCount(1000);
final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
f.startup(zks);
LOG.info("starting up the zookeeper server .. waiting");
Assert.assertTrue("waiting for server being up",
ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this);
Stat stat = zk.exists("/", false);
List<String> children = zk.getChildren("/", false);
Collections.sort(children);
for (int i = 0; i < 10; i++) {
Assert.assertTrue("data tree sanity check",
("test-" + i).equals(children.get(i)));
}
//try creating one node
zk.create("/upgrade", "upgrade".getBytes(), Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
// check if its there
if (zk.exists("/upgrade", false) == null) {
Assert.assertTrue(false);
}
zk.close();
// bring down the server
f.shutdown();
Assert.assertTrue("waiting for server down",
ClientBase.waitForServerDown(HOSTPORT,
ClientBase.CONNECTION_TIMEOUT));
}
示例11: checkBookiesUp
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
/**
* Check that a number of bookies are available
* @param count number of bookies required
* @param timeout number of seconds to wait for bookies to start
* @throws IOException if bookies are not started by the time the timeout hits
*/
int checkBookiesUp(int count, int timeout) throws Exception {
ZooKeeper zkc = connectZooKeeper();
try {
int mostRecentSize = 0;
for (int i = 0; i < timeout; i++) {
try {
List<String> children = zkc.getChildren("/ledgers/available",
false);
mostRecentSize = children.size();
// Skip 'readonly znode' which is used for keeping R-O bookie details
if (children.contains("readonly")) {
mostRecentSize = children.size() - 1;
}
if (LOG.isDebugEnabled()) {
LOG.debug("Found " + mostRecentSize + " bookies up, "
+ "waiting for " + count);
if (LOG.isTraceEnabled()) {
for (String child : children) {
LOG.trace(" server: " + child);
}
}
}
if (mostRecentSize == count) {
break;
}
} catch (KeeperException e) {
// ignore
}
Thread.sleep(1000);
}
return mostRecentSize;
} finally {
zkc.close();
}
}
示例12: utestChildren
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
private void utestChildren(int port)
throws IOException, InterruptedException, KeeperException
{
ZooKeeper zk =
new ZooKeeper("127.0.0.1:" + port, CONNECTION_TIMEOUT, this);
for (int i = 0; i < 10000; i++) {
zk.getChildren("/" + i, true);
}
zk.close();
}
示例13: waitInLine
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
static String waitInLine(ZooKeeper zookeeper, String lockNode,
String placeInLine) throws KeeperException, InterruptedException {
List<String> children = zookeeper.getChildren(lockNode, false);
Collections.sort(children);
if (children.size() == 0) {
logger.warn("getChildren() returned empty list, but we created a ticket.");
return acquireLock(zookeeper, lockNode);
}
boolean lockingTicketExists = children.get(0).equals(LOCKING_TICKET);
if (lockingTicketExists) {
children.remove(0);
}
int positionInQueue = -1;
int i = 0;
for (String child : children) {
if (child.equals(placeInLine)) {
positionInQueue = i;
break;
}
i++;
}
if (positionInQueue < 0) {
throw new RuntimeException(
"Created node (" + placeInLine + ") not found in getChildren().");
}
String placeBeforeUs;
if (positionInQueue == 0) {
if (grabTicket(zookeeper, lockNode, LOCKING_TICKET)) {
releaseTicket(zookeeper, lockNode, placeInLine);
return LOCKING_TICKET;
} else {
placeBeforeUs = LOCKING_TICKET;
}
} else {
placeBeforeUs = children.get(positionInQueue - 1);
}
final CountDownLatch latch = new CountDownLatch(1);
Stat stat = zookeeper.exists(lockNode + "/" + placeBeforeUs, event -> {
latch.countDown();
});
if (stat != null) {
logger.debug("Watching place in queue before us ({})", placeBeforeUs);
latch.await();
}
return waitInLine(zookeeper, lockNode, placeInLine);
}
示例14: testDeepChildWatcherAutoResetWithChroot
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testDeepChildWatcherAutoResetWithChroot() throws Exception {
ZooKeeper zk1 = createClient();
zk1.create("/ch1", null, Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.create("/ch1/here", null, Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.create("/ch1/here/we", null, Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zk1.create("/ch1/here/we/are", null, Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
MyWatcher watcher = new MyWatcher();
ZooKeeper zk2 = createClient(watcher, hostPort + "/ch1/here/we");
zk2.getChildren("/are", true );
// this should trigger the watch
zk1.create("/ch1/here/we/are/now", null, Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
WatchedEvent e = watcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertNotNull(e);
Assert.assertEquals(EventType.NodeChildrenChanged, e.getType());
Assert.assertEquals("/are", e.getPath());
MyWatcher childWatcher = new MyWatcher();
zk2.getChildren("/are", childWatcher);
stopServer();
watcher.waitForDisconnected(3000);
startServer();
watcher.waitForConnected(3000);
// this should trigger the watch
zk1.create("/ch1/here/we/are/again", null, Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
e = childWatcher.events.poll(TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertNotNull(e);
Assert.assertEquals(EventType.NodeChildrenChanged, e.getType());
Assert.assertEquals("/are", e.getPath());
}
示例15: testSequentialNodeNames
import org.apache.zookeeper.ZooKeeper; //导入方法依赖的package包/类
@Test
public void testSequentialNodeNames()
throws IOException, InterruptedException, KeeperException
{
String path = "/SEQUENCE";
String file = "TEST";
String filepath = path + "/" + file;
ZooKeeper zk = null;
try {
zk = createClient();
zk.create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
List<String> children = zk.getChildren(path, false);
Assert.assertEquals(1, children.size());
Assert.assertEquals(file + "0000000000", children.get(0));
zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
children = zk.getChildren(path, false);
Assert.assertEquals(2, children.size());
Assert.assertTrue("contains child 1", children.contains(file + "0000000001"));
zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
children = zk.getChildren(path, false);
Assert.assertEquals(3, children.size());
Assert.assertTrue("contains child 2",
children.contains(file + "0000000002"));
// The pattern is holding so far. Let's run the counter a bit
// to be sure it continues to spit out the correct answer
for(int i = children.size(); i < 105; i++)
zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
children = zk.getChildren(path, false);
Assert.assertTrue("contains child 104",
children.contains(file + "0000000104"));
}
finally {
if(zk != null)
zk.close();
}
}