Zookeeper基础API的操作-获取数据

获取数据
客户端可以通过Zookeeper的API来获取一个节点的数据内容,

   void getData(String path, boolean watch, DataCallback cb, Object ctx)
    byte[]  getData(String path, boolean watch, Stat stat)
    void    getData(String path, Watcher watcher, DataCallback cb, Object ctx)
    byte[]  getData(String path, Watcher watcher, Stat stat)

这里也有同步和异步接口,
path:指定数据节点的节点路径,
watcher:注册的watcher,一旦注册节点内容有变更,就会向客户端发送通知,该参数允许传入null
stat:指定数据节点的节点状态信息,用法是在接口中传入一个旧的stat变量,该stat变量会在方法执行过程中,被来自服务端响应的新stat对象替换
Watch:表明是否需要注册一个watcher
Cb:注册一个异步回调函数
Ctx:用于传递上下文信息的对象

同步读取数据

package com.paic.Spark;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * Created by Xlucas on 2018/4/16.
 */
public class ZookeeperDemo7 implements  Watcher{
    private static CountDownLatch connectedSe=new CountDownLatch(1);
    private static ZooKeeper zk=null;
    private  static Stat stat=new Stat();
    public  static void  main(String[] args) throws IOException, InterruptedException, KeeperException {
        String path="/zk-hosts-getDate";
        zk=new ZooKeeper("10.25.76.173", 2000, new ZookeeperDemo7());
        connectedSe.await();
        //创建父节点
        zk.create(path,"get1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        //获取节点数据内容
        System.out.println(new String(zk.getData(path,true,stat)));
        System.out.println(stat.getCzxid()+","+ stat.getMzxid()+","+stat.getVersion());
        zk.setData(path,"123".getBytes(),-1);
        Thread.sleep(Integer.MAX_VALUE);
    }
    @Override
    public void process(WatchedEvent event) {
        //监听路径节点下是否有变化,
        if(Watcher.Event.KeeperState.SyncConnected==event.getState()){
            if (Watcher.Event.EventType.None==event.getType()&&null ==event.getPath()){
                connectedSe.countDown();
            }else if(event.getType()== Event.EventType.NodeDataChanged){
                try{
                    System.out.println(new String(zk.getData(event.getPath(),true,stat)));
                    System.out.println(stat.getCzxid()+","+stat.getMzxid()+","+stat.getVersion());
                }catch (Exception e){

                }
            }
        }
    }
}

运行结果
get1
30064782423,30064782423,0
123
30064782423,30064782424,1

        首先创建一个节点/zk-hosts-getDate,并初始化其数据内容为”get1”,然后调用getData的同步接口来获取/zk-hosts-getDate节点的数据内容,调用的同时注册了一个watcher,之后我们同样以“123”去更新将该节点的数据内容,此时,由于我们之前在该节点上注册了一个watcher,因此,一旦该节点的数据发生变化,Zookeeper服务端就会向客户端发出一个“数据变更”的事件通知,于是,客户端可以在收到这个事件通知后,再次调用getData接口来获取新的数据内容,
        另外,在调用getData接口的同时,我们传入了一个stat变量,在Zookeeper客户端的内部实现中,会从服务端的相应中获取到数据节点的最新节点状态信息,来替换这个客户端的旧状态
        从上面的内容我们可以看到,Czxid为”30064782423”时被创建,在”30064782424”被更新,于是节点的数据版本从”0“到”1”,从这里我们可以知道,数据内容或者是数据版本变化,都会触发服务端的NodeDataChanged通知。

异步读取数据

package com.paic.Spark;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * Created by Xlucas on 2018/4/16.
 */
public class ZookeeperDemo8 implements Watcher {
    private static CountDownLatch connectedSe=new CountDownLatch(1);
    private static ZooKeeper zk=null;
    public  static void  main(String[] args) throws IOException, InterruptedException, KeeperException {
        String path="/zk-hosts-getDate1";
        zk=new ZooKeeper("10.25.76.173", 2000, new ZookeeperDemo8());
        connectedSe.await();
        //创建父节点
        zk.create(path,"get1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zk.getData(path,true,new DataCallBack(),null);
        zk.setData(path,"123".getBytes(),-1);
        Thread.sleep(Integer.MAX_VALUE);
    }
    @Override
    public void process(WatchedEvent event) {
        if(Event.KeeperState.SyncConnected==event.getState()){
            if(Event.EventType.None==event.getType()&&null==event.getPath()){
                connectedSe.countDown();
            }else if(event.getType()== Event.EventType.NodeDataChanged){
                try {
                    zk.getData(event.getPath(),true,new DataCallBack(),null);
                }catch (Exception e){

                }
            }
        }
    }
}

package com.paic.Spark;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.data.Stat;

/**
 * Created by Xlucas on 2018/4/16.
 */
public class DataCallBack implements AsyncCallback.DataCallback {
    @Override
    public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
        System.out.println(rc+", "+path+", "+new String(data));
        System.out.println(stat.getCzxid()+", "+stat.getMzxid()+", "+stat.getVersion());
    }
}

运行结果
0, /zk-hosts-getDate1, get1
30064782449, 30064782449, 0
0, /zk-hosts-getDate1, 123
30064782449, 30064782450, 1

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/paicMis/article/details/79955851
文章标签: Zookeeper getData
个人分类: zookeeper
上一篇Java连接数据库JDBC-类加载方式
下一篇XML基础学习和DOM4J解析
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭