大数据学习笔记-------------------(12_2)

网友投稿 277 2022-11-17

大数据学习笔记-------------------(12_2)

11.7 setData方法

zookeeper类提供setData方法来修改附着在指定znode的数据。setData方法:

setData(String path, byte[] data, int version)

参数解释说明:

path-----------znode路径

data-----------存储在指定znode路径的数据

version-------znode当前版本。只要数据发生改变,zookeeper就会更新znode的版本号

创建一个新的Java应用程序帮助理解ZooKeeperAPI的setData函数。创建一个文件ZKSetData.java。在main方法中,使用ZooKeeperConnection对象创建一个ZooKeeper对象ZK。然后,调用ZK对象的SetData方法:指定路径、新数据、该节点的版本。

下面是完整的程序代码来修改附加在指定znode数据:ZKSetData.java

import org.apache.zookeeper.ZooKeeper;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.Watcher.Event.KeeperState;import java.io.IOException;public class ZKSetData {private static ZooKeeper zk;private static ZooKeeperConnection conn;// Method to update the data in a znode. Similar to getData but without watcher.public static void update(String path, byte[] data) throws KeeperException,InterruptedException {zk.setData(path, data, zk.exists(path,true).getVersion());}public static void main(String[] args) throws InterruptedException,KeeperException {String path= "/MyFirstZnode";byte[] data = "Success".getBytes(); //Assign data which is to be updated.try {conn = new ZooKeeperConnection();zk = conn.connect("localhost");update(path, data); // Update znode data to the specified path }catch(Exception e) {System.out.println(e.getMessage());}}}

一旦编译并运行,使用zookeeperCLI zkCli.sh检查指定znode数据变化:

启动客服端:bin/zkCli.sh start

调用get:get /MyFirstZnode

11.8 getChildren 方法

zookeeper类提供的getChildren方法来获取指定znode的所有子节点。getChildren方法:

getChildren(String path, Watcher watcher)

参数解释说明:

path----------znode路径​

watcher----调用类型为watcher函数。当指定znode被删除或znode以下的children被创建/删除时,zookeeper ensemble会通知。只会通知一次​

代码:ZKGetChildren.java

import java.io.IOException;import java.util.*;import org.apache.zookeeper.ZooKeeper;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.Watcher.Event.KeeperState;import org.apache.zookeeper.data.Stat;public class ZKGetChildren {private static ZooKeeper zk;private static ZooKeeperConnection conn;// Method to check existence of znode and its status, if znode is available.public static Stat znode_exists(String path) throws KeeperException,InterruptedException {return zk.exists(path,true);}public static void main(String[] args) throws InterruptedException,KeeperException {String path= "/MyFirstZnode"; // Assign path to the znodetry {conn = new ZooKeeperConnection();zk = conn.connect("localhost");Stat stat = znode_exists(path); // Stat checks the pathif(stat!= null) {//“getChildren” method- get all the children of znode.It has two args, path and watchList children = zk.getChildren(path, false);for(int i = 0; i < children.size(); i++)System.out.println(children.get(i)); //Print children's} else {System.out.println("Node does not exists");}}catch(Exception e) {System.out.println(e.getMessage());}}}

在运行程序之前,使用ZooKeeper CLI,zkCli.sh为/MyFirstZnode创建两个子节点:

启动客服端:bin/zkCli.sh start

调用create:

create/MyFirstZnode/myfirstsubnode Hi create /MyFirstZnode/mysecondsubmode Hi

现在,编译和运行程序的输出上面创建的znodes:myfirstsubnode 、mysecondsubnode

11.9 删除Znode

zookeeper类提供delete方法来删除指定znode。Delete方法:

delete(String path, int version)

参数解释说明:

path------znode路径

version---当前znode的版本

创建一个新的Java应用程序帮助理解ZooKeeperAPI的delete函数。创建一个文件ZKDelete.java。在main方法中,使用ZooKeeperConnection对象创建一个ZooKeeper对象ZK。然后,调用ZK对象的delete方法:节点的指定路径和版本。

完整的程序代码来删除一个znode如下:ZKDelete.java

import org.apache.zookeeper.ZooKeeper;import org.apache.zookeeper.KeeperException;public class ZKDelete { private static ZooKeeper zk;private static ZooKeeperConnection conn;// Method to check existence of znode and its status, if znode is available.public static void delete(String path) throws KeeperException,InterruptedException {zk.delete(path,zk.exists(path,true).getVersion());}public static void main(String[] args) throws InterruptedException,KeeperException {String path= "/MyFirstZnode"; //Assign path to the znodetry{conn = new ZooKeeperConnection();zk = conn.connect("localhost");delete(path); //delete the node with the specified path}catch(Exception e) {System.out.println(e.getMessage()); // catches error messages}}}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:大数据学习笔记-------------------(11)
下一篇:宽带光纤接入的概念及典型应用类型
相关文章

 发表评论

暂时没有评论,来抢沙发吧~