java_api操作zookeeper节点

来源:互联网 发布:金英杰免费网络课登录 编辑:程序博客网 时间:2024/05/22 06:18
package com.sanlen.zookeeper.test;

import java.util.Iterator;
import java.util.List;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
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;

import com.google.gson.Gson;

public class ZookeeperTest1 {
    ZooKeeper zk=null;
    
    
    @Before
    public void init() throws Exception{
        zk=new ZooKeeper("wmxpc1:2181,wmxpc2:2181,wmxpc3:2181",2000,null);
    }
    /**
     *增加节点
     * @throws Exception
     */
    @Test
    public void createNode() throws Exception{
        //创建临时的带序号的节点
//        String create = zk.create("/eclipse/aaa", "1212".getBytes("utf-8"), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        //创建永久的带序号的节点
//        String create = zk.create("/persistent", "1212".getBytes("utf-8"), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
        //创建临时的不带序号的节点
//        String create = zk.create("/ephemeral", "1212".getBytes("utf-8"), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        //创建永久的不带序号的
    String create = zk.create("/ephemeral", "1212".getBytes("utf-8"), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println(create);
        Thread.sleep(8000);
        zk.close();        
    }
    /**
     *删除节点
     * @throws Exception
     */
    @Test
    public void delNode() throws Exception{
        zk.delete("/ephemeral", -1);
        zk.close();
    }
    /**
     * 修改节点
     * @throws Exception
     */
    @Test
    public void testSetData() throws Exception{
        Stat setData = zk.setData("/eclipse", "eclipse vs idea".getBytes(), -1);
        byte[] data = zk.getData("/eclipse", false, null);
        System.out.println(new String(data,"utf-8"));
        zk.close();
    }
    
    
    /**
     * 判断一个节点是否存在
     * @throws Exception
     */
    @Test
    public void testExist() throws Exception{
        Stat exists = zk.exists("/eclipse/eee", false);
        System.out.println(exists==null?"不存在":"存在");
        zk.close();
    }
    /**
     * 查询目录下的字节点
     * @throws Exception
     */
    @Test
    public void testGetChildren() throws Exception{
        List<String> child = zk.getChildren("/", false);
        for (String string : child) {
            System.out.println(string);
        }
        zk.close();
    }
    
    /**
     * 存储一个对象的到zookeeper中
     * @throws Exception
     */
    @Test
    public void testPutObjectIntoZookeeper() throws Exception{
        Person p1=new Person("angleBaby");
        Gson gson=new Gson();
        String json=gson.toJson(p1);
        Stat setData = zk.setData("/aaa", json.getBytes(), -1);
        byte[] data = zk.getData("/aaa", false, null);
        String jsonBack=new String(data);
        Person p = gson.fromJson(jsonBack,Person.class);
        System.out.println(p.getName());
        zk.close();
    }
    
    
    /**
     * 查询节点的值
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        ZooKeeper zooKeeper = new ZooKeeper("wmxpc1:2181,wmxpc2:2181,wmxpc3:2181",2000,null);
        byte[] data = zooKeeper.getData("/aaa/bbb", false, null);
        System.err.println(new String(data));
        zooKeeper.close();
    }
}