【题目来自头歌enducoder平台,有帮助的话,不忘点个赞、关注哟!】
import java.io.IOException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
public class test1 {
// 初始化 ZooKeeper 实例
private ZooKeeper zk;
// 实现一个 Watcher
// 请在此处编写代码
/********* Begin *********/
private Watcher wh = new Watcher() {
// 重写 process 方法
public void process(WatchedEvent event) {
System.out.println(event);
}
};
/********* End *********/
// 连接 ZooKeeper 服务器
private void createZKInstance() throws IOException {
// 请在此处编写代码
/********* Begin *********/
zk = new ZooKeeper("localhost:2181", 15000, this.wh);
/********* End *********/
}
// 断开连接
private void ZKClose() throws InterruptedException {
// 请在此处编写代码
/********* Begin *********/
zk.close();
/********* End *********/
}
public static void main(String[] args) throws IOException, InterruptedException{
test1 test = new test1();
// 连接 ZooKeeper 服务器
test.createZKInstance();
// 断开连接
test.ZKClose();
}
}
import org.apache.zookeeper.*;
import java.io.IOException;
public class Student {
// 初始化 ZooKeeper 实例
private ZooKeeper zk;
// 实现一个 Watcher
private Watcher wh = new Watcher() {
// 重写 process 方法
public void process(WatchedEvent event) {
System.out.println(event);
}
};
// 连接ZooKeeper服务器
public ZooKeeper createZKInstance() throws IOException {
zk = new ZooKeeper("localhost:2181", 15000, this.wh);
return zk;
}
// 使用同步方式删除节点 /mynode1
public void deleteNode() throws KeeperException, InterruptedException {
// 请在此处编写代码
/********* Begin *********/
zk.delete("/mynode1",-1);
/********* End *********/
}
// 使用异步方式创建节点 /mynode1
public void createNodeSync() {
// 请在此处编写代码
/********* Begin *********/
String path = "/mynode1";
zk.create(path, "This node is mine!".getBytes(), ZooDefs.Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT,new test.MyStringCallback(),"CreateMynode!" );
/********* End *********/
}
}
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
public class Student {
// 初始化 ZooKeeper 实例
private ZooKeeper zk;
// 实现一个 Watcher
private Watcher wh = new Watcher() {
// 重写 process 方法
public void process(WatchedEvent event) {
System.out.println(event);
}
};
// 连接ZooKeeper服务器
public ZooKeeper createZKInstance() throws IOException {
zk = new ZooKeeper("localhost:2181", 15000, this.wh);
return zk;
}
// 同步获取节点数据
public void getData() throws IOException, KeeperException, InterruptedException {
// 请在此处编写代码
/********* Begin *********/
Stat stat = new Stat();
byte[] data = zk.getData("/myzk", wh, stat);
System.out.println(new String(data));
/********* End *********/
}
}
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
public class Student {
// 初始化 ZooKeeper 实例
private ZooKeeper zk;
// 实现一个 Watcher
private Watcher wh = new Watcher() {
// 重写 process 方法
public void process(WatchedEvent event) {
System.out.println(event);
}
};
// 连接ZooKeeper服务器
public ZooKeeper createZKInstance() throws IOException {
zk = new ZooKeeper("localhost:2181", 15000, this.wh);
return zk;
}
// 异步删除节点
public void deleteNode() throws IOException, KeeperException, InterruptedException {
// 请在此处编写代码
/********* Begin *********/
Stat stat = new Stat();
zk.delete("/mycallnode",stat.getVersion(),new MyCallback(),"delete");
/********* End *********/
}
// 请在此处编写代码,完成回调函数的编写
/********* Begin *********/
public static class MyCallback implements AsyncCallback.VoidCallback{
// 重写 processResult 方法
@Override
public void processResult(int i, String s, Object o) {
// 输出上下文信息
System.out.println(o);
}
}
/********* End *********/
}