zookeeper的java客户端常用api
一。maven导入zookeeper
1. 在这个网址搜索zookeeper导入:https://search.maven.org/ ,pom文件可如下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>hadoop_01</groupId> <artifactId>hadoop_01</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.13</version> <type>pom</type> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
2. 如果zookeeper不可用在刚才的网址直接下载类包导入。
二。增删改查
1. 初始化
1 public class ZkClient { 2 private static Logger logger = Logger.getLogger(ZkClient.class); 3 ZooKeeper zooKeeper = null; 4 5 //挨个尝试连接,哪个能连接上就连接哪个 6 private static final String connectString = "host1:2181,host2:2181,host3:2181"; 7 // 在 C:\Windows\System32\drivers\etc 的 hosts 文件加入 host1,host2,host3 的ip 8 private static final int sessionTimeOut = 2000; 9 10 @Before 11 public void init() throws IOException { 12 BasicConfigurator.configure(); 13 logger.info("123456"); 14 15 //参数1:连接地址,2:连接超时时间(毫秒),3:回调事件,4:是否只读 16 zooKeeper = new ZooKeeper(connectString, sessionTimeOut, (event -> { 17 // 事件的回调 18 logger.info(event.getType() + "----事件回调----" + event.getPath()); 19 }),false); 20 } 21 22 /** 23 * 增删改查 24 * @throws KeeperException 25 * @throws InterruptedException 26 */ 27 28 }
2. 增加节点
1 @Test 2 // 创建数据到zk节点中 3 public void createNode() throws KeeperException, InterruptedException { 4 //返回值:节点路径 参数1:节点路径 2:节点数据(可以是任何类型) 3:节点权限(OPEN_ACL_UNSAFE 开放) 4:节点类型(PERSISTENT 持久的) 5 String createNode = zooKeeper.create("/idea", "aaaaaaaa".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 6 logger.info("返回值为:" + createNode); 7 }
可以看到增加的节点
[zk: localhost:2181(CONNECTED) 0] ls / [zookeeper] [zk: localhost:2181(CONNECTED) 1] ls / [zookeeper, idea] [zk: localhost:2181(CONNECTED) 2]
3 判断节点是否存在
1 @Test 2 // 判断节点是否存在 3 public void nodeExists() throws KeeperException, InterruptedException { 4 // Stat 为数据描述信息 5 Stat stat = zooKeeper.exists("/idea",false); 6 logger.info(stat==null?"节点不存在":"节点存在"); 7 }
4. 获取子节点
1 @Test 2 // 获取子节点 3 public void getNode() throws KeeperException, InterruptedException { 4 // 参数2:监听节点变化,不写回调函数则会调用初始化时的回调函数 5 List<String> node = zooKeeper.getChildren("/",true); 6 for (String n:node){ 7 logger.info("节点为:" + n); 8 } 9 Thread.sleep(Long.MAX_VALUE); 10 }
5. 获取节点数据
1 @Test 2 // 获取节点数据 3 public void getData() throws KeeperException, InterruptedException { 4 byte[] data = zooKeeper.getData("/idea",false,null); 5 logger.info("数据为:" + new String(data, StandardCharsets.UTF_8)); 6 }
6. 删除节点
1 @Test 2 // 删除节点 3 public void deleteNode() throws KeeperException, InterruptedException { 4 // 参数2:删除数据的版本,-1为全部版本都删除 5 zooKeeper.delete("/idea",-1); 6 }
7. 修改节点数据
1 @Test 2 // 修改节点数据 3 public void setData(){ 4 zooKeeper.setData("/idea", "qweasd".getBytes(), -1); 5 }
三。更多api可在idea编辑器查看。