《Zookeeper 2》--Zookeeper客户端的java API

Zookeeper API:

**** 若运行项目报错  ****
     1.将 log4j.properties 拷贝到项目下,路径不能放错
     2.将 hadoop jar 包 引入项目下                                                                                                                                                      ****************************

    1.API 测试:

public class TestZookeeper{
    //创建zookeeper对象 
    //192.168.179.103:2181    192.168.179.103    master
    ZooKeeper zk = new ZooKeeper("192.168.179.103:2181",5000,null);
    
    //创建永久znode   create /ccc1  hellowordccc
    zk.create("/ccc2", "hellowordccc".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

    //创建永久znode   create  -s   /ccc1  hellowordccc
    zk.create("/ccc2", "hellowordccc".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
    
    //创建临时znode   create -e /ccc3  hellowordccc
    zk.create("/ccc3", "hellowordccc".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    
     //创建临时znode zonde有序列   create -e /ccc3  hellowordccc
     zk.create("/ccc4", "hellowordccc".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
    
     Thread.sleep(10000L);

    //设置要给版本 set   /ccc1    "new ccc1"
    zk.setData("/ccc1", "new ccc2 ".getBytes(), 1);
    
    //  ls   /
    List<String> paths = zk.getChildren("/ccc1", null);
    for(String path : paths) {
        System.out.println(path);
    }
    
    //没有子路径的znode删除
    zk.delete("/ccc1", -1);
    
    //有子路径的znode删除
    List<String> paths = zk.getChildren("/ccc1", null);
    for(String path : paths) { 
        zk.delete("/ccc1/"+path, -1);
    }
    zk.delete("/ccc1", -1);
    zk.close();

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        String host = "192.168.179.136:2181";
        ZooKeeper zKeeper = new ZooKeeper(host, 2000,null);  
        zKeeper.create("/bbb",  "bbbword".getBytes(),  Ids.OPEN_ACL_UNSAFE,  CreateMode.PERSISTENT);
        zKeeper.close();
    }



/**
 *第二部分测试
 */
    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        String host = "192.168.179.135:2181";
        ZooKeeper zKeeper = new ZooKeeper(host, 2000,null);  

        //删除目录不能有子节点
        zKeeper.delete("/aaaccc", 1);
    
        Thread.sleep(10000L);
        zKeeper.close();
    }    

    // 版本 -1 ,可以不指定版本
    zKeeper.setData("/test2", "ok1231212".getBytes(), 1);  
     /*在集群上查看结果                                                                                                                                                                            [zk: master:2181(CONNECTED) 8] get /test2
      ok1231212                                                                                                                                                                                          */

    byte[] byts = zk.getData("/a2", null, null);
    System.out.println(new String(byts));


    List<String> list = zKeeper.getChildren("/", null); 
    for(String znode : list) {
        System.out.println(znode);
    }

    List<String> childrens  = zk.getChildren("/a2", null);
    for(String path : childrens) { 
        zk.delete("/a2/"+path, -1); 
    }
    zk.delete("/a2", -1);
    System.out.println("OK");

    /*
      [zk: master:2181,slave1:2181(CONNECTED) 36] ls /a4
      [aaa]
      [zk: master:2181,slave1:2181(CONNECTED) 37] rmr /a4
     */
   
    Stat stat = zkClient.exists("/eclipse", false);
    System.out.println(stat==null?"not exist":"exist");

}


2、实现观察者要实现 接口/内部类
      watcher(观察者 案例) :zk客户端连接服务端触发一次事件 (构造函数连接zk的服务端),
                                            所以执行一次process

       命令行: stat    /test2    true

       测试:
              stat命令可以得到一个znode节点的属性,
              并允许我们在已经存在的znode节点上设置监视点.
              通过在路径后面设置参数true来添加监视点,
              当活动的主节点崩溃时, 我们会观察到.

             $>stat /master true

             $>set /master 2222222helloword

  
  --创建观察者两种方式:
           1)创建 内部类  
           2)实现Watcher接口
    2.watcher(观察者)

public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
    Watcher wc = new Watcher(){
        @Override
        public void process(WatchedEvent event) { 
            System.out.println("观察者正在执行"+event.getType()+"   " +event.toString());
        } 
    };
    
    String host = "192.168.179.135:2181";
    ZooKeeper zKeeper = new ZooKeeper(host, 2000,wc);  
    Thread.sleep(10000L);

    zKeeper.setData("/test2", "ok3666".getBytes(), 2);  
    System.out.println("设置test2"); 

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值