ZkClient operation

Basic add delete change check code as follows

public class createSession { public static void main(String[] args) { ZkClient zkClient = new ZkClient (119.45.52.68: "2181"); System.out.println("connect success"); } } public class CreateNode { public static void main(String[] args) { ZkClient zkClient = new ZkClient (119.45.52.68: "2181"); System.out.println("connect success"); / / true representatives can create recursive directory zkClient. CreatePersistent ("/zkClient/persistent/children ", true); System.out.println("create node success"); } } public class DeleteNode { public static void main(String[] args) { ZkClient zkClient = new ZkClient (119.45.52.68: "2181"); zkClient.deleteRecursive("/zkclient"); System.out.println("success delete node"); } } public class GetChildrenChanged { public static void main(String[] args) throws Exception{ ZkClient zkClient = new ZkClient (119.45.52.68: "2181"); zkClient.createPersistent("/zkClient"); Thread.sleep(1000); List<String> children = zkClient.getChildren("/zkClient"); System.out.println(children); zkClient.subscribeChildChanges("/zkClient", new IZkChildListener() { @Override public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {system.out. println(" parentPath "+parentPath+" current child path "+currentChilds); }}); zkClient.createPersistent("/zkClient/child1"); Thread.sleep(1000); zkClient.delete("/zkClient/child1"); Thread.sleep(Integer.MAX_VALUE); } } public class GetNodeData { public static void main(String[] args) throws Exception{ String path = "/zkclient-node"; ZkClient = new ZkClient("119.45.52.68:2181"); ZkClient = new ZkClient("119.45.52.68:2181"); boolean exist = zkClient.exists(path); if (! exist){ zkClient.createEphemeral(path,"123"); } zkClient.subscribeDataChanges(path, new IZkDataListener() { @Override public void handleDataChange(String dataPath, Object data) throws Exception {system.out.println (dataPath+" Node content is updated "+data); } @override public void handleDataDeleted(String dataPath) throws Exception {system.out.println (dataPath+" The node content was deleted "); }}); Object o = zkClient.readData(path); System.out.println(o); zkClient.writeData(path,"4567"); Thread.sleep(2000); zkClient.delete(path); Thread.sleep(2000); }}Copy the code

Used by the curator operation API

Public class CreateSession {public static void main(String[] args) {// RetryPolicy RetryPolicy = new ExponentialBackoffRetry (1000, 3); CuratorFramework CuratorFramework = CuratorFrameworkFactory. NewClient (" 119.45.52.68:2181, 5000300, retryPolicy); curatorFramework.start(); System.out.println("create session success"); / / second way CuratorFramework client = CuratorFrameworkFactory. Builder (). The connectString (" 119.45.52.68:2181)" .sessionTimeoutMs(5000) .sessionTimeoutMs(3000) .retryPolicy(retryPolicy) .namespace("base") .build(); client.start(); System.out.println("createe session2 success "); } } public class CreateNode { public static void main(String[] args) throws Exception{ RetryPolicy retryPolicy = new ExponentialBackoffRetry (1000, 3); CuratorFramework client = CuratorFrameworkFactory. The builder (). The connectString (119.45.52.68: "2181"). SessionTimeoutMs (5000). .sessionTimeoutMs(3000) .retryPolicy(retryPolicy) //.namespace("base") .build(); client.start(); String path = "/curator/child1"; client.create().creatingParentContainersIfNeeded() .withMode(CreateMode.PERSISTENT) .forPath(path,"init".getBytes()); System.out.println("success create node"); } } public class DeleteNode { public static void main(String[] args) throws Exception{ RetryPolicy retryPolicy = new ExponentialBackoffRetry (1000, 3); CuratorFramework client = CuratorFrameworkFactory. The builder (). The connectString (119.45.52.68: "2181"). SessionTimeoutMs (5000). .sessionTimeoutMs(3000) .retryPolicy(retryPolicy) //.namespace("base") .build(); client.start(); String path = "/curator"; client.delete().deletingChildrenIfNeeded().withVersion(-1).forPath(path); System.out.println("delete node success "); } } public class GetNodeData { public static void main(String[] args) throws Exception{ RetryPolicy retryPolicy = new ExponentialBackoffRetry (1000, 3); CuratorFramework client = CuratorFrameworkFactory. The builder (). The connectString (119.45.52.68: "2181"). SessionTimeoutMs (5000). .sessionTimeoutMs(3000) .retryPolicy(retryPolicy) //.namespace("base") .build(); client.start(); String path = "/curator/child1"; client.create().creatingParentContainersIfNeeded() .withMode(CreateMode.PERSISTENT) .forPath(path,"init".getBytes()); System.out.println("success create node"); Stat stat = new Stat(); byte [] data = client.getData().storingStatIn(stat).forPath(path); System.out.println(" data as "+new String(data)); } } public class UpdateNodeData { public static void main(String[] args) throws Exception{ RetryPolicy retryPolicy = new ExponentialBackoffRetry (1000, 3); CuratorFramework client = CuratorFrameworkFactory. The builder (). The connectString (119.45.52.68: "2181"). SessionTimeoutMs (5000). .sessionTimeoutMs(3000) .retryPolicy(retryPolicy) //.namespace("base") .build(); client.start(); String path = "/curator/child1"; // client.create().creatingParentContainersIfNeeded() // .withMode(CreateMode.PERSISTENT) // .forPath(path,"init".getBytes()); // System.out.println("success create node"); Stat stat = new Stat(); byte [] data = client.getData().storingStatIn(stat).forPath(path); System.out.println(" data as "+new String(data)); int version = client.setData().withVersion(stat.getVersion()).forPath(path).getVersion(); System.out.println("update node "+path+" version "+version); client.setData().withVersion(stat.getVersion()).forPath(path).getAversion(); }}Copy the code

The last line of code will report an error because the same data version cannot be set.

In addition to the above two common zkClient and Curatir operations. There are a few other things you can try. The code address is github.com/zhendiao/de…

Welcome to search attention to my public number [micro view technology], and summary of classified interview questions github.com/zhendiao/Ja…