大数据-Zookeeper基本操作命令

Zookeeper的基本操作命令

(1)启动客户端

zkCli.sh

(2)连接其它机器的客户端

connect host:2181

(3)查看帮助

help

(4)查看当前znode所包含的内容

ls /

(5)创建节点

create path data

(6)创建短暂znode

create -e path data

(7)创建带序号znode

create -s path data

(8)创建短暂带序号znode

create -s -e path data

(9)查看此节点的详细信息

ls2 /

(10)获得节点值监听

get path watch

(11)监听路径

ls / watch

(12)修改znode数据

set path data

(13)删除节点

delete path

(14)递归删除

rmr path

(15)查看节点状态信息

stat path
ACL权限列表

OPEN_ACL_UNSAFE:完全开放

CREATOR_ALL_ACL:创建该znode的连接拥有所有权限

READ_ACL_UNSAFE:所有的客户端都可读

CreateMode节点类型

PERSISTENT:持久化节点

PERSISTENT_SEQUENTIAL:持久化有序节点

EPHEMERAL:临时节点(连接断开自动删除)

EPHEMERAL_SEQUENTIAL:临时有序节点(连接断开自动删除)

package com.zkCli.util;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

/*
 * @author Administrator
 * @version 1.0
 */
public class zkClient {
    //定义String类型的connectString
    private String connectString = "192.168.138.130:2181,192.168.138.129:2181,192.168.138.128:2181";
    //定义int类型的sessionTimeout
    private int sessionTimeout = 1000;
    //实例化Zookeeper对象
    ZooKeeper zooKeeper;

    //连接Zookeeper集群
    @Before
    public void Init() throws IOException, KeeperException, InterruptedException {
        //String:连接集群的IP端口,int:超时设置,Watch:监听
        zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("路径:"+watchedEvent.getPath());
                System.out.println("已经触发了"+watchedEvent.getType()+"事件");
            }
        });
    }

    //创建节点
    @Test
    //路径:null
	//已经触发了None事件
	///zook
	//路径:/zook
	//已经触发了NodeChildrenChanged事件
    public void createNode() throws KeeperException, InterruptedException {
        String s = zooKeeper.create("/zook", "zookeeper".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println(s);
        zooKeeper.getChildren("/zook",true);
        String str = zooKeeper.create("/zook/192.168.138.130:2181", "zookeeper".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println(str);
    }

    //查看节点
    @Test
    public void getNode() throws KeeperException, InterruptedException {
        List<String> children = zooKeeper.getChildren("/zook", true);

        for (String s: children){
            System.out.println(s);
        }
    }

    //修改数据
    @Test
    public void setNode() throws KeeperException, InterruptedException {
        zooKeeper.setData("/zook/192.168.138.130:2181","follower".getBytes(),-1);
    }

    //测试是否存在节点
    @Test
    public void existsNode() throws KeeperException, InterruptedException {
        Stat exists = zooKeeper.exists("/zook/192.168.138.130:2181", true);
        System.out.println(exists);
    }

    //删除节点
    @Test
    public void deleteNode() throws KeeperException, InterruptedException {
        zooKeeper.getChildren("/zook",true);
        zooKeeper.delete("/zook/192.168.138.130:2181",-1);
    }
}

Zookeeper的动态上下线感知

ZookeeperServer类

package com.Zookeeper.util;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
 * @author Administrator
 * @version 1.0
 */
public class ZookeeperServer implements Watcher {
    //定义String类型的connectString
    private String connectString;
    //定义int类型的sessionTimeout
    private int sessionTimeout;
    //初始化Zookeeper
    private ZooKeeper zooKeeper = null;

    //构造方法
    public ZookeeperServer(String connectString,int sessionTimeout) throws IOException {
        this.connectString = connectString;
        this.sessionTimeout = sessionTimeout;

        if (zooKeeper == null){
            System.out.println("Starting Zookeeper!");
            //实例化Zookeeper对象
            zooKeeper = new ZooKeeper(connectString,sessionTimeout,this);
        }
    }
    @Override
    public void process(WatchedEvent watchedEvent) {
        try {
            //判断是否存在这路径
            Stat stat = zooKeeper.exists("/", true);

            if (stat == null){
                zooKeeper.create("/","zookeeper".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
            }else {
                //获取子节点
                List<String> children = zooKeeper.getChildren("/", true);

                //创建服务器列表
                ArrayList<String> arrayList = new ArrayList<String>();

                if (children.size() != 0){
                    //遍历子目录
                    for (String str: children){
                        System.out.println("Zookeeper的子目录:"+str);
                        byte[] data = zooKeeper.getData("/" + str, true, null);

                        if (data != null){
                            arrayList.add(new String(data));
                        }
                    }

                    //输出服务器列表的数据
                    System.out.println(arrayList);
                }
            }
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("事件的类型:"+watchedEvent.getType());
    }
}

ZookeeperClient类

package com.Zookeeper.util;

import java.io.IOException;

public class ZookeeperClient extends ZookeeperServer {
    public ZookeeperClient(String connectString, int sessionTimeout) throws IOException {
        super(connectString, sessionTimeout);
    }

    public static void main(String[] args) {
        try {
            //实例化ZookeeperClient对象
            ZookeeperClient zookeeperClient = new ZookeeperClient("192.168.138.130:2181,192.168.138.129:2181,192.168.138.128:2181", 1000);

            //永久监控
            Thread.sleep(Long.MAX_VALUE);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

//Zookeeper的子目录:zook
//Zookeeper的子目录:zookeeper
//[data, ]
//事件的类型:None
create /zook data
//Zookeeper的子目录:zook
//Zookeeper的子目录:zookeeper
//[data, ]
//事件的类型:NodeChildrenChanged
set /zook zookeeper
//Zookeeper的子目录:zook
//Zookeeper的子目录:zookeeper
//[zookeeper, ]
//事件的类型:NodeDataChanged
delete /zook -1
//Zookeeper的子目录:zookeeper
//[]
//事件的类型:NodeDeleted
//Zookeeper的子目录:zookeeper
//[]
//事件的类型:NodeChildrenChanged

障碍和生产者 - 消费者队列的简单实现

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Random;

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;

public class SyncPrimitive implements Watcher {

    static ZooKeeper zk = null;
    static Integer mutex;
    String root;

    SyncPrimitive(String address) {
        if(zk == null){
            try {
                System.out.println("Starting ZK:");
                zk = new ZooKeeper(address, 3000, this);
                mutex = new Integer(-1);
                System.out.println("Finished starting ZK: " + zk);
            } catch (IOException e) {
                System.out.println(e.toString());
                zk = null;
            }
        }
        //else mutex = new Integer(-1);
    }

    synchronized public void process(WatchedEvent event) {
        synchronized (mutex) {
            //System.out.println("Process: " + event.getType());
            mutex.notify();
        }
    }

    /**
     * Barrier
     */
    static public class Barrier extends SyncPrimitive {
        int size;
        String name;

        /**
         * Barrier constructor
         *
         * @param address
         * @param root
         * @param size
         */
        Barrier(String address, String root, int size) {
            super(address);
            this.root = root;
            this.size = size;

            // Create barrier node
            if (zk != null) {
                try {
                    Stat s = zk.exists(root, false);
                    if (s == null) {
                        zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE,
                                CreateMode.PERSISTENT);
                    }
                } catch (KeeperException e) {
                    System.out
                            .println("Keeper exception when instantiating queue: "
                                    + e.toString());
                } catch (InterruptedException e) {
                    System.out.println("Interrupted exception");
                }
            }

            // My node name
            try {
                name = new String(InetAddress.getLocalHost().getCanonicalHostName().toString());
            } catch (UnknownHostException e) {
                System.out.println(e.toString());
            }

        }

        /**
         * Join barrier
         *
         * @return
         * @throws KeeperException
         * @throws InterruptedException
         */

        boolean enter() throws KeeperException, InterruptedException{
            zk.create(root + "/" + name, new byte[0], Ids.OPEN_ACL_UNSAFE,
                    CreateMode.EPHEMERAL_SEQUENTIAL);
            while (true) {
                synchronized (mutex) {
                    List<String> list = zk.getChildren(root, true);

                    if (list.size() < size) {
                        mutex.wait();
                    } else {
                        return true;
                    }
                }
            }
        }

        /**
         * Wait until all reach barrier
         *
         * @return
         * @throws KeeperException
         * @throws InterruptedException
         */
        boolean leave() throws KeeperException, InterruptedException{
            zk.delete(root + "/" + name, 0);
            while (true) {
                synchronized (mutex) {
                    List<String> list = zk.getChildren(root, true);
                        if (list.size() > 0) {
                            mutex.wait();
                        } else {
                            return true;
                        }
                    }
                }
            }
        }

    /**
     * Producer-Consumer queue
     */
    static public class Queue extends SyncPrimitive {

        /**
         * Constructor of producer-consumer queue
         *
         * @param address
         * @param name
         */
        Queue(String address, String name) {
            super(address);
            this.root = name;
            // Create ZK node name
            if (zk != null) {
                try {
                    Stat s = zk.exists(root, false);
                    if (s == null) {
                        zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE,
                                CreateMode.PERSISTENT);
                    }
                } catch (KeeperException e) {
                    System.out
                            .println("Keeper exception when instantiating queue: "
                                    + e.toString());
                } catch (InterruptedException e) {
                    System.out.println("Interrupted exception");
                }
            }
        }

        /**
         * Add element to the queue.
         *
         * @param i
         * @return
         */

        boolean produce(int i) throws KeeperException, InterruptedException{
            ByteBuffer b = ByteBuffer.allocate(4);
            byte[] value;

            // Add child with value i
            b.putInt(i);
            value = b.array();
            zk.create(root + "/element", value, Ids.OPEN_ACL_UNSAFE,
                        CreateMode.PERSISTENT_SEQUENTIAL);

            return true;
        }

        /**
         * Remove first element from the queue.
         *
         * @return
         * @throws KeeperException
         * @throws InterruptedException
         */
        int consume() throws KeeperException, InterruptedException{
            int retvalue = -1;
            Stat stat = null;

            // Get the first element available
            while (true) {
                synchronized (mutex) {
                    List<String> list = zk.getChildren(root, true);
                    if (list.size() == 0) {
                        System.out.println("Going to wait");
                        mutex.wait();
                    } else {
                        Integer min = new Integer(list.get(0).substring(7));
                        String minNode = list.get(0);
                        for(String s : list){
                            Integer tempValue = new Integer(s.substring(7));
                            //System.out.println("Temporary value: " + tempValue);
                            if(tempValue < min) {
                                min = tempValue;
                                minNode = s;
                            }
                        }
                        System.out.println("Temporary value: " + root + "/" + minNode);
                        byte[] b = zk.getData(root + "/" + minNode,
                        false, stat);
                        zk.delete(root + "/" + minNode, 0);
                        ByteBuffer buffer = ByteBuffer.wrap(b);
                        retvalue = buffer.getInt();

                        return retvalue;
                    }
                }
            }
        }
    }

    public static void main(String args[]) {
        if (args[0].equals("qTest"))
            queueTest(args);
        else
            barrierTest(args);
    }

    public static void queueTest(String args[]) {
        Queue q = new Queue(args[1], "/app1");

        System.out.println("Input: " + args[1]);
        int i;
        Integer max = new Integer(args[2]);

        if (args[3].equals("p")) {
            System.out.println("Producer");
            for (i = 0; i < max; i++)
                try{
                    q.produce(10 + i);
                } catch (KeeperException e){

                } catch (InterruptedException e){

                }
        } else {
            System.out.println("Consumer");

            for (i = 0; i < max; i++) {
                try{
                    int r = q.consume();
                    System.out.println("Item: " + r);
                } catch (KeeperException e){
                    i--;
                } catch (InterruptedException e){
                }
            }
        }
    }

    public static void barrierTest(String args[]) {
        Barrier b = new Barrier(args[1], "/b1", new Integer(args[2]));
        try{
            boolean flag = b.enter();
            System.out.println("Entered barrier: " + args[2]);
            if(!flag) System.out.println("Error when entering the barrier");
        } catch (KeeperException e){
        } catch (InterruptedException e){
        }

        // Generate random integer
        Random rand = new Random();
        int r = rand.nextInt(100);
        // Loop for rand iterations
        for (int i = 0; i < r; i++) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
        try{
            b.leave();
        } catch (KeeperException e){

        } catch (InterruptedException e){

        }
        System.out.println("Left barrier");
    }
}

参考资料:http://zookeeper.apache.org/doc/current/zookeeperStarted.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值