Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The zookeeper (API) of big data's ecology

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Import the relevant jar packages when writing zookeeper programs using the maven project:

Org.apache.zookeeper zookeeper 3.4.10

1. Create a cluster connection

/ / Cluster node nodes, separated by commas between static String url= "hadoop01:2181,hadoop02:2181,hadoop03:2181"; / / Cluster programming entry static ZooKeeper zk; / / timeout static long time=5000; / / initialize the ZooKeeper object static {try (url, (int) time,null) using a static code block } catch (IOException e) {e.printStackTrace ();}}

two。 Create a nod

Public void createZnode (ZooKeeper zk) {/ / must use the absolute path String path= "/ test/test04"; / / when creating a node, you must specify the node content byte data [] = "I am a good boy!" .getBytes () The four parameters of try {/ * zk.create indicate: * arg1: node path * arg2: content stored by node * arg3: permission setting: default * arg4: node type (permanent no order, permanent order, temporary no order, Temporary order) * / String info = zk.create (path Data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL) System.out.println (info==null? "fail": "success");} catch (KeeperException e) {e.printStackTrace ();} catch (InterruptedException e) {e.printStackTrace ();}}

3. View node information

Public void getInfo (ZooKeeper zk,String path) {try {/ * zk.getData (path, null, null) * Parameter 1: absolute path of the node string * parameter 2: node listening * parameter 3: node status object * / byte [] data = zk.getData (path, null, null); System.out.println ("node:" + path+ "message is" + (new String (data) } catch (KeeperException e) {e.printStackTrace ();} catch (InterruptedException e) {e.printStackTrace ();}}

4. Modify node data

Public void updateData (ZooKeeper zk,String path) {try {/ * zk.setData (path, data, version) * path: modified node * data: updated data * version: modified version: if you don't know, write-1, indicating the latest version * / zk.setData (path, "" .getBytes (),-1);} catch (Exception e) {e.printStackTrace ();}}

5. Delete nod

Public void deleteZnode (ZooKeeper zk,String path) {try {/ * zk.delete (path, version); * path: path * version: deleted version. If you don't know, write-1 to delete the latest version int * / zk.delete (path,-1). } catch (Exception e) {e.printStackTrace ();}}

6. Get permission information of znode node

Public void getACL (ZooKeeper zk,String path) {try {/ * * zk.getACL (path,stat) * path: path * stat: status information * / List acls = zk.getACL (path, null); for (ACL acl:acls) {System.out.println (acl.getPerms ()) } catch (Exception e) {e.printStackTrace ();}}

7. Get child node

Public void getChildren (ZooKeeper zk, String path) {try {/ / determine whether a node exists if (zk.exists (path, null) = = null) {List childrens = zk.getChildren (path, null) For (String children:childrens) {/ / Note that what is received is the relative path System.out.println (children) of the child nodes under the current node;} catch (Exception e) {e.printStackTrace ();}}

Test monitoring of 8.znode

Public class testZKWatcher {static String url= "hadoop01:2181,hadoop02:2181,hadoop03:2181"; static ZooKeeper zk; static long time=5000; static {try {zk=new ZooKeeper (url, (int) time,null);} catch (Exception) {e.printStackTrace () }} / / set listening public static void liisten (String path,ZooKeeper zk) {try {zk.exists (path) New Watcher () {@ Override / / WatchedEvent event listening event object public void process (WatchedEvent event) {/ / Node trigger type (add, modify, create...) Event.EventType type = event.getType () / / get the path of the triggered node event.getPath (); System.out.println ("path to trigger event" + path+ ", type of triggered event" + type ";}});} catch (Exception e) {e.printStackTrace () }} public static void main (String [] args) {String path= "/ test/test01"; liisten (path,zk); try {/ / trigger snooping zk.create ("jj", "zzy" .getBytes (), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL) } catch (Exception e) {e.printStackTrace ();}

| it should be noted that listening events are one-time. If you need to monitor all the time, you need to set cyclic listening.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 206

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report