“Java 操作 Znode 节点”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
 
(未显示同一用户的6个中间版本)
第1行: 第1行:
 
=== 查询 ===
 
=== 查询 ===
 +
<syntaxhighlight lang="java">
 +
package org.example;
 +
 +
import org.apache.curator.framework.CuratorFramework;
 +
import org.junit.Test;
 +
 +
import java.util.List;
 +
 +
public class Demo2 {
 +
 +
    CuratorFramework cf = ZkUtil.cf();
 +
 +
    /**
 +
    * 查询子节点
 +
    * @throws Exception
 +
    */
 +
    @Test
 +
    public void getChildren() throws Exception {
 +
        List<String> strings = cf.getChildren().forPath("/");
 +
        for (String string : strings) {
 +
            System.out.println(string);
 +
        }
 +
    }
 +
 +
    /**
 +
    * 查询子节点数据
 +
    * @throws Exception
 +
    */
 +
    @Test
 +
    public void getData() throws Exception {
 +
        byte[] bytes = cf.getData().forPath("/qf");
 +
        System.out.println(new String(bytes, "UTF-8"));
 +
    }
 +
 +
}
 +
 +
</syntaxhighlight>
 +
  
 
=== 添加 ===
 
=== 添加 ===
 +
<syntaxhighlight lang="java">
 +
......
 +
    /**
 +
    * 创建新节点
 +
    * @throws Exception
 +
    */
 +
    @Test
 +
    public void create() throws Exception {
 +
        cf.create().withMode(CreateMode.PERSISTENT).forPath("/qf2", "uuuu".getBytes());
 +
    }
 +
......
 +
</syntaxhighlight>
  
 
=== 修改 ===
 
=== 修改 ===
 +
<syntaxhighlight lang="java">
 +
......
 +
    /**
 +
    * 改数据
 +
    * @throws Exception
 +
    */
 +
    @Test
 +
    public void update() throws Exception {
 +
        cf.setData().forPath("/qf2", "oooo".getBytes());
 +
    }
 +
......
 +
</syntaxhighlight>
  
 
=== 删除 ===
 
=== 删除 ===
 +
<syntaxhighlight lang="java">
 +
......
 +
    /**
 +
    * 删除节点及其子节点
 +
    * @throws Exception
 +
    */
 +
    @Test
 +
    public void delete() throws Exception {
 +
        cf.delete().deletingChildrenIfNeeded().forPath("/qf2");
 +
    }
 +
......
 +
</syntaxhighlight>
 +
 +
=== 查看 Znode 的状态 ===
 +
<syntaxhighlight lang="java">
 +
......
 +
    /**
 +
    * 查看节点下状态
 +
    * @throws Exception
 +
    */
 +
    @Test
 +
    public void stat() throws Exception {
 +
        Stat stat = cf.checkExists().forPath("/qf");
 +
        System.out.println(stat);
 +
    }
 +
......
 +
</syntaxhighlight>
 +
[[文件:ZooKeeper查看节点下状态.png|无|缩略图|700x700像素]]

2022年8月17日 (三) 11:47的最新版本

查询

package org.example;

import org.apache.curator.framework.CuratorFramework;
import org.junit.Test;

import java.util.List;

public class Demo2 {

    CuratorFramework cf = ZkUtil.cf();

    /**
     * 查询子节点
     * @throws Exception
     */
    @Test
    public void getChildren() throws Exception {
        List<String> strings = cf.getChildren().forPath("/");
        for (String string : strings) {
            System.out.println(string);
        }
    }

    /**
     * 查询子节点数据
     * @throws Exception
     */
    @Test
    public void getData() throws Exception {
        byte[] bytes = cf.getData().forPath("/qf");
        System.out.println(new String(bytes, "UTF-8"));
    }

}


添加

......
    /**
     * 创建新节点
     * @throws Exception
     */
    @Test
    public void create() throws Exception {
        cf.create().withMode(CreateMode.PERSISTENT).forPath("/qf2", "uuuu".getBytes());
    }
......

修改

......
    /**
     * 改数据
     * @throws Exception
     */
    @Test
    public void update() throws Exception {
        cf.setData().forPath("/qf2", "oooo".getBytes());
    }
......

删除

......
    /**
     * 删除节点及其子节点
     * @throws Exception
     */
    @Test
    public void delete() throws Exception {
        cf.delete().deletingChildrenIfNeeded().forPath("/qf2");
    }
......

查看 Znode 的状态

......
    /**
     * 查看节点下状态
     * @throws Exception
     */
    @Test
    public void stat() throws Exception {
        Stat stat = cf.checkExists().forPath("/qf");
        System.out.println(stat);
    }
......
ZooKeeper查看节点下状态.png