Code common operations implemented ZooKeeper

hmr.jr.zk package;

import java.util.List;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

public class TestZK {
    /**
     * 创建节点
     * @throws Exception 
     */
    @Test
    public void createNode() throws Exception{
        ZooKeeper zk=new ZooKeeper("datanode3:2181",5000,null);
        String path=zk.create("/a/a1", "tom".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println(path);
    }
    
    /**
     * 创建节点
     */
    @Test
    public void deleteNode() throws Exception{
        ZooKeeper zk=new ZooKeeper("datanode3:2181",5000,null);
        zk.delete("/a/a1/a11", 0);
    }
    /**
     * 列出所有孩子
     * @throws Exception 
     */
    @Test
    public void listAllChildren() throws Exception{
        ZooKeeper zk=new ZooKeeper("datanode1:2181,datanode2:2181,datanode3:2181",5000,null);
        listChildren(zk,"/");
    }
    public void listChildren(ZooKeeper zk,String path) throws Exception{
        String data=getData(zk,path);
        System.out.println(path+"==="+data);
        System.out.println(path);
        List<String> children =zk.getChildren(path, false);
        if(children ==null || children.isEmpty()){
            return;
        }
        for (String str : children){
            if(path.equals("/")){
                path = "";
            }
            listChildren(zk,path+"/"+str);
        }
    }
    
    /**
     * 提取数据
     */
    public String getData(ZooKeeper zk,String path){
        try{
            Stat st = new Stat();
            byte[] data=zk.getData(path, null, st);
            return new String(data);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 获取节点数据
     * @throws Exception 
     */
    @Test
    public void getZNodeStat() throws Exception{
        ZooKeeper zk=new ZooKeeper("datanode3:2181",5000,null);
        Stat stat=new Stat();
        byte[] data=zk.getData("/a/a1", false, stat);
        System.out.print(new String(data));
    }
    
    /**
     * 设置节点数据
     * @throws Exception 
     */
    @Test
    public void setData() throws Exception{
        ZooKeeper zk=new ZooKeeper("datanode3:2181",5000,null);
        zk.setData("/a", "tomas".getBytes(), -1);
    }
    
    /**
     * watch
     * @throws Exception 
     */
    @Test
    public void watch() throws Exception{
        final ZooKeeper zk=new ZooKeeper("datanode3:2181",5000,null);
        //创建watcher对象
        Watcher w=new Watcher(){
            public void process(WatchedEvent event){
                try{
                    String path=event.getPath();
                    EventType e=event.getType();
                    if(e == EventType.NodeDeleted){
                        System.out.println(path +"节点删除了,over");
                        return;
                    }
                    System.out.println(path+ "出事了!"+e);
                    zk.getData("/a/a1", this, null);
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
        };
        byte[] data = zk.getData("/a/a1", w, null);
        System.out.print("ok");
        System.out.print(new String(data));
        while(true){
            Thread.sleep(5000);
        }
    }

}
 

Guess you like

Origin blog.csdn.net/nengyu/article/details/85535925