(2-1)Zookeeper笔记


export ZOOKEEPER_HOME=/usr/local/zookeeper
export PATH=$ZOOKEEPER_HOME/bin:$PATH
//----------------------------------------------------------------------------------------
本地模式:
pwd:/usr/local/zookeeper
mkdir data
 
 
dataDir=/usr/local/zookeeper/data
clientPort=2181


集群模式:
pwd:/usr/local/zookeeper
mkdir data

pwd:/usr/local/zookeeper/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

//------------------------------------------------------------------------------------------
3-22
cl客户端:练习命令  rmr、delete

create /zk1 mybook1
create /zk1/zk11 mybook11

rmr可以删除zk1,delete不能删除zk1

create /zk2 mybook2

rmr和delete都可以删除zk2


总结:rmr可以删除有子节点的节点,delete只能删除没有子节点的节点。




java客户端:delete、getChildren

create /zk1 mybook
create /zk1/zk11 mybook
create /zk1/zk11/zk111 mybook
create /zk2 mybook

//--------------------------------------------------------------------------------------
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);

//--------------------------------------------------------------------------------------------------------------------------
zookeeper常用命令


1、启动 Zookeeper服务:bin/zkServer.sh start
2、查看 Zookeeper状态状态:bin/zkServer.sh status
3、停止 Zookeeper服务:bin/zkServer.sh stop
4、重启 Zookeeper服务:bin/zkServer.sh restart

5、连接服务器:zkCli.sh -server 192.168.1.2:2181

6、查看根目录:ls /

7、创建myData节点:create /zk myDate

8、查看节点内容: get /zk

9、设置节点内容: set /zk myBook

10、删除节点: delete /zk

你可能感兴趣的:(2-1Zookeeper笔记)