Curator之基础操作

文章目录

    • 1.curator 概念
    • 2.API
      • 1.pom文件
      • 2.基本api

1.curator 概念

zookeeper原生API不得不说,有很多的问题,比如:不能递归创建和删除节点、Watcher只能使用一次、还有很多可以解决分布式应用问题的api(比如分布式锁,leader选举等),但由于ZooKeeper提供的原始API并不是很易用,在其基础上封装一些高级应用又是一件很复杂的事情。

这个时候,Curator出现了,Curator是Netflix公司开源的一个Zookeeper客户端,后捐献给Apache,Curator框架在zookeeper原生API接口上进行了包装,解决了很多ZooKeeper客户端非常底层的细节开发。提供ZooKeeper各种应用场景(recipe, 比如:分布式锁服务、集群领导选举、共享计数器、缓存机制、分布式队列等)的抽象封装,实现了Fluent风格的API接口,是最好用,最流行的zookeeper的客户端。

2.API

1.pom文件

    <dependencies>
        <!--hadoop 依赖-->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.2</version>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.5-cdh5.15.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.0.0</version>
        </dependency>
    </dependencies>

2.基本api

  • 在test中进行单元测试
package com.ruozedata.bigdata.zk;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;


public class CuratorApp {
    final static private String connectString = "ruozedata000:2181";
    private static Logger logger = LoggerFactory.getLogger(CuratorApp.class);
    ExponentialBackoffRetry retryPolicy;
    CuratorFramework curatorFramework;

    @Before
    public void setUp() {

        retryPolicy = new ExponentialBackoffRetry(100, 3);  //
        curatorFramework = CuratorFrameworkFactory.newClient(connectString, retryPolicy);
        curatorFramework.start();
    }

    //创建永久节点
    @Test
    public void createPNode() {
        String path = "/ruoze/aaa/b/e";

        try {
            curatorFramework.create()
                    .creatingParentsIfNeeded()      //归创建,如果没有父节点,自动创建父节点
                    .withMode(CreateMode.PERSISTENT)    //节点类型,持久节点
                    .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)   //设置ACL,和原生API相同
                    .forPath(path, "abc".getBytes());
            System.out.println("creating persistent node succeed: " + path);

        } catch (Exception e) {
            logger.error("creating persistent node failed: {}", path);
        }
    }

    //创建临时节点
    @Test
    public void createENode() {
        String path = "/ruoze/d/b/c";

        try {
            curatorFramework.create()
                    .creatingParentsIfNeeded()
                    .withMode(CreateMode.EPHEMERAL)
                    .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
                    .forPath(path, "dbc".getBytes());

            System.out.println("creating EPHEMERAL node succeed: " + path);

        } catch (Exception e) {
            logger.error("creating EPHEMERAL node failed: {}", path);
        }
    }

    //获取节点数据
    @Test
    public void getData() {
        String path = "/ruoze/aaa/b/e";
        Stat nodeStat = new Stat();
        try {
            byte[] bytes = curatorFramework.getData()
                    .storingStatIn(nodeStat)
                    .forPath(path);
            System.out.println("Getting the info for node:" + new String(bytes));
        } catch (Exception e) {
            logger.error("Failing to get the info for node: {}", path);
        }
    }

    //节点是否存在
    @Test
    public void isExist() throws Exception {
        String path = "/ruoze/aaa/b/e/e";

        Stat stat = curatorFramework.checkExists()
                .forPath(path);

        if (stat != null) {
            logger.warn("node exists");
        } else {
            logger.warn("node does not exist");
        }
    }

    //递归删除节点
    @Test
    public void deleteNode() {
        String path = "/ruoze/aaa/b/e";
        try {
            curatorFramework.delete()
                    .deletingChildrenIfNeeded()
                    .forPath(path);
            System.out.println("Succeed to delete the node");

        } catch (Exception e) {
            logger.error("Failing to delete the  node: {}", path);
        }
    }

    //查询某个节点的所有子节点路径
    @Test
    public void childPath() throws Exception {
        String path = "/ruoze1";

        List<String> children = curatorFramework.getChildren()
                .forPath(path);

        for(String child:children){
            logger.warn(child);
        }
    }
}

你可能感兴趣的:(大数据组件)