从零开始学Hadoop大数据分析之ZooKeeper实战

导言

上一节我学习了关于ZooKeeper基础知识,接下开始ZooKeeper的实战,只有从实战中学习才能进步更快。

ZooKeeper命令集

zookeeper常用命令大致分为连接,获取,修改,删除,关闭连接五步。


从零开始学Hadoop大数据分析之ZooKeeper实战_第1张图片
zookeeper常用命令.png

实战

导入架包
  
            org.apache.zookeeper
            zookeeper
            3.4.14
        
实现Watcher对象
public class ZooKeeperUtil implements Watcher {
 public void process(WatchedEvent watchedEvent) {
        if (Event.KeeperState.SyncConnected==watchedEvent.getState()){
            if (Event.EventType.NodeCreated==watchedEvent.getType()){
                try {
                    //注册
                    zooKeeper.exists(watchedEvent.getPath(),true);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else if (Event.EventType.NodeDeleted==watchedEvent.getType()){
                try {
                    //注册
                    zooKeeper.exists(watchedEvent.getPath(),true);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else if (Event.EventType.NodeDataChanged==watchedEvent.getType()){
                try {
                    //注册
                    zooKeeper.exists(watchedEvent.getPath(),true);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
连接服务器
 public static ZooKeeper zooKeeper=null;
 private final static int SESSION_TIMEOUT=30000;
    /**
     * 初始化ZooKeeper
     * @param ipAddress 节点地址
     * @param path  创建路径
     */
    public void initZooKeeper(String ipAddress,String path){
        try {
            //创建对象
            zooKeeper=new ZooKeeper(ipAddress,SESSION_TIMEOUT,new ZooKeeperUtil());
            //注册
            zooKeeper.exists(path,true);
//            byte[] bytes= "mycontent1".getBytes();
//            createNode(path,bytes);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
    }
创建节点
 /**
     * 创建节点
     * @param path  数据地址
     * @param bytes  写入数据
     */
    public void createNode(String ipAddress,String path,byte[] bytes){
        //创建节点
        try {
            if (zooKeeper!=null){
                initZooKeeper(ipAddress,path);
            }
            if (path.length()>0&&bytes.length>0){
                zooKeeper.create(path,bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
                zooKeeper.close();
            }
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

得到数据
 /**
     * 获取数据
     * @param path  数据地址
     * @return  数据
     */
    public String getNodeData(String ipAddress,String path){
        try {
            if (zooKeeper!=null){
                initZooKeeper(ipAddress,path);
            }
            if (path.length()>0){
               byte[] datas= zooKeeper.getData(path,null,null);
                zooKeeper.close();
                String result=new String(datas);
                return result;
            }
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }
删除节点
 /**
     * 删除节点
     * @param path
     */
    public void delNode(String ipAddress,String path){
        try {
            if (zooKeeper!=null){
                initZooKeeper(ipAddress,path);
            }
            if (path.length()>0){
            zooKeeper.delete(path,-1);
                zooKeeper.close();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(从零开始学Hadoop大数据分析之ZooKeeper实战)