zookeeper的主要功能以及zookeeper客户端操作

一、zookeeper的主要功能

zookeeper是大数据集群中的一个基础组件,主要有两个功能:
1、为客户端管理少量数据
2、为客户端监听指定数据节点的状态,并在数据节点发生变化时,通知客户端。

二、zookeeper命令行客户端操作(shell)

1、启动zookeeper命令行客户端:zkCli.sh
2、zookeeper客户端基本命令

  • 查看子节点  ls /
  • 创建节点  create /mm “hellozk”
    创建临时节点,重启会删除掉 create -e /hh 998
    给节点编号   create -s /hh 998
  • 查看节点数据  get /mm
  • 修改节点 set /mm “hellospark”
  • 删除节点  delete /mm
  • 递归删除节点  rmr /mm
  • 监听  set /mm 1998 watch 只能监听一次,监听数据修改
    ls   / watch  监听/目录下子节点的创建和删除,只能监听一次

**:每台服务器上的zookeeper数据是同步的

三、zookeeper的Java客户端操作(JavaAPI)

public class ZkCURD {

    private static final String connectString = "hadoop01:2181,hadoop02:2181,hadoop03:2181";
    private int sessionTimeout = 2000;
    ZooKeeper zkCli = null;
    @Before
    public void init() throws Exception {
    //连接zookeeper,并开启监听,设置会话超时
         zkCli = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            //监听 / 目录下子节点变化的逻辑回调
            @Override
            public void process(WatchedEvent event) {
    //输出事件的状态和节点            System.out.println(event.getType() +"   "+  event.getPath());
                try {
                    zkCli.getChildren("/", true);
                } catch (KeeperException | InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });


    }
    /**
     * 创建节点
     * @throws Exception
     */
    @Test
    public void Create() throws Exception {
        zkCli.create("/ghm/hh", "aminaiff".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
    }

    /**
     * 获取子节点
     * @throws Exception 
     */
    @Test
    public void GetChildren() throws Exception {
        List<String> children = zkCli.getChildren("/", false);
        System.out.println(children);
    }
    /**
     * 查看节点是否存在
     * @throws Exception 
     */
    @Test
    public void exist() throws Exception {
        Stat exists = zkCli.exists("/mm", false);
        System.out.println(exists);
    }
    /**
     * 获取节点数据
     * @throws Exception 
     * @throws KeeperException 
     * @throws Exception
     */
    @Test
    public void GetData() throws KeeperException, Exception {
        byte[] data = zkCli.getData("/mm", false, null);
        System.out.println(new String(data));
        System.out.println(data.toString());

    }
    /**
     * 删除节点
     * @throws Exception 
     */
    @Test
    public void delete() throws Exception {
        zkCli.delete("/ghm0000000003", -1);
    }
    /**
     * 修改节点中的数据
     * @throws Exception 
     * @throws KeeperException 
     * @throws Exception
     */
    @Test 
    public void SetData() throws KeeperException, Exception {
        zkCli.setData("/mm", "huihui".getBytes(), -1);
    }
    /**
    *监听 / 目录下子节点
    */
    @Test
    public void watch() throws Exception, Exception {
        List<String> children = zkCli.getChildren("/", true);
        for (String string : children) {
            System.out.println(string);
        }
        Thread.sleep(2*60*1000);
    }
    //关闭资源
    @After
    public void close() throws Exception {
        zkCli.close();
    }
}

**:watch监听只能执行一次,zkCli.getChildren(“/”, true)只是开启监听,并没有监听的逻辑,
在@Before中的zkCli = new ZooKeeper(connectString, sessionTimeout, new Watcher() 写了监听的逻辑,使监听生效,并且在每次连接zookeeper时都可以开启监听,使监听可以循环多次使用。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值