ZooKeeper (3.4.5) Simple example of open source client curator

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.