当前位置:   article > 正文

zookeeper快速入门四:在java客户端中操作zookeeper

zookeeper快速入门四:在java客户端中操作zookeeper

系列文章:

zookeeper快速入门一:zookeeper安装与启动-CSDN博客

zookeeper快速入门二:zookeeper基本概念-CSDN博客

zookeeper快速入门三:zookeeper的基本操作


先启动zookeeper服务端

在maven引入zookeeper依赖。

<dependency>
   <groupId>org.apache.hadoop</groupId>
   <artifactId>zookeeper</artifactId>
   <version>3.3.1</version>
</dependency>

org.apache.zookeeper.Zookeeper是客户端入口主类,负责建立与server的会话。它提供了以下 所示几类主要方法。 

 

在java中启动客户端,注册一个watcher监听链接的建立。

  1. import org.apache.zookeeper.WatchedEvent;
  2. import org.apache.zookeeper.Watcher;
  3. import org.apache.zookeeper.ZooKeeper;
  4. public class ZookeeperClient {
  5. private static final String connectString = "127.0.0.1:2181";
  6. private static final int sessionTimeout = 2000;
  7. private static ZooKeeper zkClient = null;
  8. public void init() throws Exception {
  9. zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
  10. @Override
  11. public void process(WatchedEvent event) {
  12. // 收到事件通知后的回调函数(应该是我们自己的事件处理逻辑)
  13. System.out.println("zookeeper链接建立");
  14. }
  15. });
  16. }
  17. }

在main方法里测试我们的init方法,用Thread.sleep方法等待zookeeper连接创建,不然在zookeeper客户端建立连接之前主线程就已经退出。

  1. public static void main(String[] args)throws Exception{
  2. new ZookeeperClient().init();
  3. Thread.sleep(5000);
  4. }

控制台输出:

接下来我们创建一个名为“/java”的节点,节点数据为“data”,ZooDefs.Ids.OPEN_ACL_UNSAFE的意思是不节点能被所有人访问,CreateMode.PERSISTENT:节点的类型为持久节点。

  1. public void createZnode()throws Exception{
  2. zkClient.create("/java", "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  3. }

判断节点是否存在,false代表不注册监听事件,如果是true,则注册我们在new zookeeper方法里面传递的watcher。

  1. public void testExist() throws Exception{
  2. Stat stat = zkClient.exists("/java", false);
  3. System.out.println(stat==null?"节点不存在":"节点存在");
  4. }

测试一下我们的方法。

  1. public static void main(String[] args)throws Exception{
  2. new ZookeeperClient().init();
  3. Thread.sleep(5000);
  4. new ZookeeperClient().createZnode();
  5. new ZookeeperClient().testExist();
  6. }

 获取节点的数据。false和上面exists方法参数含义一样,表示不注册连接建立时的watcher,第三个stat对象则存储了除了节点数据之外的其他信息,如czxid、mzxid等。如果为null则表示不保存节点的这些信息。

  1. public void getNodeData()throws Exception{
  2. byte[] res = zkClient.getData("/java",false,new Stat());
  3. System.out.println(new String(res));
  4. }

同样测试我们的方法。

  1. public static void main(String[] args)throws Exception{
  2. new ZookeeperClient().init();
  3. Thread.sleep(3000);
  4. new ZookeeperClient().getNodeData();
  5. }

 获取ACL控制列表

  1. public void getACl()throws Exception{
  2. List<ACL> res = zkClient.getACL("/java",new Stat());
  3. for(ACL acl : res){
  4. System.out.println(acl.getId().toString()+acl.getPerms());
  5. }
  6. }

测试:

  1. public static void main(String[] args)throws Exception{
  2. new ZookeeperClient().init();
  3. Thread.sleep(3000);
  4. new ZookeeperClient().getACl();
  5. }

在/java下创建子节点,获取子节点列表。

  1. public void createChildZnode()throws Exception{
  2. zkClient.create("/java/child", "child data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  3. zkClient.create("/java/child2", "child2 data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  4. }
  5. public void getChildNode()throws Exception{
  6. List<String> res = zkClient.getChildren("/java",false);
  7. for(String s : res){
  8. System.out.println(s);
  9. }
  10. }

测试:

  1. public static void main(String[] args)throws Exception{
  2. new ZookeeperClient().init();
  3. Thread.sleep(3000);
  4. new ZookeeperClient().createChildZnode();
  5. new ZookeeperClient().getChildNode();
  6. }

全部代码如下:

  1. import org.apache.zookeeper.*;
  2. import org.apache.zookeeper.data.ACL;
  3. import org.apache.zookeeper.data.Stat;
  4. import java.util.List;
  5. public class ZookeeperClient {
  6. private static final String connectString = "127.0.0.1:2181";
  7. private static final int sessionTimeout = 2000;
  8. private static ZooKeeper zkClient = null;
  9. public void init() throws Exception {
  10. zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
  11. @Override
  12. public void process(WatchedEvent event) {
  13. // 收到事件通知后的回调函数(应该是我们自己的事件处理逻辑)
  14. System.out.println("zookeeper链接建立");
  15. }
  16. });
  17. }
  18. public void createZnode()throws Exception{
  19. zkClient.create("/java", "data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  20. }
  21. public void createChildZnode()throws Exception{
  22. zkClient.create("/java/child", "child data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  23. zkClient.create("/java/child2", "child2 data".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  24. }
  25. public void getChildNode()throws Exception{
  26. List<String> res = zkClient.getChildren("/java",false);
  27. for(String s : res){
  28. System.out.println(s);
  29. }
  30. }
  31. public void testExist() throws Exception{
  32. Stat stat = zkClient.exists("/java", false);
  33. System.out.println(stat==null?"节点不存在":"节点存在");
  34. }
  35. public void getNodeData()throws Exception{
  36. byte[] res = zkClient.getData("/java",false,new Stat());
  37. System.out.println(new String(res));
  38. }
  39. public void getACl()throws Exception{
  40. List<ACL> res = zkClient.getACL("/java",new Stat());
  41. for(ACL acl : res){
  42. System.out.println(acl.getId().toString()+acl.getPerms());
  43. }
  44. }
  45. public static void main(String[] args)throws Exception{
  46. new ZookeeperClient().init();
  47. Thread.sleep(3000);
  48. new ZookeeperClient().createChildZnode();
  49. new ZookeeperClient().getChildNode();
  50. }
  51. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/302444
推荐阅读
相关标签
  

闽ICP备14008679号