zookeeper增删改查、监听,java练习

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

public class ZkTest {

    private RetryPolicy retryPolicy;
    private CuratorFramework client;

    public ZkTest() {
        // 重试策略
        this.retryPolicy = new ExponentialBackoffRetry(1000,3);

        //创建连接
        //注意sessionTimeoutMs 会话时间不能够设置太短,否则会报错
        this.client = CuratorFrameworkFactory.newClient(
                 "192.168.88.3:2181,192.168.88.4:2181,192.168.88.5:2181"
                ,50000
                ,1000
                , this.retryPolicy
        );

        System.out.println("正在连接");
        this.client.start();
        System.out.println("连接成功");
    }

    /*
     * 创建节点 路径 + 内容
     */
    @Test
    public void createPointFun1() throws Exception {
        this.client.create().forPath("/ZkTest/fun1","测试".getBytes());
    }

    /*
     * 创建节点 路径
     * 初始内容为空
     */
    @Test
    public void createPointFun2() throws Exception {
        this.client.create().forPath("/ZkTest/fun2");
    }

    /*
     * 创建节点
     * 自动递归创建父节点
     */
    @Test
    public void createPointFun3() throws Exception {
        this.client.create().creatingParentsIfNeeded().forPath("/ZkTest2/fun3");
    }

    /*
     * 创建临时节点
     */
    @Test
    public void createPointFun4() throws Exception {
        this.client.create().withMode(CreateMode.EPHEMERAL).forPath("/ZkTest/fun4","测试创建临时节点".getBytes());
        //等待10秒查看结果
        Thread.sleep(10000);
    }

    /*
     * 删除节点
     */
    @Test
    public void deletePointFun1() throws Exception {
        this.client.delete().forPath("/ZkTest2/fun3");
    }

    /*
     * 删除节点并递归删除其子节点
     */
    @Test
    public void deletePointFun2() throws Exception {
        this.client.delete().deletingChildrenIfNeeded().forPath("/ZkTest2");
    }

    /*
     * 强制保证删除一个节点
     */
    @Test
    public void deletePointFun3() throws Exception {
        this.client.delete().guaranteed().forPath("/ZkTest2");
    }

    /*
     * 读取数据
     */
    @Test
    public void getDataFun1() throws Exception {
        byte[] result = this.client.getData().forPath("/ZkTest/fun1");
        String s = new String(result);
        System.out.println(s);
    }

    /*
     * 包含状态的查询
     */
    @Test
    public void getDataFun2() throws Exception {
        Stat stat = new Stat();
        byte[] result = this.client.getData().storingStatIn(stat).forPath("/ZkTest/fun1");
        System.out.println(new String(result));
        System.out.println(stat.toString());
    }

    /*
     * 更新数据
     */
    @Test
    public void setDataFun3() throws Exception {
        this.client.setData().forPath("/ZkTest/fun1","测试 更新内容".getBytes());
    }

    /*
     * 监听节点
     */
    @Test
    public void watchFun1() throws Exception {
        NodeCache nodecache = new NodeCache(this.client,"/ZkTest/fun1");
        nodecache.getListenable().addListener(new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                String path = nodecache.getPath();
                System.out.println("节点 " + path + "检测到操作");
            }
        });

        nodecache.start(true);

        System.out.println("监听器开启");
        Thread.sleep(60000);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
课后作业 1. 简述zookeeper在HBase中的作用 2. 如何获取链接zookeeper的客户端的信息 3. 简述如何用zookeeper实现一个普通的一对多fifo队列 4. 使用伪代码简述mapreduce的流程和中间结果 其中input和output都是fileinputformat和fileoutputformat 5. 编写程序实现倒排索引 首先准备数据:1.txt,文件内容如下: The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models. It is designed to scale up from single servers to thousands of machines, each offering local computation and storage. Rather than rely on hardware to deliver high-availability, the library itself is designed to detect and handle failures at the application layer, so delivering a highly-available service on top of a cluster of computers, each of which may be prone to failures. 文件2.txt的内容如下所示: In order to scale the name service horizontally, federation uses multiple independent Namenodes/Namespaces. The Namenodes are federated, that is, the Namenodes are independent and don’t require coordination with each other. The datanodes are used as common storage for blocks by all the Namenodes. Each datanode registers with all the Namenodes in the cluster. Datanodes send periodic heartbeats and block reports and handles commands from the Namenodes. 建立类似的文件,放入/test1文件夹下 要求处理结果为: The 1.txt n 2.txt n Apache 1.txt n 2.txt n 其中,n为某个单词在某个文件出现的次数,即,格式解释为:单词iterator<所在文件 出现次数>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值