pom.xml
org.apache.hadoop
zookeeper
3.3.1
client.javaimport org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
public class client {
private static final int TIME_OUT = 3000;
private static final String HOST = "localhost:2181";
public static void main(String[] args) throws Exception{
ZooKeeper zookeeper = new ZooKeeper(HOST, TIME_OUT, null);
System.out.println("=========创建节点===========");
if(zookeeper.exists("/zk", false) == null)
{
zookeeper.create("/zk", "zk data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
System.out.println("=============查看节点是否安装成功===============");
System.out.println(new String(zookeeper.getData("/zk", false, null)));
System.out.println("=========修改节点的数据==========");
String data = "zNode2";
zookeeper.setData("/zk", data.getBytes(), -1);
System.out.println("========查看修改的节点是否成功=========");
System.out.println(new String(zookeeper.getData("/zk", false, null)));
System.out.println("=======删除节点==========");
zookeeper.delete("/zk", -1);
System.out.println("==========查看节点是否被删除============");
System.out.println("节点状态:" + zookeeper.exists("/test", false));
Thread.sleep(1000);
zookeeper.close();
}
}