In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "java operation zookeeper example code", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn the "java operation zookeeper example code"!
Package com.rxzx.demo
Import java.io.IOException
Import java.util.List
Import org.apache.zookeeper.CreateMode
Import org.apache.zookeeper.KeeperException
Import org.apache.zookeeper.WatchedEvent
Import org.apache.zookeeper.Watcher
Import org.apache.zookeeper.ZooDefs.Ids
Import org.apache.zookeeper.ZooKeeper
Import org.apache.zookeeper.data.Stat
Public class ZKDemo {
/ / timeout for connecting to zookeeper (milliseconds)
Private static final int OUT_TIME=60000
/ / zookeeper cluster connection object
Private ZooKeeper zk
/ / Custom listening event object
Private Watcher wc=new Watcher () {
@ Override
Public void process (WatchedEvent event) {
/ / TODO Auto-generated method stub
If (event.getType ()) = = Event.EventType.NodeDataChanged) {
System.out.println (event.getPath () + "+ event.getType ())
} else {
System.out.println (event.getPath () + "+ event.getType ())
}
}
}
/ / obtain the monitoring object
Public Watcher getWatcher () {
Return this.wc
}
/ / get the zookeeper cluster connection object
Public ZooKeeper getZooKeeper () {
If (zk==null) {
Try {
Zk=new ZooKeeper ("192.168.2.100 2181192.168.2.101Rose 2181192.168.2.102", OUT_TIME,null)
} catch (IOException e) {
/ / TODO Auto-generated catch block
E.printStackTrace ()
}
}
Return zk
}
/ / close the zookeeper connection object
Public void close () {
If (zkhorse null) {
Try {
Zk.close ()
} catch (InterruptedException e) {
/ / TODO Auto-generated catch block
E.printStackTrace ()
}
}
}
Public static void main (String [] args) {
/ / TODO Auto-generated method stub
ZKDemo zd=new ZKDemo ()
Try {
/ / get the zookeeper connection object
ZooKeeper zk=zd.getZooKeeper ()
/ / traverse the child nodes of the node node
List nodelist=zk.getChildren ("/ node", zd.getWatcher ())
For (String s:nodelist) {
System.out.println (s)
}
/ / create a child node son of node
/ / zk.create ("/ node/son", "hello word" .getBytes (), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL)
/ / determine whether the data node exists, and return the object if it exists
Stat son=zk.exists ("/ node/son0000000011", zd.getWatcher ())
If (sonographic null) {
System.out.println (son.getCzxid ())
} else {
System.out.println ("this node does not exist")
}
/ / modify the data content of the data node
/ / zk.setData ("/ node/son0000000011", "wwwwww" .getBytes (),-1)
/ / Stat stat=new Stat ()
/ / get the data content of the data node
/ / byte [] d=zk.getData ("/ node/son0000000011", zd.getWatcher (), null)
/ System.out.println (new String (d))
/ / System.out.println (stat.getCzxid ())
Zk.delete ("/ node/son4",-1)
System.out.println (operation complete)
} catch (KeeperException e) {
/ / TODO Auto-generated catch block
E.printStackTrace ()
} catch (InterruptedException e) {
/ / TODO Auto-generated catch block
E.printStackTrace ()
} finally {
Zd.close ()
}
}
}
At this point, I believe you have a deeper understanding of the "java operation zookeeper instance code", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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: 0
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.