Zookeeper client operation

1. Client command line operation

1.1 basic grammar

commandexplain
helpDisplay all operation commands
ls pathView the child nodes of the current node (can listen)
-w listen for changes in child nodes
-s additional secondary information
createCreate normal node
-s contains sequences
-e temporary (restart or timeout disappears)
get pathObtain the value of the node under the specified path (listening)
-w listen for changes in node content
-s additional secondary information
setSet node details
statView node status
deleteDelete node
deleteallRecursively 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

detailsdescribe
czxidCreate a transaction zxid for the node
ctimeNumber of milliseconds a node was created (since 1970)
mzxidThe node follows the new transaction zxid
mtimeNumber of milliseconds the node was last modified (since 1970)
pzxidNode last updated child node zxid
cversionChange number of child nodes, modification times of child nodes
dataversionNode data change number
aclVersionChange number of node access control list
ephemeralOwnerIf it is a temporary node, this is the session id of the node owner. If it is not a temporary node, it is 0
dataLengthData length of node
numChildrenNumber 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 ❤️

Keywords: Big Data Zookeeper

Added by prueba123a on Thu, 30 Dec 2021 11:04:02 +0200