资料来源:
1、连接到zookeeper服务
[root@datanode1 ~]# zkCli.sh -server 127.0.0.1:2181
2、zk命令行工具的一些简单操作如下:
1)、显示根目录下文件
#使用ls命令来查看当前zookeeper中所包含的内容
[zk: 127.0.0.1:2181(CONNECTED) 2] ls /[storm, hbase, zookeeper]
#查看当前节点数据并能看到更新次数等数据
[zk: 127.0.0.1:2181(CONNECTED) 3] ls2 /[storm, hbase, zookeeper]cZxid = 0x0ctime = Thu Jan 01 07:00:00 CST 1970mZxid = 0x0mtime = Thu Jan 01 07:00:00 CST 1970pZxid = 0x1100000002cversion = 1dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 0numChildren = 3[zk: 127.0.0.1:2181(CONNECTED) 4]
2)、创建文件,并设置初始内容
#创建一个新的znode节点“zk”以及与它关联的字符串
[zk: 127.0.0.1:2181(CONNECTED) 4] create /zk "test4~"Created /zk[zk: 127.0.0.1:2181(CONNECTED) 5] ls /[zk, storm, hbase, zookeeper][zk: 127.0.0.1:2181(CONNECTED) 6] create /zk "test01"Node already exists: /zk[zk: 127.0.0.1:2181(CONNECTED) 7] ls /[zk, storm, hbase, zookeeper][zk: 127.0.0.1:2181(CONNECTED) 8]
3)、获取文件内容
#确认znode是否包含我们所创建的字符串
[zk: 127.0.0.1:2181(CONNECTED) 9] get /zk"test4~"cZxid = 0x1800001c42ctime = Mon Aug 03 14:59:30 CST 2015mZxid = 0x1800001c42mtime = Mon Aug 03 14:59:30 CST 2015pZxid = 0x1800001c42cversion = 0dataVersion = 0aclVersion = 0ephemeralOwner = 0x0dataLength = 8numChildren = 0[zk: 127.0.0.1:2181(CONNECTED) 10]
4)、修改文件内容
#对zk所关联的字符串进行设置
[zk: 127.0.0.1:2181(CONNECTED) 10] set /zk "zkbak" cZxid = 0x1800001c42ctime = Mon Aug 03 14:59:30 CST 2015mZxid = 0x1800001d07mtime = Mon Aug 03 15:04:52 CST 2015pZxid = 0x1800001c42cversion = 0dataVersion = 1aclVersion = 0ephemeralOwner = 0x0dataLength = 7numChildren = 0[zk: 127.0.0.1:2181(CONNECTED) 11]
[zk: 127.0.0.1:2181(CONNECTED) 11] get /zk"zkbak"cZxid = 0x1800001c42ctime = Mon Aug 03 14:59:30 CST 2015mZxid = 0x1800001d07mtime = Mon Aug 03 15:04:52 CST 2015pZxid = 0x1800001c42cversion = 0dataVersion = 1aclVersion = 0ephemeralOwner = 0x0dataLength = 7numChildren = 0[zk: 127.0.0.1:2181(CONNECTED) 12]
5)、删除文件
#将刚才创建的znode删除
[zk: 127.0.0.1:2181(CONNECTED) 12] delete /zk[zk: 127.0.0.1:2181(CONNECTED) 13] ls /[storm, hbase, zookeeper][zk: 127.0.0.1:2181(CONNECTED) 14]
6)、退出客户端
[zk: 127.0.0.1:2181(CONNECTED) 15] quit
7)、帮助命令
[zk: 127.0.0.1:2181(CONNECTED) 14] help
3、zookeeper常用四字命令
1)、查看哪个节点被选择作为follower或者leader
[root@datanode1 ~]# echo stat | nc 127.0.0.1 2181Zookeeper version: 3.4.6-1569965, built on 02/20/2014 09:09 GMTClients: /127.0.0.1:51410[0](queued=0,recved=1,sent=0) /192.168.57.137:53366[1](queued=0,recved=3740,sent=3740)Latency min/avg/max: 0/2/1135Received: 9184Sent: 9183Connections: 2Outstanding: 0Zxid: 0x1800001e68Mode: followerNode count: 29[root@datanode1 ~]#
2)、测试是否启用了该server
#若回复imok表示已经启动
[root@datanode1 ~]# echo ruok|nc 127.0.0.1 2181imok[root@datanode1 ~]#
3)、列出未经处理的会话和临时节点
imok[root@datanode1 ~]# echo dump|nc 127.0.0.1 2181SessionTracker dump:org.apache.zookeeper.server.quorum.LearnerSessionTracker@7c6faeb8ephemeral nodes dump:Sessions with Ephemerals (3):0x34ef173456d0006: /storm/supervisors/1f53140e-f9fe-43e3-841d-4ef92fd862f40x14ef17333560003: /storm/supervisors/2ee21b72-3291-41f8-857c-ed8a3a8a70c70x34ef173456d0005: /storm/supervisors/56fa3c25-82c2-46ff-919c-7fbc16d6345d[root@datanode1 ~]#
4)、关掉server
[root@datanode1 ~]# echo kill | nc 127.0.0.1 1281[root@datanode1 ~]#
5)、列出相关服务配置的详细信息
[root@datanode1 ~]# echo conf | nc 127.0.0.1 2181clientPort=2181dataDir=/home/hadoop/modules/zookeeper-data/data/version-2dataLogDir=/home/hadoop/modules/zookeeper-data/logs/version-2tickTime=2000maxClientCnxns=60minSessionTimeout=4000maxSessionTimeout=40000serverId=1initLimit=10syncLimit=5electionAlg=3electionPort=3888quorumPort=2888peerType=0[root@datanode1 ~]#
6)、列出所有连接到服务器的客户端的完全的连接 / 会话的详细信息
[root@datanode1 ~]# echo cons | nc 127.0.0.1 2181 /127.0.0.1:51415[0](queued=0,recved=1,sent=0) /192.168.57.137:53366[1](queued=0,recved=4300,sent=4300,sid=0x14ef17333560003,lop=SETD,est=1438580726687,to=20000,lcxid=0x10c9,lzxid=0x1800002047,lresp=1438586879981,llat=2,minlat=0,avglat=4,maxlat=1101)[root@datanode1 ~]#
7)、输出关于服务环境的详细信息(区别于 conf 命令)
[root@datanode1 ~]# echo envi | nc 127.0.0.1 2181Environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMThost.name=datanode1java.version=1.7.0_67java.vendor=Oracle Corporationjava.home=/usr/java/jdk1.7.0_67/jrejava.class.path=/home/hadoop/modules/zookeeper/bin/../build/classes:/home/hadoop/modules/zookeeper/bin/../build/lib/*.jar:/home/hadoop/modules/zookeeper/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/hadoop/modules/zookeeper/bin/../lib/slf4j-api-1.6.1.jar:/home/hadoop/modules/zookeeper/bin/../lib/netty-3.7.0.Final.jar:/home/hadoop/modules/zookeeper/bin/../lib/log4j-1.2.16.jar:/home/hadoop/modules/zookeeper/bin/../lib/jline-0.9.94.jar:/home/hadoop/modules/zookeeper/bin/../zookeeper-3.4.6.jar:/home/hadoop/modules/zookeeper/bin/../src/java/lib/*.jar:/home/hadoop/modules/zookeeper/bin/../conf:.:/usr/java/jdk1.7.0_67//lib/tools.jarjava.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/libjava.io.tmpdir=/tmpjava.compiler=os.name=Linuxos.arch=amd64os.version=2.6.32-431.el6.x86_64user.name=rootuser.home=/rootuser.dir=/root[root@datanode1 ~]#
8)、列出未经处理的请求
[root@datanode1 ~]# echo reqs | nc 127.0.0.1 2181[root@datanode1 ~]#
9)、列出服务器 watch 的详细信息
[root@datanode1 ~]# echo wchs | nc 127.0.0.1 21810 connections watching 0 pathsTotal watches:0[root@datanode1 ~]#
10)、通过 session 列出服务器 watch 的详细信息,它的输出是一个与 watch 相关的会话的列表
[root@datanode1 ~]# echo wchc | nc 127.0.0.1 2181[root@datanode1 ~]#
11)、通过路径列出服务器 watch 的详细信息
它输出一个与session相关的路径。
[root@datanode1 ~]# echo wchp | nc 127.0.0.1 2181[root@datanode1 ~]#