zookeeper命令

  常用命令:

  

     使用ls命令查看当前Zookeeper中所包含的内容:ls /

  

       创建一个新的Znode节点"zk",以及和它相关字符,执行命令:create /zk myData

  

   查看创建的节点

  

   更改字符:

  

    删除节点

  

   API的使用

  Zookeeper API共包含五个包,分别为:

  (1)org.apache.zookeeper
  (2)org.apache.zookeeper.data
  (3)org.apache.zookeeper.server
  (4)org.apache.zookeeper.server.quorum
  (5)org.apache.zookeeper.server.upgrade

   其中org.apache.zookeeper,包含Zookeeper类,是编程时最常用的类文件。这个类是Zookeeper客户端的主要类文件。如果要使用Zookeeper服务,应用程序首先必须创建一个Zookeeper实例,这时就需要使用此类。一旦客户端和Zookeeper服务建立起了连接,Zookeeper系统将会给次连接会话分配一个ID值,并且客户端将会周期性的向服务器端发送心跳来维持会话连接。只要连接有效,客户端就可以使用Zookeeper API来做相应处理。

  

   ZooKeeper中的组成员关系

  znode既可以作为保存数据的容器(如同文件),也可以作为保存其他znode的容器(如同目录)。所有的znode构成一个层次化的命名空间。一种自然的建立组成员列表的方式就是利用这种层次结构,创建一个以组名为节点名的znode作为父节点,然后以组成员名(服务器名)为节点名来创建作为子节点的znode。如下图给出了一组具有层次结构的znode

  

  创建组代码:

package com.smart;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class CreateGroup implements Watcher {

    private int TIMEOUT = 5000;
    private CountDownLatch countDownLatch = new CountDownLatch(1);
    private ZooKeeper zooKeeper;

    @Override
    public void process(WatchedEvent event) {
        if(event.getState().equals(Event.KeeperState.SyncConnected)){
            countDownLatch.countDown();
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        CreateGroup createGroup = new CreateGroup();
        createGroup.connect("192.168.223.142:2181");
        createGroup.createGroup("/zkNode");
    }

    private void createGroup(String zkNode) throws KeeperException, InterruptedException {
        if(zooKeeper.exists(zkNode,false)==null){
            zooKeeper.create(zkNode,null, ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
        }
        System.out.println("create node success");
    }

    private void connect(String hosts) throws IOException, InterruptedException {
        zooKeeper=new ZooKeeper(hosts,TIMEOUT,this);
        countDownLatch.await();
    }

}

  添加组

package com.smart;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class ConnectionWatcher  implements Watcher {

    private int TIMEOUT=5000;
    private CountDownLatch countDownLatch=new CountDownLatch(1);
    protected ZooKeeper zooKeeper;

    @Override
    public void process(WatchedEvent event) {
        if(event.getState().equals(Event.KeeperState.SyncConnected)){
            countDownLatch.countDown();
        }
    }

    protected void createGroup(String zkNode) throws KeeperException, InterruptedException {
        if(zooKeeper.exists(zkNode,false)==null){
            zooKeeper.create(zkNode,null, ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
        }
        System.out.println("create group success");
    }


    public void connect(String hosts) throws IOException, InterruptedException {
        zooKeeper=new ZooKeeper(hosts,TIMEOUT,this);
        countDownLatch.await();
    }
}



package com.smart;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;

import java.io.IOException;

public class TestGroup extends ConnectionWatcher {

    public void joinGroup(String groupName,String nodeName) throws KeeperException, InterruptedException {
        String path="/"+groupName+"/"+nodeName;
        String createPath = zooKeeper.create(path, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println("create path:"+createPath);
    }

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        TestGroup createGroup = new TestGroup();
        createGroup.connect("192.168.223.142:2181");
        createGroup.joinGroup("zkNode","node");
    }

}

  列出组成员

public void listGroup(String groupName) throws KeeperException, InterruptedException {
        String path="/"+groupName;
        List<String> children = zooKeeper.getChildren(path, false);
        if(children==null ||children.isEmpty()){
            System.out.println("children is empty");
            return;
        }
        for (String child : children){
            System.out.println(child);
        }
    }

  调用getChildren()方法来检索并打印输出一个znode的子节点列表,调用参数为:该znode的路径和设为false的观察标志。如果在一znode上设置了观察标志,那么一旦该znode的状态改变,关联的观察(Watcher)会被触发。

      删除组

  ZooKeeper类提供了一个delete()方法,该方法有两个参数:

  1. 路径

  2. 版本号

  如果所提供的版本号与znode的版本号一致,ZooKeeper会删除这个znode。这是一种乐观的加锁机制,使客户端能够检测出对znode的修改冲突。通过将版本号设置为-1,可以绕过这个版本检测机制,不管znode的版本号是什么而直接将其删除。ZooKeeper不支持递归的删除操作,因此在删除父节点之前必须先删除子节点。

 public void deleteGroup(String groupName) throws KeeperException, InterruptedException {
        String path="/"+groupName;
        List<String> children = zooKeeper.getChildren(path, false);
        for(String child : children){
            String childPath=path+"/"+child;
            zooKeeper.delete(childPath,-1);
        }
        zooKeeper.delete(path,-1);
    }

  

  

  

   

posted on 2019-10-26 22:19  溪水静幽  阅读(178)  评论(0)    收藏  举报