package com.bw.ZK;
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
public class ZkText01 {
static String zkUrl="linux04:2181";
public static void main(String[] args) {
//创建连接对象 连接对象 连接地址 监听
try {
ZooKeeper zk = new ZooKeeper(zkUrl,5000, new Watcher() {
//客户端操作
@Override
public void process(WatchedEvent arg0) {
}
});
//创建文件 /参数 1.path 2.内容 (二进制数组) 3. 访问控制列表(这里使用完全开放列表) 4.文件性质
zk.create("/beijing01","hello word".getBytes(),Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
} catch ( Exception e) {
e.printStackTrace();
}
}
}
package com.bw.ZK;
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
public class ZkText2 {
static String zkUrl="linux04:2181";
public static void main(String[] args) {
//创建连接对象 连接对象 连接地址 监听
try {
ZooKeeper zk = new ZooKeeper(zkUrl,5000, new Watcher() {
//客户端操作
@Override
public void process(WatchedEvent arg0) {
}
});
//创建文件 /参数 1.path 2.内容 (二进制数组) 3. 访问控制列表(这里使用完全开放列表) 4.文件性质
//zk.create("/beijing01","hello word".getBytes(),Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
//删除文件 -1 默认全部操作
//zk.delete("/beijing01", -1);
//判断路径是否存在
System.out.println(zk.exists("/beijing01", false));
//获取子节点
System.out.println(zk.getChildren("/", false));
//获取连接 id
System.out.println(zk.getSessionId());
//获取zk的状态
System.out.println(zk.getState());
//设置数据
zk.setData("/test01", "hello".getBytes(),-1);
//监听
zk.exists("/test01",true);
zk.getChildren("/", true);
//getdata
zk.getData("test01", true, new Stat());
//卡住监听状态
while(true);
} catch ( Exception e) {
e.printStackTrace();
}
}
}