1. 程式人生 > 實用技巧 >Zookeeper的安裝和基本操作

Zookeeper的安裝和基本操作

Zookeeper概念

Zookeeper是Apache Hadoop專案下的一個子專案,是一個樹形目錄服務

Zookeeper翻譯過來就是動物園管理員,他是用來管Hadoop(大象)、Hive(蜜蜂)、Pig(小豬)的管理員。簡稱zk

Zookeeper是一個分散式的、開源的分散式應用程式的協調服務。

Zookeeper提供的主要功能包括:

  1. 配置管理
  2. 分散式鎖
  3. 叢集管理(註冊中心)

Zookeeper的安裝(linux)

  1. 上傳zookeeper壓縮包
  2. 將壓縮包移動到 /opt/zookeeper 目錄下
      mv /root/apache-zookeeper-3.5.8-bin.tar.gz /opt/zookeeper/
  3. 解壓壓縮包
      tar -zxvf apache-zookeeper-3.5.8-bin.tar.gz
  4. 修改配置檔案
    1. 進入到conf目錄
      cd /opt/zookeeper/apache-zookeeper-3.5.8-bin/conf/
    2. 拷貝
      cp zoo_sample.cfg zoo.cfg (這一步必做,zookeeper的配置檔案的名稱是zoo才會生效
    3. 修改儲存目錄:dataDir=/opt/zookeeper/zkdata
      [root@zyh conf]# vim zoo.cfg 
      # The number of milliseconds of each tick
      tickTime=2000
      # The number of ticks that the initial
      # synchronization phase can take
      initLimit
      =10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/opt/zookeeper/zkdata # the port at which the clients will connect clientPort
      =2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1 ~ ~ "zoo.cfg" 28L, 929C
  5. 啟動zookeeper
    cd /opt/zooKeeper/apache-zookeeper-3.5.8-bin/bin/
    啟動
    ./zkServer.sh start
    檢視ZooKeeper狀態
    ./zkServer.sh status
    停止
    ./zkServer.sh stop

zookeeper資料模型

  • ZooKeeper是一個樹形目錄服務,其資料模型和Unix的檔案系統目錄樹很類似,擁有一個層次化結構。

  • 這裡面的每一個節點都被稱為:ZNode,每個節點上都會儲存自己的資料和節點資訊。
  • 節點可以擁有子節點,同時也允許少量(1MB)資料儲存在該節點之下。

  • 節點可以分為四大類:

    PERSISTENT持久化節點
    EPHEMERAL臨時結點:-e
    PERSISTENT SEQUENTIAL持久化順序節點:-s
    EPHEMERAL SEQUENTIAL臨時順序節點:-es

Zookeeper命令

Zookeeper服務端常用命令


啟動ZooKeeper服務:/zkServer.sh start
檢視ZooKeeper服務狀態:/zkServer.sh status
停止ZooKeeper服務:/zkServer.sh stop
重啟ZooKeeper服務:./zkServer.sh restart

Zookeeper客戶端常用命令

連線ZooKeeper服務端
./zkCli.sh -server ip:port (連線本機的時候可以省略-server ip:port)


斷開連線
quit

檢視命令幫助
help

顯示指定目錄下節點
ls 目錄


設定節點值
set /節點path value


刪除單個節點
delete /節點path


刪除帶有子節點的節點
deleteall /節點path


建立節點
create /節點path value


獲取節點值
get /節點path

建立臨時節點

create -e /節點path value

退出

quit (再次使用zk-cli連線服務端,驗證剛才建立的臨時節點已經沒了)

建立順序節點,zk會自動在節點路徑後邊新增序號

create -s /節點path value

查詢節點詳細資訊

ls –s /節點path

詳細資訊如下:

•czxid:節點被建立的事務ID

•ctime: 建立時間

•mZxid: 最後一次被更新的事務ID

•mtime: 修改時間

•pzxid:子節點列表最後一次被更新的事務ID

•cversion:子節點的版本號

•dataversion:資料版本號

•aclversion:許可權版本號

•ephemeralOwner:用於臨時節點,代表臨時節點的事務ID,如果為持久節點則為0

•dataLength:節點儲存的資料的長度

•numChildren:當前節點的子節點個數

ZooKeeper JavaAPI 操作(Curator內容過多重開一篇博文)