zookeeper初探三 java客戶端連接
阿新 • • 發佈:2018-02-11
cti ring 客戶端連接 test tid pre toolbar client pen pom.xml
<dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>zookeeper</artifactId> <version>3.3.1</version> </dependency> </dependencies>
client.java
import 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(); } }
zookeeper初探三 java客戶端連接