zookeeper api 增删改查

阅读更多
package com.jianfei.zk;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

public class ZKDemo {

 private ZooKeeper zk = null;

 @Before
 public void init() throws IOException {
  zk = new ZooKeeper("hdp-node01:2081", 2000, new Watcher() {
   public void process(WatchedEvent event) {
    if(event.getType() == EventType.None) return;
    System.out.println(event.getType()); 
    System.out.println(event.getPath()); 
    
    //-----解决持续监听
    try {
     //数据变化持续监听
     zk.getData("/sh18qi", true, null);
     //子节点持续监听
     zk.getChildren("/sh18qi", true);
    } catch (KeeperException e) {
     e.printStackTrace();
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  });
 }
 
 /**
  * 向zookeeper服务集群中注册数据,添加znode
  * 
  * @throws InterruptedException
  * @throws KeeperException
  * @throws UnsupportedEncodingException
  */
 @Test
 public void testCreateZnode() throws UnsupportedEncodingException, KeeperException, InterruptedException {
  zk.create("/sh18qi", "这是我的第一个案例".getBytes("UTF-8"), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  zk.create("/sh18qi/banzhang", "花花".getBytes("UTF-8"), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
  zk.create("/sh18qi/banmi", "草草".getBytes("UTF-8"), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
  zk.close();
 }
 /**
  * 从zookeeper删除znode
  * @throws InterruptedException
  * @throws KeeperException
  */
 @Test
 public void testDeleteZnode() throws InterruptedException, KeeperException{
  //参数1:要删除的节点路径
  //参数2:要删除的节点版本  -1 匹配所有版本
  zk.delete("/sh18qi", -1);
  //????不监听  为什么上面方法还是执行呢?
  Stat exites = zk.exists("/sh18qi", false);//第二个参数,是否监听  
  
  System.out.println(exites);
 }
 /**
  * 修改zookeeper节点
  * @throws UnsupportedEncodingException
  * @throws KeeperException
  * @throws InterruptedException
  */
 @Test
 public void testUpdateZnode() throws UnsupportedEncodingException, KeeperException, InterruptedException{
  byte[] data = zk.getData("/sh18qi", false, null);//默认最新
  System.out.println(new String(data,"UTF-8"));
  zk.setData("/sh18qi", "我有一只小毛驴".getBytes("UTF-8"), -1);
  data = zk.getData("/sh18qi", false, null);//默认最新
  System.out.println(new String(data,"UTF-8"));
 }
 /**
  * 查询zookeeper的子节点
  * @throws KeeperException
  * @throws InterruptedException
  */
 @Test
 public void testGetChildren() throws KeeperException, InterruptedException{
  List children = zk.getChildren("/sh18qi", false);
  for(String child : children){
   System.out.println(child);
  }
 }
 /**
  * zk的监听机制:
  * 1.事先定义好监听的回调函数
  * 2.在对znode进行各种访问操作时可以注册监听
  * 3.监听的znode上发生相应的事件时,客户端zk会接收到zookeeper集群的事件通知
  * 4.客户端zk根据事件调用我们事先定义好的回掉函数
  * @throws InterruptedException 
  * @throws KeeperException 
  */
 @Test
 public void testWatch() throws KeeperException, InterruptedException{
  //在获取znode数据时注册监听
  //监听器只能起到一次作用,只要监听到一次事件就失效了
  //getData监听的是数据的更改
  byte[] data = zk.getData("/sh18qi", true, null);
  
  
  //在做查询子节点操作的时候注册监听
  //监听的事件就是监听节点下的子节点变化的事件
  List children = zk.getChildren("/sh18qi", true);
  
  Thread.sleep(Long.MAX_VALUE);
  
  
  //事件类型
  //EventType
  
  
 }
 
 /**
  * 模拟配置文件集中管理
  * 将配置文件上传到zookeeper中
  * @throws IOException
  * @throws KeeperException
  * @throws InterruptedException
  */
 public void testUploadConfigFileToZookeeper() throws IOException, KeeperException, InterruptedException{
  String schema_xml = FileUtils.readFileToString(new File("g:/schema.xml"));
  //System.out.println(schema_xml);
//  zk.create("/conf", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  zk.create("/conf/schema.xml", schema_xml.getBytes("UTF-8"),Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  
  zk.close();
 }
 
}

 

你可能感兴趣的:(zookeeper api 增删改查)