在ZooKeeper中,创建节点时,数据是以字符串形式存储的
例如,如果你想创建一个名为/exampleNode
的节点,并设置其数据为exampleValue
,你可以使用ZooKeeper的命令行工具或Java客户端库来执行以下操作:
create /exampleNode exampleValue
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 = "exampleValue".getBytes();
String path = "/exampleNode";
zooKeeper.create(path, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
在这两种情况下,/exampleNode
节点将被创建,其数据为exampleValue
。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>