1. Client command line operation
1.1 basic grammar
command | explain |
---|---|
help | Display all operation commands |
ls path | View the child nodes of the current node (can listen) -w listen for changes in child nodes -s additional secondary information |
create | Create normal node -s contains sequences -e temporary (restart or timeout disappears) |
get path | Obtain the value of the node under the specified path (listening) -w listen for changes in node content -s additional secondary information |
set | Set node details |
stat | View node status |
delete | Delete node |
deleteall | Recursively delete nodes |
1.2 node type
Persistent node: after the client and server are disconnected, the created node will not be deleted
- Persistent directory node: the node still exists after the client is disconnected from Zookeeper
- Persistent sequential numbering directory node: after the client is disconnected from Zookeeper, the node still exists, but Zookeeper sequentially numbers the section name
Ephemeral node: after the client and server are disconnected, the created node will be deleted
- Temporary directory node: the node is deleted after the client is disconnected from Zookeeper
- Temporary sequence number directory node: after the client is disconnected from Zookeeper, the node is deleted, but Zookeeper gives the sequence number to the node name
When creating a node, the sequence ID is set. A value is appended to the node name. The sequence number is a monotonically increasing counter maintained by the parent node.
1.3 node data details
details | describe |
---|---|
czxid | Create a transaction zxid for the node |
ctime | Number of milliseconds a node was created (since 1970) |
mzxid | The node follows the new transaction zxid |
mtime | Number of milliseconds the node was last modified (since 1970) |
pzxid | Node last updated child node zxid |
cversion | Change number of child nodes, modification times of child nodes |
dataversion | Node data change number |
aclVersion | Change number of node access control list |
ephemeralOwner | If it is a temporary node, this is the session id of the node owner. If it is not a temporary node, it is 0 |
dataLength | Data length of node |
numChildren | Number of child nodes of node |
2. Client API operation
2.1 project preparation
one ️⃣ Create Maven project
two ️⃣ Import dependency
<!--unit testing --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> <scope>test</scope> </dependency> <!--zookeeper rely on--> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.7</version> </dependency> <!--journal--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.6.2</version> </dependency>
2.2 create client
private String connectString = "node1:2181,node2:2181,node3:2181"; private int sessionTimeOut = 2000; private ZooKeeper zooKeeper; /** * Initialize connection * @throws IOException */ @Before public void init() throws IOException { // connectString: connected host and port number // sessionTimeOut: timeout (in seconds) // Watcher: node listener zooKeeper = new ZooKeeper(connectString, sessionTimeOut, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { } }); }
2.3 creating nodes
@Test public void create() throws InterruptedException, KeeperException, IOException { // Create the address of the node // Data content of node // Node level // Node type String nodeCreated = zooKeeper.create("/mouse", "Jack".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); }
Test using the command line:
2.4 determining whether a node exists
@Test public void exist() throws IOException, InterruptedException, KeeperException { if(zooKeeper == null){ System.out.println("zooKeeper Null, creating new connection!----------------"); zooKeeper = new ZooKeeper(connectString, sessionTimeOut, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { } }); } Stat stat = zooKeeper.exists("/mouse", false); System.out.println(stat==null ? "The node does not exist" : "The node does not exist"); }
IDEA output test:
2.5 listening node
@Test public void getChildren() throws IOException, InterruptedException, KeeperException { if(zooKeeper == null){ System.out.println("zooKeeper Null, creating new connection!----------------"); zooKeeper = new ZooKeeper(connectString, sessionTimeOut, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { System.out.println("---------------------"); List<String> childrens = null; try { childrens = zooKeeper.getChildren("/", true); for (String children : childrens) { System.out.println(children); } System.out.println("---------------------"); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }); } Thread.sleep(Long.MAX_VALUE); }
Test:
❤️ END ❤️