大数据之ZooKeeper_java端监听节点信息变化,代码实现

首先用Java操作zookeeper之前需要先创建一个Maven项目,然后导入如下依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>zookeeper3</artifactId>
    <version>1.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.6</version>
    </dependency>

</dependencies>
    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    </build>
</project>

java客户端操作zookeeper基础命令

public class Demo {
   static ZooKeeper zooKeeper ;
    public static void main(String[] args) throws Exception {
        //创建一个ZooKeeper客户端,参数一:连接的ZK的位置,参数二:超时时间,参数三:监听器
        zooKeeper = new ZooKeeper("linux01:2181,linux02:2181,linux03:2181",2000,null);
        //获取节点的value
        byte[] data = zooKeeper.getData("/hello0000000000", null, null);
        String string = new String(data);
        System.out.println(s);

        String s = data.toString();
        System.out.println(s);
        修改节点的value,修改的信息需要以byte数组的形式写出
        zooKeeper.setData("/hello0000000000","haha".getBytes(),-1);

        //创建节点,参数一:创建的节点路径,参数二:节点的value,参数三:节点的权限,参数四:节点的类型,永久暂时有序无序
        zooKeeper.create("/h0000000002/h1","hehe".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zooKeeper.create("/h0000000002/h1/h2","hehe".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        zooKeeper.create("/h0000000002/h1/h2/h3","hehe".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

        //获取节点目录下的字节点名
        List<String> children = zooKeeper.getChildren("/h0000000002", null);
        for (String child : children) {
            System.out.println(child);
        }
        删除没有子节点的的节点
        zooKeeper.delete("/h0000000003",-1);
        rmr("/h0000000002");
        zooKeeper.close();
    }
    //递归删除多层节点
    public static void rmr(String path) throws Exception {
        List<String> children = zooKeeper.getChildren(path, null);
        if (children!=null&&children.size()>0){
            for (String child : children) {
                rmr(path+"/"+child);
            }
        }
        zooKeeper.delete(path,-1);
    }
}

java客户端操作zookeeper监听节点value数据变化

public class Demo02 {
    static ZooKeeper  zooKeeper;
    static {
        try {
            zooKeeper = new ZooKeeper("linux01:2181", 2000, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws Exception {
        //获取节点数据,new Watcher监听节点数据变化
        if (zooKeeper==null){
            System.out.println("000");
        }
        byte[] data = zooKeeper.getData("/hello0000000000", new Watcher() {
            public void process(WatchedEvent event) {
                //event.getPath()获取节点路径,event.getType()获取节点变化类型
                System.out.println(event.getPath()+"节点数据发生了变化:"+event.getType());
                byte[] data1 = new byte[0];
                try {
                    //this,当节点信息继续发生变化时,再次调用监控器,起到持续监控的作用
                    data1 = zooKeeper.getData("/hello0000000000", this, null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("变化后的数据为:"+new String(data1));
            }
        }, null);
        System.out.println("原始数据:"+new String(data));
        Thread.sleep(Integer.MAX_VALUE);
        zooKeeper.close();
    }
}

java客户端操作zookeeper,模拟监听Namenode主节点中从节点datanode数量的变化

/**
 * 监控/Namenode节点的子节点个数的变化
 * 当子节点的个数发生变化的时候 重新获取服务列表
 */
public class NameNode {
    static ZooKeeper zk = null;
    public void register() throws Exception {
        zk = new ZooKeeper("linux01:2181,linux02:2181,linux03:2181", 2000, null);
    }
    public void datanodeList() throws Exception {
        List<String> list = zk.getChildren("/Namenode", new Watcher() {
            public void process(WatchedEvent event) {
                try {
                    List<String> children = zk.getChildren("/Namenode", this);
                    List<String> hosts = new ArrayList<String>();
                    for (String datanode : children) {
                        byte[] data = zk.getData("/Namenode/" + datanode, null, null);
                        hosts.add(new String(data));
                    }
                    System.out.println("当前正在服务的datanode有:" + hosts);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        List<String> hosts = new ArrayList<String>();
        for (String datanode : list) {
            byte[] data = zk.getData("/Namenode/" + datanode, null, null);
            hosts.add(new String(data));
        }
        System.out.println("当前正在服务的datanode有:" + hosts);
    }
    public void service() throws InterruptedException {
        System.out.println("正在服务");
        Thread.sleep(Integer.MAX_VALUE);
    }
   public static void main(String[] args) throws Exception {
        NameNode nn =  new NameNode() ;
        nn.register();
        nn.datanodeList();
        nn.service();
    }
}
/**
 * DataNode ,在namenode中建立字节点
 */
public class DataNode {
    static ZooKeeper zk = null;
    public void register(String hostName) throws Exception {
        zk = new ZooKeeper("linux01:2181,linux02:2181,linux03:2181", 2000, null);
        //添加节点
        zk.create("/Namenode/datanode",hostName.getBytes() , ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println("你的宝宝上线了。。。。"+hostName);
    }
    public void service() throws InterruptedException {
        System.out.println("正在服务");
        Thread.sleep(Integer.MAX_VALUE);
    }
    public static void main(String[] args) throws Exception {
        DataNode dn = new  DataNode() ;
        //打成jar包,在CDM窗口运行,可在运行代码的后面跟上你想设置的value值
        dn.register(args[0]);
        dn.register(args[1]);
        dn.service();
    }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值