Get the App
SLTechnology News&Howtos  ›  Database  › 

Java operation zookeeper instance code

Shulou Source: shulou.com Published: 2022-05-31 17:53:14 09月30日 Update

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!

Tags: Objects nodes data content code examples clusters learning practicality deeper events interests practicality practicality simplicity of operation methods duration more friends websites Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info Xiaomi vpn Linux