java连接zookeeper集群

/*
 * Copyright (C), 2016-2016, 上

 * Date:     2016年10月25日 下午2:22:44  

API  http://zookeeper.apache.org/doc/r3.4.8/api/index.html

 */
package com.zookeeper.test.base;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;


import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.Op;
import org.apache.zookeeper.Transaction;
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.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.test.ClientBase;
import org.codehaus.jettison.badgerfish.BadgerFishXMLStreamWriter;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;


/**
 * @date 2016年10月25日 下午2:22:44 zookeeper-3.4.9
 */
public class Test {


    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
         String url = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183";
        try {
            ZooKeeper zooKeeper = new ZooKeeper(url, ClientBase.CONNECTION_TIMEOUT, new Watcher() {


                @Override
                public void process(WatchedEvent watchedevent) {
                    // TODO Auto-generated method stub
                    System.out.println("已结触发了:" + watchedevent.getPath() + "," + watchedevent.getState() + ","
                            + watchedevent.getType());
//                    None = new EventType("None", 0, -1);
//                    NodeCreated = new EventType("NodeCreated", 1, 1);
//                    NodeDeleted = new EventType("NodeDeleted", 2, 2);
//                    NodeDataChanged = new EventType("NodeDataChanged", 3, 3);
//                    NodeChildrenChanged = new EventType("NodeChildrenChanged", 4, 4);
//                    $VALUES = (new EventType[] { None, NodeCreated, NodeDeleted, NodeDataChanged, NodeChildrenChanged });
                }
            });
//            zooKeeper.addAuthInfo("digest", "user:password".getBytes());


            zooKeeper.create("/testRootPath", "testRootData".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            // 创建一个子目录节点
            zooKeeper.create("/testRootPath/testChildPathOne", "testChildDataOne".getBytes(), Ids.OPEN_ACL_UNSAFE,
                    CreateMode.PERSISTENT);
            System.out.println(new String(zooKeeper.getData("/testRootPath", false, null)));
            // 取出子目录节点列表
            System.out.println(zooKeeper.getChildren("/testRootPath", true));
            // 修改子目录节点数据
            zooKeeper.setData("/testRootPath/testChildPathOne", "modifyChildDataOne".getBytes(), -1);
            System.out.println("目录节点状态:[" + zooKeeper.exists("/testRootPath", true) + "]");
            // 创建另外一个子目录节点
            zooKeeper.create("/testRootPath/testChildPathTwo", "testChildDataTwo".getBytes(), Ids.OPEN_ACL_UNSAFE,
                    CreateMode.PERSISTENT);
            System.out.println(new String(zooKeeper.getData("/testRootPath/testChildPathTwo", true, null)));
            // 删除子目录节点
            Stat stat=zooKeeper.exists("/testRootPath/testChildPathTwo", true);//true需要wather
            System.out.println("stat1:"+stat);
            
            zooKeeper.delete("/testRootPath/testChildPathTwo", -1);
             stat=zooKeeper.exists("/testRootPath/testChildPathTwo", false);//true需要wather
            System.out.println("stat2:"+stat);
            zooKeeper.delete("/testRootPath/testChildPathOne", -1);
            // 删除父目录节点
            zooKeeper.delete("/testRootPath", -1);
            // 关闭连接
            zooKeeper.close();
            
//            Transaction transaction=zooKeeper.transaction();
//            OP op=Op.create(path, data, acl, createMode);
//            op=Op.delete(path, version);
//            op.setData(path, data, version);
//            op.check(path, version);
//            transaction.commit();
//            zooKeeper.multi(new Op[]{op});


        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (KeeperException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }


}




//public class ZooDefs {
//    public static interface Ids {
//
//        public static final Id ANYONE_ID_UNSAFE = new Id("world", "anyone");
//        public static final Id AUTH_IDS = new Id("auth", "");
//        public static final ArrayList OPEN_ACL_UNSAFE = new ArrayList(
//                Collections.singletonList(new ACL(31, ANYONE_ID_UNSAFE)));
//        public static final ArrayList CREATOR_ALL_ACL = new ArrayList(Collections.singletonList(new ACL(31, AUTH_IDS)));
//        public static final ArrayList READ_ACL_UNSAFE = new ArrayList(
//                Collections.singletonList(new ACL(1, ANYONE_ID_UNSAFE)));
//
//    }
//
//    public static interface Perms {
//
//        public static final int READ = 1;
//        public static final int WRITE = 2;
//        public static final int CREATE = 4;
//        public static final int DELETE = 8;
//        public static final int ADMIN = 16;
//        public static final int ALL = 31;
//    }
//
//    public static interface OpCode {
//
//        public static final int notification = 0;
//        public static final int create = 1;
//        public static final int delete = 2;
//        public static final int exists = 3;
//        public static final int getData = 4;
//        public static final int setData = 5;
//        public static final int getACL = 6;
//        public static final int setACL = 7;
//        public static final int getChildren = 8;
//        public static final int sync = 9;
//        public static final int ping = 11;
//        public static final int getChildren2 = 12;
//        public static final int check = 13;
//        public static final int multi = 14;
//        public static final int auth = 100;
//        public static final int setWatches = 101;
//        public static final int sasl = 102;
//        public static final int createSession = -10;
//        public static final int closeSession = -11;
//        public static final int error = -1;
//    }
//
//    public ZooDefs() {
//    }
//
//    public static final String opNames[] = { "notification", "create", "delete", "exists", "getData", "setData",
//            "getACL", "setACL", "getChildren", "getChildren2", "getMaxChildren", "setMaxChildren", "ping" };
//
//}

你可能感兴趣的:(zookeeper)