Hadoop生态圈-zookeeper本地搭建以及常用命令介绍

时间:2023-12-04 20:38:56

                Hadoop生态圈-zookeeper本地搭建以及常用命令介绍

                                            作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.下载zookeeper软件

  下载地址:https://www.apache.org/dyn/closer.cgi/zookeeper/

Hadoop生态圈-zookeeper本地搭建以及常用命令介绍

二.安装zookeeper

1>.解压源文件

[yinzhengjie@s101 ~]$ tar zxf zookeeper-3.4..tar.gz -C /soft/

2>.创建软连接

[yinzhengjie@s101 ~]$ ln -s /soft/zookeeper-3.4./ /soft/zk

3>.配置环境变量

[yinzhengjie@s101 ~]$ tail - /etc/profile
#ADD zooKeeper
ZOOKEEPER=/soft/zk
PATH=$PATH:$ZOOKEEPER/bin
[yinzhengjie@s101 ~]$

4>.使配置的环境变量生效

[yinzhengjie@s101 ~]$ source /etc/profile

三.启动zookeeper服务

1>.创建出zookeeper配置文件(我们这里直接把配置文件拷贝过来就好)

[yinzhengjie@s101 ~]$ cp /soft/zk/conf/zoo_sample.cfg /soft/zk/conf/zoo.cfg

2>.启动zookeeper

[yinzhengjie@s101 ~]$ zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /soft/zk/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[yinzhengjie@s101 ~]$

3>.检查是否启动成功

[yinzhengjie@s101 ~]$ netstat -untalp | grep
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp6 ::: :::* LISTEN /java
[yinzhengjie@s101 ~]$

4>.启动成功后zookeeper有个QuorumPeerMain进程

[yinzhengjie@s101 ~]$ jps
QuorumPeerMain
Jps
[yinzhengjie@s101 ~]$

四.zookeeper服务端脚本用法

1>.查看服务的状态,我们这是本地模式,显示的状态为standalone

[yinzhengjie@s101 ~]$ zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /soft/zk/bin/../conf/zoo.cfg
Mode: standalone
[yinzhengjie@s101 ~]$

2>.重启zookeeper服务

[yinzhengjie@s101 ~]$ zkServer.sh restart
ZooKeeper JMX enabled by default
Using config: /soft/zk/bin/../conf/zoo.cfg
ZooKeeper JMX enabled by default
Using config: /soft/zk/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
ZooKeeper JMX enabled by default
Using config: /soft/zk/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[yinzhengjie@s101 ~]$

3>.停止zookeeper服务

[yinzhengjie@s101 ~]$ zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /soft/zk/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[yinzhengjie@s101 ~]$

4>.zookeeper服务端的其他用法

[yinzhengjie@s101 ~]$ zkServer.sh
ZooKeeper JMX enabled by default
Using config: /soft/zk/bin/../conf/zoo.cfg
Usage: /soft/zk/bin/zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}
[yinzhengjie@s101 ~]$

五.zookeeper客户端脚本用法

1>.连接客户端(连接成功后需要敲回车执行你需要执行的命令哟!,如果只连接本地的zookeeper的话,直接在命令行输入zkCli.sh即可)

[yinzhengjie@s101 ~]$ zkCli.sh -server localhost:
Connecting to localhost:
-- ::, [myid:] - INFO [main:Environment@] - Client environment:zookeeper.version=3.4.-e5259e437540f349646870ea94dc2658c4e44b3b, built on // : GMT
-- ::, [myid:] - INFO [main:Environment@] - Client environment:host.name=s101
-- ::, [myid:] - INFO [main:Environment@] - Client environment:java.version=1.8.0_131
-- ::, [myid:] - INFO [main:Environment@] - Client environment:java.vendor=Oracle Corporation
-- ::, [myid:] - INFO [main:Environment@] - Client environment:java.home=/soft/jdk1..0_131/jre
-- ::, [myid:] - INFO [main:Environment@] - Client environment:java.class.path=/soft/zk/bin/../build/classes:/soft/zk/bin/../build/lib/*.jar:/soft/zk/bin/../lib/slf4j-log4j12-1.7.25.jar:/soft/zk/bin/../lib/slf4j-api-1.7.25.jar:/soft/zk/bin/../lib/netty-3.10.6.Final.jar:/soft/zk/bin/../lib/log4j-1.2.17.jar:/soft/zk/bin/../lib/jline-0.9.94.jar:/soft/zk/bin/../lib/audience-annotations-0.5.0.jar:/soft/zk/bin/../zookeeper-3.4.12.jar:/soft/zk/bin/../src/java/lib/*.jar:/soft/zk/bin/../conf:
2018-05-28 20:36:33,356 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-05-28 20:36:33,357 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-05-28 20:36:33,357 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2018-05-28 20:36:33,357 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2018-05-28 20:36:33,357 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2018-05-28 20:36:33,357 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0-327.el7.x86_64
2018-05-28 20:36:33,357 [myid:] - INFO [main:Environment@100] - Client environment:user.name=yinzhengjie
2018-05-28 20:36:33,357 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/home/yinzhengjie
2018-05-28 20:36:33,357 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/home/yinzhengjie
2018-05-28 20:36:33,358 [myid:] - INFO [main:ZooKeeper@441] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@277050dc
Welcome to ZooKeeper!
2018-05-28 20:36:33,378 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1028] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2018-05-28 20:36:33,439 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@878] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session
2018-05-28 20:36:33,446 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1302] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x10000b217fa0001, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]
[zk: localhost:2181(CONNECTED) 0]

[yinzhengjie@s101 ~]$ zkCli.sh -server localhost:2181

2>.查看帮助信息(其实你输入任何一条没有特殊含义的指令都会弹出帮助信息哟)

[zk: localhost:(CONNECTED) ] 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:(CONNECTED) ]

[zk: localhost:2181(CONNECTED) 0] help

3>.列出"/"下的节点

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

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

4>.创建“name”节点,并添加数据“yinzhengjie”

[zk: localhost:(CONNECTED) ] create /name yinzhengjie
Created /name
[zk: localhost:(CONNECTED) ]

[zk: localhost:2181(CONNECTED) 3] create /name yinzhengjie

5>.创建空节点

[zk: localhost:(CONNECTED) ] create /hadoop ''
Created /hadoop
[zk: localhost:(CONNECTED) ]

[zk: localhost:2181(CONNECTED) 4] create /hadoop ''

6>.查看“/name”节点的数据

[zk: localhost:(CONNECTED) ] get /name
yinzhengjie
cZxid = 0x900000009 //(create)节点创建事务的id
ctime = Thu May :: CST //创建时间
mZxid = 0x900000009 //(modify)节点修改事务的id
mtime = Thu May :: CST //(modify)时间,当当前节点被修改时时间会跟着变化
pZxid = 0x900000009 //最新子节点的CZxid
cversion = //创建的版本号
dataVersion = //删除时指定此版本
aclVersion = //ACL : Access Control List
ephemeralOwner = 0x0 //临时拥有者
dataLength = //数据长度
numChildren = //子节点个数
[zk: localhost:(CONNECTED) ]

[zk: localhost:2181(CONNECTED) 5] get /name

7>.设置根节点数据为“yinzhengjie”

[zk: localhost:(CONNECTED) ] set / yinzhengjie
cZxid = 0x0
ctime = Wed Dec :: PST
mZxid = 0xa
mtime = Mon May :: PDT
pZxid = 0x9
cversion =
dataVersion =
aclVersion =
ephemeralOwner = 0x0
dataLength =
numChildren =
[zk: localhost:(CONNECTED) ]

[zk: localhost:2181(CONNECTED) 6] set / yinzhengjie

8>.删除单个节点

[zk: localhost:(CONNECTED) ] ls /
[name, hadoop, zookeeper]
[zk: localhost:(CONNECTED) ] ls /hadoop
[]
[zk: localhost:(CONNECTED) ] delete /hadoop
[zk: localhost:(CONNECTED) ]
[zk: localhost:(CONNECTED) ] ls /
[name, zookeeper]
[zk: localhost:(CONNECTED) ]

[zk: localhost:2181(CONNECTED) 9] delete /hadoop

9>.递归删除节点

[zk: localhost:(CONNECTED) ] ls /
[name, zookeeper]
[zk: localhost:(CONNECTED) ]
[zk: localhost:(CONNECTED) ] create /name/zookeeper zk
Created /name/zookeeper
[zk: localhost:(CONNECTED) ] ls /
[name, zookeeper]
[zk: localhost:(CONNECTED) ] ls /name
[zookeeper]
[zk: localhost:(CONNECTED) ]
[zk: localhost:(CONNECTED) ] rmr /name
[zk: localhost:(CONNECTED) ]
[zk: localhost:(CONNECTED) ] ls /
[zookeeper]
[zk: localhost:(CONNECTED) ]

[zk: localhost:2181(CONNECTED) 21] rmr /name

10>.退出客户端程序

[zk: localhost:(CONNECTED) ] quit
Quitting...
-- ::, [myid:] - INFO [main:ZooKeeper@] - Session: 0x10000b217fa0004 closed
-- ::, [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@] - EventThread shut down for session: 0x10000b217fa0004
[yinzhengjie@s101 ~]$
[yinzhengjie@s101 ~]$

[zk: localhost:2181(CONNECTED) 23] quit

六.配置文件简介

1>.日志文件(zookeeper.out)

Hadoop生态圈-zookeeper本地搭建以及常用命令介绍

2>.核心配置文件(/soft/zk/conf/zoo.cfg)

Hadoop生态圈-zookeeper本地搭建以及常用命令介绍