export ZOOKEEPER_HOME=/usr/local/zookeeper export PATH=$ZOOKEEPER_HOME/bin:$PATH//----------------------------------------------------------------------------------------
mkdir data
dataDir=/usr/local/zookeeper/data clientPort=2181
mkdir data
vim myid
dataDir=/usr/local/zookeeper/data clientPort=2181 server.1=hadoop0:2888:3888 server.2=hadoop1:2888:3888 server.3=hadoop2:2888:3888
[root@baolibin ~]# jps 2217 Jps 2206 QuorumPeerMain
package zk1; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooDefs; import org.apache.zookeeper.ZooKeeper; public class ZK1 { private static String connectString="192.168.1.100:2181"; private static int sessionTimeout=999999; public static void main(String[] args) throws Exception{ Watcher watcher=new Watcher(){ public void process(WatchedEvent event) { System.out.println("监听到的事件:"+event); } }; //获得连接 final ZooKeeper zookeeper=new ZooKeeper(connectString,sessionTimeout,watcher); System.out.println("获得连接:"+zookeeper); //创建临时节点,创建完,在zkCli.sh里面看不到,下面不能有孩子 zookeeper.create("/zk3", "myconputer".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); //删除节点 zookeeper.delete("/zk2", -1); //创建持久节点,程序执行完,在zkCli.sh里面可以看到,该代码只能执行一次 zookeeper.create("/zk2", "mybook".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); //修改节点数据 zookeeper.setData("/zk1", "baolibin".getBytes(), -1); //得到节点数据 final byte[] data=zookeeper.getData("/zk1", watcher, null); System.out.println("读取的值:"+new String(data)); //查看节点是否存在 System.out.println("查看节点是否存在:["+zookeeper.exists("/zk1", false)+"]"); //getChildren System.out.println(zookeeper.getChildren("/zk1", true)); //关闭连接 zookeeper.close(); } }
log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. 获得连接:State:CONNECTING sessionid:0x0 local:null remoteserver:null lastZxid:0 xid:1 sent:0 recv:0 queuedpkts:0 pendingresp:0 queuedevents:0 监听到的事件:WatchedEvent state:SyncConnected type:None path:null 读取的值:baolibin [zk11]
//持久的(以下代码只能执行一次) //zooKeeper.create("/pp", " ".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); //持久有序的(在/pp/下面增加一个节点,可以重复执行,父节点必须以/结尾) //zooKeeper.create("/pp/", " ".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); //临时的(临时的下面不能有孩子) zooKeeper.create("/ee", " ".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); //临时有序的 //zooKeeper.create("/pp/", " ".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);