Hadoop生态圈-zookeeper本地搭建以及常用命令介绍
Hadoop生态圈-zookeeper本地搭建以及常用命令介绍
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.下载zookeeper软件
下载地址:https://www.apache.org/dyn/closer.cgi/zookeeper/
二.安装zookeeper
1>.解压源文件
[yinzhengjie@s101 ~]$ tar zxf zookeeper-3.4.12.tar.gz -C /soft/
2>.创建软连接
[yinzhengjie@s101 ~]$ ln -s /soft/zookeeper-3.4.12/ /soft/zk
3>.配置环境变量
[yinzhengjie@s101 ~]$ tail -3 /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 2181 (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 0 0 :::2181 :::* LISTEN 2377/java [yinzhengjie@s101 ~]$
4>.启动成功后zookeeper有个QuorumPeerMain进程
[yinzhengjie@s101 ~]$ jps 2377 QuorumPeerMain 2396 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即可)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[yinzhengjie@s101 ~]$ zkCli.sh -server localhost:2181 Connecting to localhost:2181 2018-05-28 20:36:33,351 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.12-e5259e437540f349646870ea94dc2658c4e44b3b, built on 03/27/2018 03:55 GMT 2018-05-28 20:36:33,354 [myid:] - INFO [main:Environment@100] - Client environment:host.name=s101 2018-05-28 20:36:33,354 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_131 2018-05-28 20:36:33,356 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation 2018-05-28 20:36:33,356 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/soft/jdk1.8.0_131/jre 2018-05-28 20:36:33,356 [myid:] - INFO [main:Environment@100] - 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]
2>.查看帮助信息(其实你输入任何一条没有特殊含义的指令都会弹出帮助信息哟)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[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]
3>.列出"/"下的节点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[zk: localhost:2181(CONNECTED) 2] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 3]
4>.创建“name”节点,并添加数据“yinzhengjie”
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[zk: localhost:2181(CONNECTED) 3] create /name yinzhengjie Created /name [zk: localhost:2181(CONNECTED) 4]
5>.创建空节点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[zk: localhost:2181(CONNECTED) 4] create /hadoop '' Created /hadoop [zk: localhost:2181(CONNECTED) 5]
6>.查看“/name”节点的数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[zk: localhost:2181(CONNECTED) 5] get /name yinzhengjie cZxid = 0x900000009 //(create)节点创建事务的id ctime = Thu May 31 08:47:48 CST 2018 //创建时间 mZxid = 0x900000009 //(modify)节点修改事务的id mtime = Thu May 31 08:47:48 CST 2018 //(modify)时间,当当前节点被修改时时间会跟着变化 pZxid = 0x900000009 //最新子节点的CZxid cversion = 0 //创建的版本号 dataVersion = 0 //删除时指定此版本 aclVersion = 0 //ACL : Access Control List ephemeralOwner = 0x0 //临时拥有者 dataLength = 0 //数据长度 numChildren = 0 //子节点个数 [zk: localhost:2181(CONNECTED) 6]
7>.设置根节点数据为“yinzhengjie”
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[zk: localhost:2181(CONNECTED) 6] set / yinzhengjie cZxid = 0x0 ctime = Wed Dec 31 16:00:00 PST 1969 mZxid = 0xa mtime = Mon May 28 20:43:47 PDT 2018 pZxid = 0x9 cversion = 1 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 11 numChildren = 3 [zk: localhost:2181(CONNECTED) 7]
8>.删除单个节点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[zk: localhost:2181(CONNECTED) 7] ls / [name, hadoop, zookeeper] [zk: localhost:2181(CONNECTED) 8] ls /hadoop [] [zk: localhost:2181(CONNECTED) 9] delete /hadoop [zk: localhost:2181(CONNECTED) 10] [zk: localhost:2181(CONNECTED) 10] ls / [name, zookeeper] [zk: localhost:2181(CONNECTED) 11]
9>.递归删除节点
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[zk: localhost:2181(CONNECTED) 17] ls / [name, zookeeper] [zk: localhost:2181(CONNECTED) 18] [zk: localhost:2181(CONNECTED) 18] create /name/zookeeper zk Created /name/zookeeper [zk: localhost:2181(CONNECTED) 19] ls / [name, zookeeper] [zk: localhost:2181(CONNECTED) 20] ls /name [zookeeper] [zk: localhost:2181(CONNECTED) 21] [zk: localhost:2181(CONNECTED) 21] rmr /name [zk: localhost:2181(CONNECTED) 22] [zk: localhost:2181(CONNECTED) 22] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 23]
10>.退出客户端程序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[zk: localhost:2181(CONNECTED) 23] quit Quitting... 2018-05-28 20:50:21,796 [myid:] - INFO [main:ZooKeeper@687] - Session: 0x10000b217fa0004 closed 2018-05-28 20:50:21,798 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@521] - EventThread shut down for session: 0x10000b217fa0004 [yinzhengjie@s101 ~]$ [yinzhengjie@s101 ~]$
六.配置文件简介
1>.日志文件(zookeeper.out)
2>.核心配置文件(/soft/zk/conf/zoo.cfg)