Hadoop基礎(三十五):Zookeeper API 應用 客戶端模式
阿新 • • 發佈:2020-07-21
1 Vscode 環境搭建
1.1.建立一個 Maven 工程
參照Maven 專題(二):vscode 建立一個java Maven專案(vscode)以HdfsClientDemo 為例1.2.新增 pom 檔案
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> </dependency1.3.拷貝 log4j.properties 檔案到專案根目錄 需要在專案的 src/main/resources 目錄下,新建一個檔案,命名為“log4j.properties”,在檔案中填入。> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.14</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> </dependencies>
log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=target/spring.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
2 建立 ZooKeeper 客戶端
3 建立子節點
// 建立子節點 @Test public void create() throws Exception { // 引數 1:要建立的節點的路徑; 引數 2:節點資料 ; 引數 3:節點權限 ;引數 4:節點的型別 String nodeCreated = zkClient.create("/atguigu", "jinlian".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); }
4 獲取子節點並監聽節點變化
5 判斷 Znode 是否存在
package com.atguigu.zkclient; import java.io.IOException; import java.util.List; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.data.Stat; import org.junit.Test; /** * a simple zkclient. */ public class ZkClient { private static final int SESSION_TIMEOUT = 300000; private static final String CONNECT_STRING = "192.168.1.122:2181,192.168.1.133:2181,192.168.1.144:2181"; private ZooKeeper zkCli; @Test public void before() throws IOException { zkCli = new ZooKeeper(CONNECT_STRING, SESSION_TIMEOUT, e -> { System.out.println("預設回撥函式"); }); System.out.println("before"); } @Test public void ls() throws IOException, InterruptedException, KeeperException { //檢視檔案資訊 List<String> children = zkCli.getChildren("/", true); System.out.println("=========================================="); for (String child : children) { System.out.println(child); } System.out.println("=========================================="); } @Test public void create() throws IOException, InterruptedException, KeeperException { String s = zkCli.create("/Idea", "Idea2018".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); System.out.println(s); Thread.sleep(Long.MAX_VALUE); } @Test public void get() throws IOException, KeeperException, InterruptedException { byte[] data = zkCli.getData("/zxx00000007", true, new Stat()); String string = new String(data); System.out.println(string); } @Test public void set() throws InterruptedException, KeeperException { Stat stat = zkCli.setData("/zxx00000007", "defabc".getBytes(), 0); System.out.println(stat.getDataLength()); } @Test public void stat() throws KeeperException, InterruptedException { Stat exists = zkCli.exists("/zxx00000007", false); if (exists == null) { System.out.println("節點不存在"); } else { System.out.println(exists.getDataLength()); } } @Test public void delete() throws KeeperException, InterruptedException { Stat exists = zkCli.exists("/zxx00000007", false); if (exists != null) zkCli.delete("/zxx00000007", exists.getVersion()); } @Test public void register() throws KeeperException, InterruptedException { byte[] data = zkCli.getData("/a", new Watcher() { @Override public void process(WatchedEvent event) { try { register(); } catch (KeeperException e) { //TODO: handle exception e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }, null); System.out.println(new String(data)); } @Test public void testRegister() { try { register(); Thread.sleep(Long.MAX_VALUE); } catch (KeeperException e) { //TODO: handle exception e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }