Zookeeper系列(二):Java API开发
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> ZooKeeper还为客户端提供了Java API编程接口的方式。API接口主要是ZooKeeper类中的方法,下面列举几个常用的客户端操作的Java代码示例。 1、create 创建ZooKeeper路径节点,参数需提供路径名称和路径关联数据。对应create命令。 示例代码如下: ZooKeeper zk= new ZooKeeper("127.0.0.1",SESSION_TIME,this); zk.create("/test","Hello".getBytes(), null, CreateMode.PERSISTENT); 2、删除 删除路径,对应delete命令。 ZooKeeper zk= new ZooKeeper("127.0.0.1",SESSION_TIME,this); zk.delete("/test", -1); 3、getData 获取路径关联数据。对应get命令。 ZooKeeper zk= new ZooKeeper("127.0.0.1",SESSION_TIME,this); String data = zk.getData("/test", false,null); 4、setData 设置节点关联数据。对应set命令。 ZooKeeper zk= new