java_api操作zookeeper節點

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();
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章