Curator is a set of ZooKeeper client framework open sourced by Netflix. It is easier and more convenient to use it to operate zookeeper. According to the official metaphor of Curator, guava to JAVA, Curator to Zookeeper, Curator adopts fluent-style code, which is very concise.
For the introduction of curator: please refer to the official document: http://curator.apache.org/index.html
This article mainly looks at some basic examples of using curator to operate zookeeper:
Main functions:
1. Add on zk, or Update data
2, delete data
3 on the zk node, read data 4 on a node
, upload some local files to the zk node
5, and check whether there is a node path on zookeeper The
core code is as follows:
- package com.qin.curator.zk;
- import java.io.File;
- import java.util.List;
- import org.apache.commons.io.FileUtils;
- import org.apache.curator.CuratorZookeeperClient;
- import org.apache.curator.RetryPolicy;
- import org.apache.curator.framework.CuratorFramework;
- import org.apache.curator.framework.CuratorFrameworkFactory;
- import org.apache.curator.framework.CuratorFrameworkFactory.Builder;
- import org.apache.curator.framework.api.CreateBuilder;
- import org.apache.curator.retry.ExponentialBackoffRetry;
- import org.apache.zookeeper.WatchedEvent;
- import org.apache.zookeeper.Watcher;
- import org.apache.zookeeper.ZKUtil;
- import framework.CrudExamples;
- /**
- * @author qindongliang
- * curator operation zookeeper
- * Basic example
- * **/
- publicclass CuratorTools {
- static CuratorFramework zkclient=null;
- static String nameSpace="php";
- static {
- String zkhost="192.168.46.22:2181";//zk的host
- RetryPolicy rp= new ExponentialBackoffRetry( 1000 , 3 ); //Retry mechanism
- Builder builder = CuratorFrameworkFactory.builder().connectString(zkhost)
- .connectionTimeoutMs(5000)
- .sessionTimeoutMs(5000)
- .retryPolicy(rp);
- builder.namespace(nameSpace);
- CuratorFramework zclient = builder.build();
- zkclient=zclient;
- zkclient.start(); // put it before this to execute
- zkclient.newNamespaceAwareEnsurePath(nameSpace);
- }
- publicstaticvoid main(String[] args)throws Exception {
- CuratorTools ct=new CuratorTools();
- //ct.getListChildren("/zk/bb");
- //ct.upload("/jianli/123.txt", "D:\\123.txt");
- //ct.createrOrUpdate("/zk/cc334/zzz","c");
- //ct.delete("/qinb/bb");
- //ct.checkExist("/zk");
- ct.read("/jianli/123.txt");
- zkclient.close();
- }
- /**
- * Create or update a node
- *
- * @param path path
- * @param content content
- * **/
- publicvoid createrOrUpdate(String path,String content)throws Exception{
- zkclient.newNamespaceAwareEnsurePath(path).ensure(zkclient.getZookeeperClient());
- zkclient.setData().forPath(path,content.getBytes());
- System.out.println( "Added successfully!!!" );
- }
- /**
- * delete zk node
- * @param path path to delete node
- *
- * **/
- publicvoid delete(String path)throws Exception{
- zkclient.delete().guaranteed().deletingChildrenIfNeeded().forPath(path);
- System.out.println( "Delete successful!" );
- }
- /**
- * Determine if the path exists
- * @param path
- * **/
- publicvoid checkExist(String path)throws Exception{
- if(zkclient.checkExists().forPath(path)==null){
- System.out.println( "Path does not exist!" );
- }else{
- System.out.println( "The path already exists!" );
- }
- }
- /**
- * read path
- * @param path
- * **/
- publicvoid read(String path)throws Exception{
- String data=new String(zkclient.getData().forPath(path),"gbk");
- System.out.println( "Data read: " +data);
- }
- /**
- * @param path path
- * Get all child files under a node
- * */
- publicvoid getListChildren(String path)throws Exception{
- List<String> paths=zkclient.getChildren().forPath(path);
- for(String p:paths){
- System.out.println(p);
- }
- }
- /**
- * @param zkPath path on zk
- * @param localpath the file path on the local
- *
- * **/
- publicvoid upload(String zkPath,String localpath)throws Exception{
- createrOrUpdate(zkPath, "" ); //Create a path
- byte[] bs=FileUtils.readFileToByteArray(new File(localpath));
- zkclient.setData().forPath(zkPath, bs);
- System.out.println( "Upload file successfully!" );
- } }