这篇是接着上篇zookeeper集群做的,所以有不熟悉的可以返回看下zookeeper集群的相关内容。
这里是相关的命名行使用方法:
基本命令用法
连接server |
zkCli.sh -server Hadoop2:2181 |
连接到Hadoop2服务器的2181端口 |
列出指定node的子node |
ls /path |
查看/path下的子节点列表 |
创建znode节点, 并指定关联数据 |
create /data test |
创建节点/date, 并将字符串"test"关联到该节点中. |
创建临时节点,并关联数据 |
create -e /data/test lele |
在节点/data下创建临时节点test,并将字符串"lele"关联到该节点中。 |
创建序列节点,并关联数据 |
create -s /haha/lala |
在节点/haha下创建序列节点ilala,以lala为前缀的编号节点,并将字符串关联到该节点中 |
修改节点的数据 |
set /data test1 |
将节点/hello下的值改为test1 |
获取znode的数据和状态信息 |
get /test |
获取节点/data的数据与状态 |
获取 znode的数据和状态,并监听 |
get /test date |
获取节点/hello的数据与状态,并监听其变更 |
删除znode |
delete /date |
使用delete命令可以删除指定znode. 当该znode拥有子znode时, 必须先删除其所有子znode, 否则操作将失败. rmr命令可用于代替delete命令, rmr是一个递归删除命令, 如果发生指定节点拥有子节点时, rmr命令会首先删除子节点. |
启用znode节点交互界面。
[root@hadoop1 zookeeper]# sh bin/zkCli.sh Connecting to localhost:2181 2018-04-19 13:52:53,549 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT 2018-04-19 13:52:53,553 [myid:] - INFO [main:Environment@100] - Client environment:host.name=hadoop1 2018-04-19 13:52:53,554 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_152 2018-04-19 13:52:53,556 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation 2018-04-19 13:52:53,556 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/jdk1.8.0_152/jre 2018-04-19 13:52:53,556 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/data/soft/zookeeper/bin/../build/classes:/data/soft/zookeeper/bin/../build/lib/*.jar:/data/soft/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/data/soft/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/data/soft/zookeeper/bin/../lib/netty-3.10.5.Final.jar:/data/soft/zookeeper/bin/../lib/log4j-1.2.16.jar:/data/soft/zookeeper/bin/../lib/jline-0.9.94.jar:/data/soft/zookeeper/bin/../zookeeper-3.4.9.jar:/data/soft/zookeeper/bin/../src/java/lib/*.jar:/data/soft/zookeeper/bin/../conf:.:/usr/local/jdk1.8.0_152/lib/dt.jar:/usr/local/jdk1.8.0_152/lib/tools.jar 2018-04-19 13:52:53,557 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 2018-04-19 13:52:53,557 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp 2018-04-19 13:52:53,557 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA> 2018-04-19 13:52:53,557 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux 2018-04-19 13:52:53,557 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64 2018-04-19 13:52:53,557 [myid:] - INFO [main:Environment@100] - Client environment:os.version=2.6.32-431.el6.x86_64 2018-04-19 13:52:53,557 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root 2018-04-19 13:52:53,557 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root 2018-04-19 13:52:53,559 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/data/soft/zookeeper 2018-04-19 13:52:53,563 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@579bb367 Welcome to ZooKeeper! 2018-04-19 13:52:53,611 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error) JLine support is enabled [zk: localhost:2181(CONNECTING) 0] 2018-04-19 13:52:53,795 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@876] - Socket connection established to localhost/127.0.0.1:2181, initiating session 2018-04-19 13:52:53,806 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x62dc72a43b0001, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null
在交互界面可以看到能够使用的命令
[zk: localhost:2181(CONNECTED) 2] help ZooKeeper -server host:port cmd args stat path [watch] set path data [version] ls path [watch] delquota [-n|-b] path ls2 path [watch] setAcl path acl setquota -n|-b val path history redo cmdno printwatches on|off delete path [version] sync path listquota path rmr path get path [watch] create [-s] [-e] path data acl addauth scheme auth quit getAcl path close connect host:port
创建节点/test/ 并获取保存节点的信息data
创建节点
[zk: localhost:2181(CONNECTED) 1] create /test data
Created /test [zk: localhost:2181(CONNECTED) 2] ls / [zookeeper, test] 获取节点信息
[zk: localhost:2181(CONNECTED) 18] get /test
data cZxid = 0x10000000c =>节点被创建的zxid值 ctime = Thu Apr 19 13:54:48 CST 2018 =>节点创建的时间 mZxid = 0x10000000c =>节点被修改时zxid值 mtime = Thu Apr 19 13:54:48 CST 2018 =>节点最后一次的修改时间 pZxid = 0x10000000c => cversion = 0 => 节点所拥有的子节点被修改的版本号 dataVersion = 0 => aclVersion = 0 => ephemeralOwner = 0x0 => dataLength = 4 =>节点数据的长度 numChildren = 0 =>节点拥有子节点的个数
在其他节点上看到信息是同步的
[zk: localhost:2181(CONNECTED) 2] ls / [zookeeper, test]
[zk: localhost:2181(CONNECTED) 10] get /test data cZxid = 0x10000000c ctime = Thu Apr 19 13:54:48 CST 2018 mZxid = 0x10000000c mtime = Thu Apr 19 13:54:48 CST 2018 pZxid = 0x10000000c cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 4 numChildren = 0
删除节点delete:但是节点下面不能有子节点
[zk: localhost:2181(CONNECTED) 21] delete /test [zk: localhost:2181(CONNECTED) 22] ls / [zookeeper]
递归删除节点rmr
[zk: localhost:2181(CONNECTED) 28] rmr /test
zookeeper的数据模型
层次化的目录结构,命名符合常规文件系统规范
每个节点在zookeeper中叫做znode,并且其有一个唯一的路径标识
节点znode可以包含数据和子节点,但是EPHEMERAL类型的节点不能有子节点
znode中的数据可以有多个版本,比如某一个路径下存有多个数据版本,那么查询这个路径下的数据就需要带上版本
客户端应用可以在节点上设置监视器
节点不支持部分读写,而是一次性完整读写
zookeeper的节点
znode有两种类型,临时节点(ephemeral)和持久节点(persistent)
znode的类型在创建时确定并且之后不能在修改
短暂znode的客户端回话结束时,zookeeper会将该短暂znode删除,短暂znode节点不可以有子节点
持久znode不依赖客户端会话,只有当客户端明确要删除该持久znode时才会被删除
znode有四种形式的目录节点,PERSISTENT、PERSISTENT_SEQUENTIAL、EPHEMERAL、EPHEMERAL_SEQUENTIAL。