
Zookeeper::getChildren() 메서드는 ZooKeeper API의 일부로, 지정된 노드의 자식 노드를 가져올 수 있습니다. 이 메서드는 두 개의 파라미터를 요구합니다:
- 첫 번째 파라미터는 노드의 경로입니다.
- 두 번째 파라미터는 watcher 인스턴스입니다.
이 메서드는 List 타입의 반환값을 가집니다. 이 반환값은 노드의 자식 노드의 이름을 포함하는 리스트입니다.
노드가 없을 때, 메서드는 빈 리스트를 반환합니다.
예시 코드는 다음과 같습니다:
#hostingforum.kr
java
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
public class ZookeeperExample {
public static void main(String[] args) throws Exception {
// ZooKeeper 인스턴스 생성
ZooKeeper zk = new ZooKeeper("localhost:2181", 10000, null);
// 노드의 자식 노드 가져오기
List children = zk.getChildren("/node", false);
// 반환값 출력
System.out.println(children);
}
}
위 코드는 ZooKeeper 인스턴스를 생성하고, "/node" 노드의 자식 노드를 가져옵니다. 반환값은 List 타입의 children 변수에 저장됩니다.
노드가 없을 때, 메서드는 빈 리스트를 반환하므로, 다음과 같이 코드를 수정하여 노드가 없을 때의 동작을 테스트할 수 있습니다:
#hostingforum.kr
java
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
public class ZookeeperExample {
public static void main(String[] args) throws Exception {
// ZooKeeper 인스턴스 생성
ZooKeeper zk = new ZooKeeper("localhost:2181", 10000, null);
// 노드의 자식 노드 가져오기
List children = zk.getChildren("/non-existent-node", false);
// 반환값 출력
System.out.println(children);
}
}
위 코드는 "/non-existent-node" 노드의 자식 노드를 가져옵니다. 반환값은 빈 리스트를 출력합니다.
2025-06-13 16:41