zookeeper zkCli 客户端常用命令详解

2017-01-06 10:08:24来源:oschina作者:幻人点击

zkCLi是zookeeper安装之后自带的客户端命令行工具,对应命令文件为安装目录下的bin/zkCli.sh(windows为bin/zkCli.cmd).


运行./zkCli.sh会自动连接到localhost:2181,若想连接到集群或者其他主机,可用 ./zkCli.sh -server host:post[,host:post,host:post...]



root@localhost bin]# pwd /application/zookeeper-3.4.9/bin [root@localhost bin]# ./zkCli.sh Connecting to localhost:2181 2016-12-23 16:10:17,371 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.9-1757313, built on 08/23/2016 06:50 GMT 2016-12-23 16:10:17,374 [myid:] - INFO [main:Environment@100] - Client environment:host.name=localhost 2016-12-23 16:10:17,374 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.7.0_79 2016-12-23 16:10:17,377 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation 2016-12-23 16:10:17,377 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/application/jdk1.7.0_79/jre 2016-12-23 16:10:17,377 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/application/zookeeper-3.4.9/bin/../build/classes:/application/zookeeper-3.4.9/bin/../build/lib/*.jar:/application/zookeeper-3.4.9/bin/../lib/slf4j-log4j12-1.6.1.jar:/application/zookeeper-3.4.9/bin/../lib/slf4j-api-1.6.1.jar:/application/zookeeper-3.4.9/bin/../lib/netty-3.10.5.Final.jar:/application/zookeeper-3.4.9/bin/../lib/log4j-1.2.16.jar:/application/zookeeper-3.4.9/bin/../lib/jline-0.9.94.jar:/application/zookeeper-3.4.9/bin/../zookeeper-3.4.9.jar:/application/zookeeper-3.4.9/bin/../src/java/lib/*.jar:/application/zookeeper-3.4.9/bin/../conf: 2016-12-23 16:10:17,377 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib 2016-12-23 16:10:17,377 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp 2016-12-23 16:10:17,377 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler= 2016-12-23 16:10:17,377 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux 2016-12-23 16:10:17,378 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64 2016-12-23 16:10:17,378 [myid:] - INFO [main:Environment@100] - Client environment:os.version=2.6.32-431.el6.x86_64 2016-12-23 16:10:17,378 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root 2016-12-23 16:10:17,378 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root 2016-12-23 16:10:17,378 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/application/zookeeper-3.4.9/bin 2016-12-23 16:10:17,380 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@517f673b 2016-12-23 16:10:17,409 [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) Welcome to ZooKeeper! 2016-12-23 16:10:17,428 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@876] - Socket connection established to localhost/127.0.0.1:2181, initiating session JLine support is enabled 2016-12-23 16:10:17,452 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x35910c31d1c000c, negotiated timeout = 30000


WATCHER::


WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0]


连接成功后到底有那行可执行的客户端命令,可执行一个不存在的命令如 help或者 乱写一个也会输出用法。



[zk: localhost:2181(CONNECTED) 0] help ZooKeeper -server host:port cmd args connect host:port get path [watch] ls path [watch] set path data [version] rmr path delquota [-n|-b] path quit printwatches on|off create [-s] [-e] path data acl stat path [watch] close ls2 path [watch] history listquota path setAcl path acl getAcl path sync path redo cmdno addauth scheme auth delete path [version] setquota -n|-b val path



其中常用的的命令如下:


connect host:port 连接zookeeper服务器,连接之后客户端就切换到另外的服务器连接



[zk: 192.168.1.103:2181(CONNECTED) 9] connect 192.168.1.103:2181 2016-12-23 16:18:25,952 [myid:] - INFO [main:ZooKeeper@684] - Session: 0x35910c31d1c000d closed 2016-12-23 16:18:25,952 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=192.168.1.103:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@2e63764d 2016-12-23 16:18:25,953 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x35910c31d1c000d 2016-12-23 16:18:25,954 [myid:] - INFO [main-SendThread(192.168.1.103:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 192.168.1.103/192.168.1.103:2181. Will not attempt to authenticate using SASL (unknown error) 2016-12-23 16:18:25,954 [myid:] - INFO [main-SendThread(192.168.1.103:2181):ClientCnxn$SendThread@876] - Socket connection established to 192.168.1.103/192.168.1.103:2181, initiating session [zk: 192.168.1.103:2181(CONNECTING) 10] 2016-12-23 16:18:25,960 [myid:] - INFO [main-SendThread(192.168.1.103:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 192.168.1.103/192.168.1.103:2181, sessionid = 0x35910c31d1c000e, negotiated timeout = 30000


WATCHER::


WatchedEvent state:SyncConnected type:None path:null ls / [test1, test, zookeeper]



get path 获取znode节点信息,获取 /test节点信息输出如下,其中没以后##后边为我添加的注释,zxid代表zookeeper事物id(ZooKeeper transaction id)



[zk: 192.168.1.103:2181(CONNECTED) 22] get /test testData ##zk节点数据 cZxid = 0x300000069 ##zk最后修改的事物id ctime = Fri Dec 23 13:27:30 CST 2016 ##zk节点创建时间 mZxid = 0x300000092 ##zk最后修改的事物id mtime = Fri Dec 23 16:01:46 CST 2016 ##zk节点最后修改时间 pZxid = 0x300000069 ##子节点修改的事物id 由于我没有为/test添加子节点 ,故暂时pZxid=cZxid cversion = 0 ##zk节点创建时的版本号 dataVersion = 1 ##当前数据版本号 由于我修改了 /test的数据所以版本号为1 每次修改会自动+1 aclVersion = 0 ##访问权限控制(access control list)的版本号 ephemeralOwner = 0x0 ##这个暂时不知道是什么意思 看英文的意思为临时所属人 dataLength = 8 ##数据长达 numChildren = 0 ##子节点个数



ls path 列出znode节点信息



[zk: 192.168.1.103:2181(CONNECTED) 24] ls / [test1, test, zookeeper]



set path data [version] 修改节点数据,可指定要修改的版本号,如果当前版本号指定不正确,会返回version No is not valid,不会修改数据,可保证数据的一致性.如下设置数据之后版本号变为2,之后我指定版本号修改只能写当前版本2。



[zk: 192.168.1.103:2181(CONNECTED) 41] set /test testData cZxid = 0x30000009f ctime = Fri Dec 23 16:20:37 CST 2016 mZxid = 0x3000000a1 mtime = Fri Dec 23 16:24:38 CST 2016 pZxid = 0x30000009f cversion = 0 dataVersion = 2 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 8 numChildren = 0 [zk: 192.168.1.103:2181(CONNECTED) 42] set /test testData-3 12 version No is not valid : /test [zk: 192.168.1.103:2181(CONNECTED) 43] set /test testtestData 4 version No is not valid : /test [zk: 192.168.1.103:2181(CONNECTED) 44] set /test testtestData 2 cZxid = 0x30000009f ctime = Fri Dec 23 16:20:37 CST 2016 mZxid = 0x3000000a4 mtime = Fri Dec 23 16:27:54 CST 2016 pZxid = 0x30000009f cversion = 0 dataVersion = 3 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 12 numChildren = 0 [zk: 192.168.1.103:2181(CONNECTED) 45]



printwatches on|off 打印watches信息的开关,为on是会在改变节点信息的时候如果有其他的watch信息则会打印出来,如off不打印



[zk: 192.168.1.103:2181(CONNECTED) 58] printwatches printwatches is on [zk: 192.168.1.103:2181(CONNECTED) 59] printwatches off [zk: 192.168.1.103:2181(CONNECTED) 60] printwatches printwatches is off [zk: 192.168.1.103:2181(CONNECTED) 61] printwatches on [zk: 192.168.1.103:2181(CONNECTED) 62] printwatches printwatches is on


create [-s] [-e] path data acl 创建节点 -s代表创建序列节点,-e代码节点为临时节点,path必须以/(斜杠)开始,data为节点数据,acl不指定默认为world:anyone:cdwra,可参考Zookeeper的ACL access control list



[zk: localhost:2181(CONNECTED) 0] create -s /sequenceNode nodeData Created /sequenceNode0000000009 [zk: localhost:2181(CONNECTED) 1] create -s /sequenceNode nodeData Created /sequenceNode0000000010 [zk: localhost:2181(CONNECTED) 2] create -s -e /sequenceNode nodeData Created /sequenceNode0000000011 [zk: localhost:2181(CONNECTED) 3] create /test testData world:anyone:cdwra Created /test



setAcl path acl 设置节点权限控制ACL 可参考Zookeeper的ACL access control list getAcl path 获取节点权限控制



[zk: localhost:2181(CONNECTED) 6] getAcl /zookeeper 'world,'anyone : cdrwa [zk: localhost:2181(CONNECTED) 9] setAcl /zookeeper ip:192.168.1.103:cdwra cZxid = 0x0 ctime = Thu Jan 01 08:00:00 CST 1970 mZxid = 0x0 mtime = Thu Jan 01 08:00:00 CST 1970 pZxid = 0x0 cversion = -1 dataVersion = 0 aclVersion = 1 ephemeralOwner = 0x0 dataLength = 0 numChildren = 1



delete path [version] 删除节点,也可以指定要删除节点的当前版本(与 set的版本号类似),版本不正确不会删除 rmr path删除节点 跟delete一样,不过dellete能指定删除的版本号


history 打印出最近执行的十个命令


redo cmdno 根据命令编号(可用history查询编号)重新执行以前执行过的命令


close关闭当前连接,可用connect 再次连接,不会退出客户端


quit 关闭连接并退出连接客户端


节点类型参考zookeeper 节点类型


可参考Zookeeper的ACL access control list

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台