本文转载自:Zookeeper命令操作
这里转载的是Linux版的操作,MAC版的操作命令基本无异,就是zkServer.sh对应zkServer,zkCli.sh对应zkCli。

一、Zookeeper的四字命令

Zookeeper支持某些特定的四字命令字母与其的交互。他们大多数是查询命令,用来获取Zookeeper服务的当前状态及相关信息。用户在客户端可以通过telnet或nc向Zookeeper提交相应的命令。Zookeeper常用的四字命令见下图所示。

zookeeper命令

上图,是Zookeeper四字命令的一个简单用例。

[root@hadoop ~]# echo ruok|nc localhost 2181
[root@hadoop ~]# zkServer.sh start zoo1.cfg
JMX enabled by default
Using config: /usr/local/zk/bin/../conf/zoo1.cfg
Starting zookeeper ... STARTED
[root@hadoop ~]#  zkServer.sh start zoo2.cfg
JMX enabled by default
Using config: /usr/local/zk/bin/../conf/zoo2.cfg
Starting zookeeper ... STARTED
[root@hadoop ~]#  zkServer.sh start zoo3.cfg
JMX enabled by default
Using config: /usr/local/zk/bin/../conf/zoo3.cfg
Starting zookeeper ... STARTED
[root@hadoop ~]# echo ruok|nc localhost 2181
imok[root@hadoop ~]# echo ruok|nc localhost 2182
imok[root@hadoop ~]# echo ruok|nc localhost 2183
imok[root@hadoop ~]# echo conf|nc localhost 2181
clientPort=2181
dataDir=/usr/local/zk/data_1/version-2
dataLogDir=/usr/local/zk/logs_1/version-2
tickTime=2000
maxClientCnxns=60
minSessionTimeout=4000
maxSessionTimeout=40000
serverId=0
initLimit=10
syncLimit=5
electionAlg=3
electionPort=3387
quorumPort=2287
peerType=0
[root@hadoop ~]#

二、Zookeeper的简单操作

2.1 Zookeeper的shell操作

再启动Zookeeper服务之后,输入以下命令,连接到Zookeeper服务:

zkCli.sh -server localhost:2181

执行结果如下所示:

[root@hadoop ~]# zkCli.sh -server localhost:2181
Connecting to localhost:2181
2014-10-17 03:35:51,051 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT
2014-10-17 03:35:51,055 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=hadoop
2014-10-17 03:35:51,057 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.6.0_24
2014-10-17 03:35:51,057 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Sun Microsystems Inc.
2014-10-17 03:35:51,066 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/local/jdk/jre
2014-10-17 03:35:51,079 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/usr/local/zk/bin/../build/classes:/usr/local/zk/bin/../build/lib/*.jar:/usr/local/zk/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zk/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zk/bin/../lib/netty-3.2.2.Final.jar:/usr/local/zk/bin/../lib/log4j-1.2.15.jar:/usr/local/zk/bin/../lib/jline-0.9.94.jar:/usr/local/zk/bin/../zookeeper-3.4.5.jar:/usr/local/zk/bin/../src/java/lib/*.jar:/usr/local/zk/bin/../conf:
2014-10-17 03:35:51,083 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/local/jdk/jre/lib/i386/client:/usr/local/jdk/jre/lib/i386:/usr/local/jdk/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib
2014-10-17 03:35:51,084 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2014-10-17 03:35:51,086 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2014-10-17 03:35:51,099 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2014-10-17 03:35:51,100 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=i386
2014-10-17 03:35:51,101 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=2.6.32-358.el6.i686
2014-10-17 03:35:51,101 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
2014-10-17 03:35:51,102 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
2014-10-17 03:35:51,106 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/root
2014-10-17 03:35:51,120 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@b02e7a
Welcome to ZooKeeper!
JLine support is enabled
2014-10-17 03:35:51,233 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@966] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (Unable to locate a login configuration)
2014-10-17 03:35:51,247 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@849] - Socket connection established to localhost/127.0.0.1:2181, initiating session
[zk: localhost:2181(CONNECTING) 0] 2014-10-17 03:35:51,290 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1207] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x491da0e20b0000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

[zk: localhost:2181(CONNECTED) 0]

连接成功之后,系统会输出Zookeeper的相关环境及配置信息,并在屏幕输出“welcome to Zookeeper!”等信息。输入help之后,屏幕会输出可用的Zookeeper命令,如下图所示

zookeeper-help

使用Zookeeper命令的简单操作步骤:

(1) 使用ls命令查看当前Zookeeper中所包含的内容:ls /

[zk: localhost:2181(CONNECTED) 1] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 2]

(2) 创建一个新的Znode节点”zk”,以及和它相关字符,执行命令:create /zk myData

[zk: localhost:2181(CONNECTED) 2] create /zk myData
Created /zk

(3) 再次使用ls命令来查看现在Zookeeper的中所包含的内容:ls /

[zk: localhost:2181(CONNECTED) 3] ls /
[zk, zookeeper]

此时看到,zk节点已经被创建。  

(4) 使用get命令来确认第二步中所创建的Znode是否包含我们创建的字符串,执行命令:get /zk

[zk: localhost:2181(CONNECTED) 4] get /zk
myData
cZxid = 0x500000006
ctime = Fri Oct 17 03:54:20 PDT 2014
mZxid = 0x500000006
mtime = Fri Oct 17 03:54:20 PDT 2014
pZxid = 0x500000006
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0

(5) 接下来通过set命令来对zk所关联的字符串进行设置,执行命令:set /zk jiang1234

[zk: localhost:2181(CONNECTED) 5] set /zk jiang2014
cZxid = 0x500000006
ctime = Fri Oct 17 03:54:20 PDT 2014
mZxid = 0x500000007
mtime = Fri Oct 17 03:55:50 PDT 2014
pZxid = 0x500000006
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0

(6) 再次使用get命令来查看,上次修改的内容,执行命令:get /zk

[zk: localhost:2181(CONNECTED) 6] get /zk
jiang2014
cZxid = 0x500000006
ctime = Fri Oct 17 03:54:20 PDT 2014
mZxid = 0x500000007
mtime = Fri Oct 17 03:55:50 PDT 2014
pZxid = 0x500000006
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 9
numChildren = 0

(7) 下面我们将刚才创建的Znode删除,执行命令:delete /zk

[zk: localhost:2181(CONNECTED) 7] delete /zk

(8) 最后再次使用ls命令查看Zookeeper中的内容,执行命令:ls /

[zk: localhost:2181(CONNECTED) 8] ls /
[zookeeper]

经过验证,zk节点已经删除。


2.2 Zookeeper的api的简单使用

ZookeeperAPI简介

Zookeeper API共包含五个包,分别为:

  (1)org.apache.zookeeper
  (2)org.apache.zookeeper.data
  (3)org.apache.zookeeper.server
  (4)org.apache.zookeeper.server.quorum
  (5)org.apache.zookeeper.server.upgrade

其中org.apache.zookeeper,包含Zookeeper类,他是我们编程时最常用的类文件。这个类是Zookeeper客户端的主要类文件。如果要使用Zookeeper服务,应用程序首先必须创建一个Zookeeper实例,这时就需要使用此类。一旦客户端和Zookeeper服务建立起了连接,Zookeeper系统将会给次连接会话分配一个ID值,并且客户端将会周期性的向服务器端发送心跳来维持会话连接。只要连接有效,客户端就可以使用Zookeeper API来做相应处理了。

Zookeeper类提供了如下图所示的几类主要方法:

zookeeper-api

Zookeeper API的使用

这里通过一个例子来简单介绍如何使用Zookeeper API 编写自己的应用程序,代码如下:

package org.zk;

import java.io.IOException;
import java.util.List;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;

public class ListGroup extends ConnectionWatcher {
    public void list(String groupNmae) throws KeeperException, InterruptedException{
        String path ="/"+groupNmae;
        try {
            List<String> children = zk.getChildren(path, false);
            if(children.isEmpty()){
                System.out.printf("No memebers in group %s\n",groupNmae);
                System.exit(1);
            }
            for(String child:children){
                System.out.println(child);
            }
        } catch (KeeperException.NoNodeException e) {
            System.out.printf("Group %s does not exist \n", groupNmae);
            System.exit(1);
        } 
    }
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        ListGroup listGroup = new ListGroup();
        listGroup.connect(args[0]);
        listGroup.list(args[1]);
        listGroup.close();
    }
}

此类包含两个主要的 ZooKeeper 函数,分别为 createZKInstance ()和 ZKOperations()。其中:

(1) createZKInstance()函数负责对 ZooKeeper 实例 zk 进行初始化。

ZooKeeper 类有两个构造函数,我们这里使用“ ZooKeeper (String connectString, int sessionTimeout ,Watcher watcher )”对其进行初始化。因此,我们需要提供初始化所需的,连接字符串信息,会话超时时间,以及一个 watcher 实例。 19行到 25行代码,是程序所构造的一个watcher 实例,它能够输出所发生的事件。

(2) ZKOperations ()函数是我们所定义的对节点的一系列操作。

它包括:创建 ZooKeeper 节点( 35行到 36行代码)、查看节点( 38 行到 39 行代码)、修改节点数据( 41 行到 42 行代码)、查看修改后节点数据( 44 行到 45行代码)、删除节点( 47行到 48行代码)、查看节点是否存在( 50 行到 51 行代码)。

代码的运行结果如下:

1. 创建ZooKeeper节点(Znode:/znode;数据:myData2;权限:OPEN_ACL_UNSAFE;节点类型:Persistent)
 None
2. 查看节点是否创建成功:
 /znode myData2
3. 修改节点数据:
4. 查看是否修改成功:
 jiang1234
5. 删除节点:
6. 查看/znode节点状态:
 节点间状态:[null]

更多zookeeper的Java操作可以查看原文或者官方文档
(http://www.cnblogs.com/sunddenly/p/4031881.html)
官方文档:https://zookeeper.apache.org/doc/current/javaExample.html

注:官方文档里有zookeeper的相关API文档和Java操作示例

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐