ZooKeeper的使用---Java程式

來源:互聯網
上載者:User

標籤:time   節點   data   字串   proc   pre   tab   encoding   通過   

一、匯入庫
  1. 以下庫存放在目錄lib中:
audience-annotations-0.5.0.jarjline-0.9.94.jarlog4j-1.2.17.jarnetty-3.10.6.Final.jarslf4j-api-1.7.25.jarslf4j-log4j12-1.7.25.jar 
  1. 以下庫存放在zookeeper安裝的根目錄中:
zookeeper-3.4.12.jar 二、ZooKeeper對象的操作
  1. zookeeper對象的建立
String connectString = "hadoop1:2181,hadoop2:2181,hadoop3:2181”;int sessionTimeout = 2000; ZooKeeper zookeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {@Overridepublic void process(WatchedEvent event) {...}});
其中:connectString是串連zookeeper的資訊sessionTimeout是會話逾時時間,也就是當一個zookeeper超過該時間沒有心跳,則認為該節點故障。Watcher是一個監控器,一旦觸發監控就會回調process方法,觸發的事件會存在event對象中。 
  1. ZooKeeper對象的常用方法
 
方法名 傳回值 作用 各參數 備忘
String create(final String path, byte data[], List<ACL> acl,CreateMode createMode) 傳回值是path的字串 建立節點
  1. path是建立節點路徑
  2. Data是節點中的值
  3. Acl是許可權,可以通過Ids類的枚舉擷取
  4. createMode是建立的類型,有四種,分別為:永久,永久序列化,短暫,短暫序列化。
 
Stat setData(final String path, byte data[], int version) 節點狀態物件,裡麵包含節點的各種資訊,包括節點更改的版本,節點建立的日期等等。 設定節點的資料
  1. path是修改資料的節點路徑
  2. data是修改之後的值
  3. version是修改哪個版本的節點,-1表示所有節點
 
List<String> getChildren(String path, boolean watch) 節點下子節點集合,只有節點名稱,不是全路徑 擷取子節點列表
  1. path是父節點路徑。
  2. watch是是否監控子節點變化情況
 
byte[] getData(String path, boolean watch, Stat stat) 該節點的值 擷取該節點的值
  1. path是節點路徑
  2. watch是是否監控節點的資料變化
  3. Stat是明確是哪個狀態的節點,可以設定為null.
 
void delete(final String path, int version)   刪除節點
  1. path是節點路徑
  2. version是節點版本,-1表示刪除該路徑所有版本的節點
 
 

ZooKeeper的使用---Java程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.