One, create session 1. Create a session
PackageCom.huey.dream.demo;Importorg.apache.curator.framework.CuratorFramework;Importorg.apache.curator.framework.CuratorFrameworkFactory;ImportOrg.apache.curator.retry.ExponentialBackoffRetry;/*** Create a session with curator *@authorHuey *@version1.0 * @created 2015-3-1*/ Public classCarutordemo { Public Static voidMain (string[] args)throwsException {curatorframework client=Curatorframeworkfactory.newclient ("192.168.1.109:2181",//Server List5000,//session time-out, per millisecond3000,//Connection creation time-out, per millisecond NewExponentialbackoffretry (1000, 3)//Retry Policy ); Client.start (); Client.close (); } }2. Creating a session using a chained-style API interface
PackageCom.huey.dream.demo;Importorg.apache.curator.framework.CuratorFramework;Importorg.apache.curator.framework.CuratorFrameworkFactory;ImportOrg.apache.curator.retry.ExponentialBackoffRetry;/*** Create a session using a chained-style API interface *@authorHuey *@version1.0 * @created 2015-3-1*/ Public classCarutordemo { Public Static voidMain (string[] args)throwsException {curatorframework client=Curatorframeworkfactory.builder (). ConnectString ("192.168.1.109:2181"). SESSIONTIMEOUTMS (5000). CONNECTIONTIMEOUTMS (3000). Retrypolicy (NewExponentialbackoffretry (1000, 3) . Build (); Client.start (); Client.close (); } }
Second, create the node
PackageCom.huey.dream.demo;Importorg.apache.curator.framework.CuratorFramework;Importorg.apache.curator.framework.CuratorFrameworkFactory;ImportOrg.apache.curator.retry.ExponentialBackoffRetry;ImportOrg.apache.zookeeper.CreateMode;/*** Create nodes with curator *@authorHuey *@version1.0 * @created 2015-3-1*/ Public classCarutordemo { Public Static voidMain (string[] args)throwsException {curatorframework client=Curatorframeworkfactory.builder (). ConnectString ("192.168.1.109:2181"). SESSIONTIMEOUTMS (5000). CONNECTIONTIMEOUTMS (3000). Retrypolicy (NewExponentialbackoffretry (1000, 3) . Build (); Client.start (); Client.create (). creatingparentsifneeded (). Withmode (createmode.persistent). Forpath ("/zk-huey/cnode", "Hello". GetBytes ()); Client.close (); } }
Third, delete the node
PackageCom.huey.dream.demo;Importorg.apache.curator.framework.CuratorFramework;Importorg.apache.curator.framework.CuratorFrameworkFactory;ImportOrg.apache.curator.retry.ExponentialBackoffRetry;ImportOrg.apache.zookeeper.CreateMode;/*** Delete nodes using curator *@authorHuey *@version1.0 * @created 2015-3-1*/ Public classCarutordemo { Public Static voidMain (string[] args)throwsException {curatorframework client=Curatorframeworkfactory.builder (). ConnectString ("192.168.1.109:2181"). SESSIONTIMEOUTMS (5000). CONNECTIONTIMEOUTMS (3000). Retrypolicy (NewExponentialbackoffretry (1000, 3) . Build (); Client.start (); Client.create (). creatingparentsifneeded (). Withmode (createmode.persistent). Forpath ("/zk-huey/cnode", "Hello". GetBytes ()); Client.delete (). Guaranteed (). deletingchildrenifneeded (). Withversion (-1). Forpath ("/zk-huey"); Client.close (); } }
Four, read the node data
PackageCom.huey.dream.demo;Importorg.apache.curator.framework.CuratorFramework;Importorg.apache.curator.framework.CuratorFrameworkFactory;ImportOrg.apache.curator.retry.ExponentialBackoffRetry;ImportOrg.apache.zookeeper.CreateMode;ImportOrg.apache.zookeeper.data.Stat;/*** Read node data using curator *@authorHuey *@version1.0 * @created 2015-3-1*/ Public classCarutordemo { Public Static voidMain (string[] args)throwsException {curatorframework client=Curatorframeworkfactory.builder (). ConnectString ("192.168.1.109:2181"). SESSIONTIMEOUTMS (5000). CONNECTIONTIMEOUTMS (3000). Retrypolicy (NewExponentialbackoffretry (1000, 3) . Build (); Client.start (); Client.create (). creatingparentsifneeded (). Withmode (createmode.persistent). Forpath ("/zk-huey/cnode", "Hello". GetBytes ()); Stat Stat=NewStat (); byte[] Nodedata =client.getdata (). Storingstatin (STAT). Forpath ("/zk-huey/cnode"); System.out.println ("Nodedata:" +NewString (nodedata)); System.out.println ("Stat:" +stat);Client.close (); } }
V. Update node data
PackageCom.huey.dream.demo;Importorg.apache.curator.framework.CuratorFramework;Importorg.apache.curator.framework.CuratorFrameworkFactory;ImportOrg.apache.curator.retry.ExponentialBackoffRetry;ImportOrg.apache.zookeeper.CreateMode;ImportOrg.apache.zookeeper.data.Stat;/*** Update node data with curator *@authorHuey *@version1.0 * @created 2015-3-1*/ Public classCarutordemo { Public Static voidMain (string[] args)throwsException {curatorframework client=Curatorframeworkfactory.builder (). ConnectString ("192.168.1.109:2181"). SESSIONTIMEOUTMS (5000). CONNECTIONTIMEOUTMS (3000). Retrypolicy (NewExponentialbackoffretry (1000, 3) . Build (); Client.start (); Client.create (). creatingparentsifneeded (). Withmode (createmode.persistent). Forpath ("/zk-huey/cnode", "Hello". GetBytes ()); Client.setdata (). Withversion (-1). Forpath ("/zk-huey/cnode", "World". GetBytes ()); Client.close (); } }
Vi. getting a list of child nodes
PackageCom.huey.dream.demo;Importjava.util.List;Importorg.apache.curator.framework.CuratorFramework;Importorg.apache.curator.framework.CuratorFrameworkFactory;ImportOrg.apache.curator.retry.ExponentialBackoffRetry;ImportOrg.apache.zookeeper.CreateMode;/*** Use curator *@authorHuey *@version1.0 * @created 2015-3-1*/ Public classCarutordemo { Public Static voidMain (string[] args)throwsException {curatorframework client=Curatorframeworkfactory.builder (). ConnectString ("192.168.1.109:2181"). SESSIONTIMEOUTMS (5000). CONNECTIONTIMEOUTMS (3000). Retrypolicy (NewExponentialbackoffretry (1000, 3) . Build (); Client.start (); Client.create (). creatingparentsifneeded (). Withmode (createmode.persistent). Forpath ("/zk-huey/cnode", "Hello". GetBytes ()); List<String> children = Client.getchildren (). Forpath ("/zk-huey"); System.out.println ("Children:" +children); Client.close (); } }
ZooKeeper (3.4.5) Simple example of open source client curator