在ZooKeeper中,创建节点时,数据是以字符串形式存储的
例如,如果你想创建一个名为/exampleNode
的节点,并设置其数据为exampleValue
,你可以使用ZooKeeper的命令行工具或Java客户端库来执行以下操作:
- 使用ZooKeeper命令行工具:
create /exampleNode exampleValue
- 使用Java客户端库:
import org.apache.zookeeper.*; public class CreateNodeExample { public static void main(String[] args) throws Exception { // 连接到ZooKeeper服务器 ZooKeeper zooKeeper = new ZooKeeper("localhost:2181", 3000, new Watcher() { @Override public void process(WatchedEvent event) { System.out.println("Event received: " + event); } }); // 创建节点 byte[] data = "https://www.yisu.com/ask/exampleValue".getBytes(); String path = "/exampleNode"; zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } }
在这两种情况下,/exampleNode
节点将被创建,其数据为exampleValue
。