Curator is a ZooKeeper client framework open sourced by Netflix

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:

Java code copy code  Favorite code
  1. package com.qin.curator.zk;  
  2.   
  3. import  java.io.File;  
  4. import  java.util.List;  
  5.   
  6. import org.apache.commons.io.FileUtils;  
  7. import org.apache.curator.CuratorZookeeperClient;  
  8. import org.apache.curator.RetryPolicy;  
  9. import org.apache.curator.framework.CuratorFramework;  
  10. import org.apache.curator.framework.CuratorFrameworkFactory;  
  11. import org.apache.curator.framework.CuratorFrameworkFactory.Builder;  
  12. import org.apache.curator.framework.api.CreateBuilder;  
  13. import org.apache.curator.retry.ExponentialBackoffRetry;  
  14. import org.apache.zookeeper.WatchedEvent;  
  15. import org.apache.zookeeper.Watcher;  
  16. import org.apache.zookeeper.ZKUtil;  
  17.   
  18. import framework.CrudExamples;  
  19. /** 
  20.  * @author qindongliang 
  21.  * curator operation zookeeper 
  22.  * Basic example 
  23.  * **/  
  24. publicclass CuratorTools {   
  25.       
  26.     static CuratorFramework zkclient=null;  
  27.     static String nameSpace="php";  
  28.     static {  
  29.           
  30.           String zkhost="192.168.46.22:2181";//zk的host  
  31.           RetryPolicy rp= new  ExponentialBackoffRetry( 10003 ); //Retry mechanism  
  32.           Builder builder = CuratorFrameworkFactory.builder().connectString(zkhost)  
  33.                   .connectionTimeoutMs(5000)  
  34.                   .sessionTimeoutMs(5000)  
  35.                   .retryPolicy(rp);  
  36.           builder.namespace(nameSpace);  
  37.           CuratorFramework zclient = builder.build();  
  38.           zkclient=zclient;  
  39.           zkclient.start(); // put it before this to execute  
  40.           zkclient.newNamespaceAwareEnsurePath(nameSpace);  
  41.             
  42.     }  
  43.       
  44.     publicstaticvoid main(String[] args)throws Exception {    
  45.         CuratorTools ct=new  CuratorTools();  
  46.         //ct.getListChildren("/zk/bb");  
  47.         //ct.upload("/jianli/123.txt", "D:\\123.txt");  
  48.         //ct.createrOrUpdate("/zk/cc334/zzz","c");  
  49.         //ct.delete("/qinb/bb");  
  50.         //ct.checkExist("/zk");  
  51.         ct.read("/jianli/123.txt");  
  52.         zkclient.close();  
  53.           
  54.           
  55.     }  
  56.   
  57.     /** 
  58.      * Create or update a node 
  59.      *  
  60.      * @param path path 
  61.      * @param content content 
  62.      * **/  
  63.     publicvoid createrOrUpdate(String path,String content)throws Exception{   
  64.            
  65.         zkclient.newNamespaceAwareEnsurePath(path).ensure(zkclient.getZookeeperClient());  
  66.         zkclient.setData().forPath(path,content.getBytes());      
  67.         System.out.println( "Added successfully!!!" );  
  68.           
  69.     }  
  70.       
  71.     /** 
  72.      * delete zk node 
  73.      * @param path path to delete node 
  74.      *  
  75.      * **/  
  76.     publicvoid delete(String path)throws Exception{   
  77.         zkclient.delete().guaranteed().deletingChildrenIfNeeded().forPath(path);  
  78.         System.out.println( "Delete successful!" );  
  79.     }  
  80.       
  81.       
  82.     /** 
  83.      * Determine if the path exists 
  84.      * @param path 
  85.      * **/  
  86.     publicvoid checkExist(String path)throws Exception{   
  87.           
  88.         if(zkclient.checkExists().forPath(path)==null){  
  89.             System.out.println( "Path does not exist!" );  
  90.         }else{  
  91.             System.out.println( "The path already exists!" );  
  92.         }  
  93.           
  94.     }  
  95.       
  96.     /** 
  97.      * read path 
  98.      * @param path 
  99.      * **/  
  100.     publicvoid read(String path)throws Exception{   
  101.            
  102.        
  103.         String data=new String(zkclient.getData().forPath(path),"gbk");  
  104.           
  105.         System.out.println( "Data read: " +data);  
  106.           
  107.     }  
  108.       
  109.       
  110.     /** 
  111.      * @param path path 
  112.      * Get all child files under a node 
  113.      * */  
  114.     publicvoid getListChildren(String path)throws Exception{   
  115.           
  116.         List<String> paths=zkclient.getChildren().forPath(path);  
  117.         for(String p:paths){  
  118.             System.out.println(p);  
  119.         }  
  120.           
  121.     }  
  122.       
  123.     /** 
  124.      * @param zkPath path on zk 
  125.      * @param localpath the file path on the local 
  126.      *  
  127.      * **/  
  128.     publicvoid upload(String zkPath,String localpath)throws Exception{   
  129.           
  130.         createrOrUpdate(zkPath,  "" ); //Create a path  
  131.         byte[] bs=FileUtils.readFileToByteArray(new File(localpath));  
  132.         zkclient.setData().forPath(zkPath, bs);  
  133.         System.out.println( "Upload file successfully!" );  
  134.           
  135.           
  136.     }  }  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326690597&siteId=291194637