A simple example of ZooKeeper (3.4.5) Open-source client Curator: zookeepercurator

Source: Internet
Author: User

A simple example of ZooKeeper (3.4.5) Open-source client Curator: zookeepercurator
1. Create a session 1. Create a session

Package com. huey. dream. demo; import org. apache. curator. framework. curatorFramework; import org. apache. curator. framework. curatorFrameworkFactory; import org. apache. curator. retry. exponentialBackoffRetry;/*** use Curator to create a session * @ author huey * @ version 1.0 * @ created 2015-3-1 */public class CarutorDemo {public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory. newClient ("192.168.1.109: 2181", // server list 5000, // Session Timeout, in milliseconds 3000, // connection creation timeout, in milliseconds new ExponentialBackoffRetry (1000, 3) // retry policy); client. start (); client. close ();}}
2. Create a session using a chained API
Package com. huey. dream. demo; import org. apache. curator. framework. curatorFramework; import org. apache. curator. framework. curatorFrameworkFactory; import org. apache. curator. retry. exponentialBackoffRetry; /*** use a chained API to create a session * @ author huey * @ version 1.0 * @ created 2015-3-1 */public class CarutorDemo {public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory. builder (). connectString ("192.168.1.109: 2181 "). sessionTimeoutMs (5000 ). connectionTimeoutMs (3000 ). retryPolicy (new ExponentialBackoffRetry (1000, 3 )). build (); client. start (); client. close ();}}

 

Ii. Create a node
Package com. huey. dream. demo; import org. apache. curator. framework. curatorFramework; import org. apache. curator. framework. curatorFrameworkFactory; import org. apache. curator. retry. exponentialBackoffRetry; import org. apache. zookeeper. createMode;/*** use Curator to create a node * @ author huey * @ version 1.0 * @ created 2015-3-1 */public class CarutorDemo {public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory. builder (). connectString ("192.168.1.109: 2181 "). sessionTimeoutMs (5000 ). connectionTimeoutMs (3000 ). retryPolicy (new ExponentialBackoffRetry (1000, 3 )). build (); client. start (); client. create (). creatingParentsIfNeeded (). withMode (CreateMode. PERSISTENT ). forPath ("/zk-huey/cnode", "hello ". getBytes (); client. close ();}}

 

3. delete a node
Package com. huey. dream. demo; import org. apache. curator. framework. curatorFramework; import org. apache. curator. framework. curatorFrameworkFactory; import org. apache. curator. retry. exponentialBackoffRetry; import org. apache. zookeeper. createMode;/*** use Curator to delete a node * @ author huey * @ version 1.0 * @ created 2015-3-1 */public class CarutorDemo {public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory. builder (). connectString ("192.168.1.109: 2181 "). sessionTimeoutMs (5000 ). connectionTimeoutMs (3000 ). retryPolicy (new ExponentialBackoffRetry (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 ();}}

 

4. Read node data
Package com. huey. dream. demo; import org. apache. curator. framework. curatorFramework; import org. apache. curator. framework. curatorFrameworkFactory; import org. apache. curator. retry. exponentialBackoffRetry; import org. apache. zookeeper. createMode; import org. apache. zookeeper. data. stat;/*** use Curator to read node data * @ author huey * @ version 1.0 * @ created 2015-3-1 */public class CarutorDemo {public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory. builder (). connectString ("192.168.1.109: 2181 "). sessionTimeoutMs (5000 ). connectionTimeoutMs (3000 ). retryPolicy (new ExponentialBackoffRetry (1000, 3 )). build (); client. start (); client. create (). creatingParentsIfNeeded (). withMode (CreateMode. PERSISTENT ). forPath ("/zk-huey/cnode", "hello ". getBytes (); Stat stat = new Stat (); byte [] nodeData = client. getData (). storingStatIn (stat ). forPath ("/zk-huey/cnode"); System. out. println ("NodeData:" + new String (nodeData); System. out. println ("Stat:" + stat); client. close ();}}

 

5. Update node data
Package com. huey. dream. demo; import org. apache. curator. framework. curatorFramework; import org. apache. curator. framework. curatorFrameworkFactory; import org. apache. curator. retry. exponentialBackoffRetry; import org. apache. zookeeper. createMode; import org. apache. zookeeper. data. stat;/*** use Curator to update node data * @ author huey * @ version 1.0 * @ created 2015-3-1 */public class CarutorDemo {public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory. builder (). connectString ("192.168.1.109: 2181 "). sessionTimeoutMs (5000 ). connectionTimeoutMs (3000 ). retryPolicy (new ExponentialBackoffRetry (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 ();}}

 

6. Obtain the subnode list
Package com. huey. dream. demo; import java. util. list; import org. apache. curator. framework. curatorFramework; import org. apache. curator. framework. curatorFrameworkFactory; import org. apache. curator. retry. exponentialBackoffRetry; import org. apache. zookeeper. createMode;/*** use Curator * @ author huey * @ version 1.0 * @ created 2015-3-1 */public class CarutorDemo {public static void main (String [] args) throws Exception {CuratorFramework client = CuratorFrameworkFactory. builder (). connectString ("192.168.1.109: 2181 "). sessionTimeoutMs (5000 ). connectionTimeoutMs (3000 ). retryPolicy (new ExponentialBackoffRetry (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 ();}}

 

Related Article

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.