提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录客户端API *** 作
依赖前提:1.创建 ZooKeeper客户端2.创建子节点
create 方法acl----- 权限CreateMode -----节点类型 (持久 还是 临时节点) 3.获取子节点 并监听节点变化
注意设置的监听,只监听一次,如果重复监听,得设置监听得process再次注册 4.判断 Znode是否存在
客户端API *** 作 依赖
前提:junit junitRELEASE org.apache.logging.log4j log4j-core2.8.2 org.apache.zookeeper zookeeper3.5.7
保证 hadoop102、hadoop103、hadoop104服务器上 服务器上 Zookeeper集群服务端启动。
1.创建 ZooKeeper客户端
public class zkClient { // 注意:逗号左右不能有空格 private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181"; private int sessionTimeout = 2000; private ZooKeeper zkClient; @Before public void init() throws IOException { zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { // System.out.println("-------------------------------"); // List2.创建子节点children = null; // try { // children = zkClient.getChildren("/", true); // // for (String child : children) { // System.out.println(child); // } // // System.out.println("-------------------------------"); // } catch (KeeperException e) { // e.printStackTrace(); // } catch (InterruptedException e) { // e.printStackTrace(); // } } }); } }
@Test public void create() throws KeeperException, InterruptedException { String nodeCreated = zkClient.create("/atguigu", "ss.avi".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); }create 方法
String path, 节点路径byte data[], 存储内容List acl, 权限CreateMode createMode 节点类型 (持久 还是 临时节点)
acl----- 权限
持久节点持久序号节点临时节点临时序号节点Container节点TTL节点
3.获取子节点 并监听节点变化@Before public void init() throws IOException { zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() { @Override public void process(WatchedEvent watchedEvent) { }); } @Test public void getChildren() throws KeeperException, InterruptedException { Listchildren = zkClient.getChildren("/", true); for (String child : children) { System.out.println(child); } // 延时 Thread.sleep(Long.MAX_VALUE); }
注意设置的监听,只监听一次,如果重复监听,得设置监听得process再次注册4.判断 Znode是否存在
@Test public void exist() throws KeeperException, InterruptedException { Stat stat = zkClient.exists("/atguigu", false); System.out.println(stat==null? "not exist " : "exist"); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)