前文
pom.xml文件增加依赖
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.7.0</version>
</dependency>
新建java文件:ZookeeperTest
public class ZookeeperTest {
public static void main(String[] args) throws Exception {
ZooKeeper zk = new ZooKeeper(
"192.168.142.10:2181," +
"192.168.142.20:2181," +
"192.168.142.30:2181", 300000, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println("事件类型为:" + watchedEvent.getType());
System.out.println("事件发生的路径:" + watchedEvent.getPath());
System.out.println("通知状态为:" + watchedEvent.getState());
}
});
zk.create("/testRootPath", "testRootData".getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zk.create("/testRootPath/testChildPathOne", "testChildPathOne".getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("testRootData节点数据为:" +
new String(zk.getData("/testRootPath", false, null)));
System.out.println(zk.getChildren("/testRootPath", true));
zk.setData("/testRootPath/testChildPathOne", "modifyChildDataOne".getBytes(), -1);
System.out.println("目录节点状态[" + zk.exists("/testRootPath", true) + "]");
zk.delete("/testRootPath/testChildPathOne", -1);
zk.delete("/testRootPath", -1);
zk.close();
}
}
GitHub文件下载
HadoopDemo