pom.xml
1 2 3 4 5 6 7 | < dependencies > < dependency > < groupId >org.apache.hadoop</ groupId > < artifactId >zookeeper</ artifactId > < version >3.3.1</ version > </ dependency > </ dependencies > |
client.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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(); } } |