Hadoop基础教程-第8章 Zookeeper(8.5 Zookeeper内存数据库)

2022-05-06 18:51:46 浏览数 (1)

第8章 Zookeeper

8.5 Zookeeper内存数据库

8.5.2 内存数据库介绍

ZooKeeper的数据模型是一棵树,而从使用角度看, Zookeeper就像一个内存数据库一样。在这个内存数据库中,存储了整棵树的内容,包括所有的节点路径、节点数据及其ACL信息等,Zookeeper会定时将这个数据存储到磁盘上。 Zookeeper内存数据库是通过ZKDatabase类实现, 内部属性包括dataTree,sessionsWithTimeouts,snapLog,commitedLog。

DateTree是整个zookeeper内存数据库的核心,代表了内存中一份完整的数据;

数据模型和层次命名空间 ZooKeeper提供的名称空间与标准文件系统类似。名称是以斜杠(/)分隔的路径元素序列。ZooKeeper的名称空间中的每个节点都由路径标识。

8.5.2 zkCli.sh命令

连接本地zookeeper

代码语言:javascript复制
[root@node1 ~]# zkCli.sh
Connecting to localhost:2181
2017-07-08 04:30:45,293 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
2017-07-08 04:30:45,312 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=node1
2017-07-08 04:30:45,312 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_112
2017-07-08 04:30:45,315 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-07-08 04:30:45,320 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/opt/jdk1.8.0_112/jre
2017-07-08 04:30:45,320 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/opt/zookeeper-3.4.10/bin/../build/classes:/opt/zookeeper-3.4.10/bin/../build/lib/*.jar:/opt/zookeeper-3.4.10/bin/../lib/slf4j-log4j12-1.6.1.jar:/opt/zookeeper-3.4.10/bin/../lib/slf4j-api-1.6.1.jar:/opt/zookeeper-3.4.10/bin/../lib/netty-3.10.5.Final.jar:/opt/zookeeper-3.4.10/bin/../lib/log4j-1.2.16.jar:/opt/zookeeper-3.4.10/bin/../lib/jline-0.9.94.jar:/opt/zookeeper-3.4.10/bin/../zookeeper-3.4.10.jar:/opt/zookeeper-3.4.10/bin/../src/java/lib/*.jar:/opt/zookeeper-3.4.10/bin/../conf:.::/opt/jdk1.8.0_112/lib
2017-07-08 04:30:45,321 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2017-07-08 04:30:45,321 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2017-07-08 04:30:45,321 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2017-07-08 04:30:45,321 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux
2017-07-08 04:30:45,321 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2017-07-08 04:30:45,322 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=3.10.0-514.el7.x86_64
2017-07-08 04:30:45,322 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root
2017-07-08 04:30:45,322 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root
2017-07-08 04:30:45,322 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/root
2017-07-08 04:30:45,324 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@506c589e
2017-07-08 04:30:45,401 [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!
JLine support is enabled
2017-07-08 04:30:45,627 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@876] - Socket connection established to localhost/127.0.0.1:2181, initiating session
2017-07-08 04:30:45,663 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x15d214ff93a0002, negotiated timeout = 30000

WATCHER::

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

输入help查看所有支持的命令

代码语言:javascript复制
[zk: localhost:2181(CONNECTED) 0] 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
[zk: localhost:2181(CONNECTED) 1] 

查看当前节点列表

代码语言:javascript复制
[zk: localhost:2181(CONNECTED) 1] ls /
[zookeeper]

创建一个新的 znode,使用 create /test myData 。这个命令创建了一个新的 znode 节点“test ”以及与它关联的字符串myData:

代码语言:javascript复制
[zk: localhost:2181(CONNECTED) 2] create /test myData
Created /test
[zk: localhost:2181(CONNECTED) 3] ls /
[zookeeper, test]

使用get 命令来查看znode

代码语言:javascript复制
[zk: localhost:2181(CONNECTED) 4] get /test
myData
cZxid = 0x200000008
ctime = Sat Jul 08 04:43:08 EDT 2017
mZxid = 0x200000008
mtime = Sat Jul 08 04:43:08 EDT 2017
pZxid = 0x200000008
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 6
numChildren = 0
[zk: localhost:2181(CONNECTED) 5] 

通过 set 命令来对 zk 所关联的字符串进行设置

代码语言:javascript复制
[zk: localhost:2181(CONNECTED) 5] set /test testDemo
cZxid = 0x200000008
ctime = Sat Jul 08 04:43:08 EDT 2017
mZxid = 0x200000009
mtime = Sat Jul 08 04:46:35 EDT 2017
pZxid = 0x200000008
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
[zk: localhost:2181(CONNECTED) 6] get /test           
testDemo
cZxid = 0x200000008
ctime = Sat Jul 08 04:43:08 EDT 2017
mZxid = 0x20000000b
mtime = Sat Jul 08 04:47:12 EDT 2017
pZxid = 0x200000008
cversion = 0
dataVersion = 3
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 8
numChildren = 0
[zk: localhost:2181(CONNECTED) 7] 

创建子目录

代码语言:javascript复制
[zk: localhost:2181(CONNECTED) 7] create /test/node1 node1
Created /test/node1
[zk: localhost:2181(CONNECTED) 8] ls /test                
[node1]
[zk: localhost:2181(CONNECTED) 9] 

删除刚才创建的 znode

代码语言:javascript复制
[zk: localhost:2181(CONNECTED) 9] delete /test/node1
[zk: localhost:2181(CONNECTED) 10] ls /test
[]

退出命令

代码语言:javascript复制
[zk: localhost:2181(CONNECTED) 11] quit
Quitting...
2017-07-08 04:50:40,834 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x15d214ff93a0003 closed
2017-07-08 04:50:40,837 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x15d214ff93a0003
[root@node1 ~]# 

0 人点赞