18 大資料zookeeper --使用java api操作zookeeper
阿新 • • 發佈:2019-01-12
ZooKeeper服務命令:
在準備好相應的配置之後,可以直接通過zkServer.sh 這個指令碼進行服務的相關操作
1. 啟動ZK服務: sh bin/zkServer.sh start
2. 檢視ZK服務狀態: sh bin/zkServer.sh status
3. 停止ZK服務: sh bin/zkServer.sh stop
4. 重啟ZK服務: sh bin/zkServer.sh restart
zk客戶端命令
ZooKeeper命令列工具類似於Linux的shell環境,我們可以簡單的對ZooKeeper進行訪問,資料建立,資料修改等操作. 使用 zkCli.sh -server 127.0.0.1:2181 連線到 ZooKeeper 服務,連線成功後,系統會輸出 ZooKeeper 的相關環境以及配置資訊。
命令列工具的一些簡單操作如下:
1. 顯示根目錄下、檔案: ls / 使用 ls 命令來檢視當前 ZooKeeper 中所包含的內容 2. 顯示根目錄下、檔案: ls2 / 檢視當前節點資料並能看到更新次數等資料 3. 建立檔案,並設定初始內容: create /zk "test" 建立一個新的 znode節點“ zk ”以及與它關聯的字串 4. 獲取檔案內容: get /zk 確認 znode 是否包含我們所建立的字串 5. 修改檔案內容: set /zk "zkbak" 對 zk 所關聯的字串進行設定 6. 刪除檔案: delete /zk 將剛才建立的 znode 刪除 7. 退出客戶端: quit 8. 幫助命令: help
建立maven專案,匯入zookeeper相關包
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.7</version>
</dependency>
</dependencies>
1、連線zookeeper
public class TestCase { private static final String connectionString = "192.168.25.127:2181," + "192.168.25.129:2181," + "192.168.25.130:2181"; public static final Integer sessionTimeout = 2000; public static ZooKeeper zkClient = null; @Before public void init() throws Exception{ //三個引數分別為連線的zookeeper叢集伺服器的ip,超時時間,監聽器 zkClient = new ZooKeeper(connectionString, sessionTimeout, new Watcher(){ //收到事件通知後的回撥函式(應該是我們自己的事件處理邏輯) public void process(WatchedEvent event) { System.out.println(event.getType()+","+event.getPath()); }}); }
命令列檢視
[zk: localhost:2181(CONNECTED) 1] ls /
[app1, idea, test, servers, zookeeper]
2、建立資料節點到zk中
@Test
public void createNode() throws Exception{
/*
* 傳入四個引數
* 1、建立的節點
* 2、節點資料
* 3、節點的許可權,OPEN_ACL_UNSAFE表示內部應用許可權
* 4、節點型別,4種:持久化節點,帶序列持久化節點,臨時節點,帶序列的臨時節點
*/
String path = zkClient.create("/idea",
"helloworld".getBytes(),
Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
System.out.println(path);
}
3、獲取子節點
@Test
public void getChildren() throws Exception{
/*
* 傳入2個引數
* 1、指定獲取哪個節點的孩子
* 2、是否使用監聽器(watcher),true表示使用以上的監聽功能
*/
List<String> children = zkClient.getChildren("/",true);
for (String child : children) {
System.out.println(child);
}
System.in.read();
}
控制檯輸出
None,null
app1
idea
test
zookeeper
servers
由於使用了監聽功能,那麼可以測試下監聽功能
使用命令列刪除掉節點/idea
控制檯會繼續輸出
None,null
app1
idea
test
zookeeper
servers
NodeChildrenChanged,/ 繼續輸出的內容
但是如果再建立該節點,控制檯不會再輸出任何內容,這是因為監聽是一次性的。監聽到了刪除節點的事件後便不能監聽到建立節點。
為了能持續監聽需要對監聽處理邏輯做修改
public void process(WatchedEvent event) {
//收到事件通知後的回撥函式(應該是我們自己的事件處理邏輯)
System.out.println(event.getType()+"---"+event.getPath());
//為了能一直監聽,呼叫一次註冊一次
try {
zkClient.getChildren("/", true);
} catch (Exception e) {
e.printStackTrace();
}
4、判斷節點是否存在
@Test
public void testExist() throws Exception{
//一個引數是節點,一個是是否用監聽功能,Stat封裝了該節點的相關資訊比如:czxid,mzxid,ctime,mtime等
Stat stat = zkClient.exists("/idea", false);
System.out.println(stat==null?"不存在":"存在");
}
5、獲取節點資料
@Test
public void getData() throws Exception{
byte[] data = zkClient.getData("/idea", false, null);
System.out.println(new String(data));
}
輸出:
None,null
helloworld
6、刪除節點
@Test
public void delete() throws Exception{
//第一個引數為要刪除的節點,第二個引數表示版本,-1表示所有版本
zkClient.delete("/idea",-1);
}
7、修改節點資料
@Test
public void update() throws Exception{
//原 /idea節點的資料為helloworld
zkClient.setData("/idea", "zookeeper".getBytes(), -1);
//檢視修改資料是否成功
byte[] data = zkClient.getData("/idea", false, null);
System.out.println(new String(data));
}
控制檯輸出:
None,null
zookeeper
監聽機制
參考https://blog.csdn.net/liu857279611/article/details/70495413
版本資訊
參考https://blog.csdn.net/u012831423/article/details/82795563