ZooKeeper的示例代码

来源:互联网 发布:网络路演 编辑:程序博客网 时间:2024/05/14 17:22
<span style="font-size:18px;">import java.util.List;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.KeeperException;import org.apache.zookeeper.WatchedEvent;import org.apache.zookeeper.Watcher;import org.apache.zookeeper.ZooDefs.Ids;import org.apache.zookeeper.ZooKeeper;import org.apache.zookeeper.data.Stat;/** * ZooKeeper用到的类是ZooKeeper * @Date 2015年3月5日 * @Time 下午5:58:10 * @author chaigy * @company emotte */public class ZooKeepDeemo { public static void main(String[] args)throws Exception {  System.out.println("没有看到相应的代码操作,估计是我看的视频还不够的意思,只看到PPT里面有代码,我瞅瞅自己写看看能不能成功");  System.out.println("因为视频里面讲的一般都比较简单,现在还没有上手去做,所以我也就写几个简单的demo了 ");  //第一个参数是主机IP或者主机名  第二个参数是超时时间  第三个参数是 监听  ZooKeeper zooKeeper = new ZooKeeper("chaigy", 5000, new Watcher() {   public void process(WatchedEvent event) {    System.out.println("lalal"+event.getType()+event.getPath());   }  });   //我都不晓得这些设置是什么玩意   第一个是要建的文件,第二个是数据,第三个晓不晓得  第四个是创建的形式  //Znode有四种形式的目录节点,PERSISTENT、PERSISTENT_SEQUENTIAL、EPHEMERAL、EPHEMERAL_SEQUENTIAL  //添加  //createZnode(zooKeeper);  //获取  //getData(zooKeeper);  //删除  //zooKeeper.delete("/chaigy4", 0);  //修改  //zooKeeper.setData("/chaigy2", "呜呜呜呜".getBytes(), 0);  //findAll(zooKeeper);  System.out.println(zooKeeper.getChildren("/", true));  } private static void findAll(ZooKeeper zooKeeper) throws KeeperException,   InterruptedException {  List<String> children = zooKeeper.getChildren("/", true);  for (String string : children) {   System.out.println(string+"********");  } } private static void getData(ZooKeeper zooKeeper) throws KeeperException,   InterruptedException {  Stat stat = new Stat();  byte[] data = zooKeeper.getData("/chaigy4", true, stat);  System.out.println(new String(data)); } private static void createZnode(ZooKeeper zooKeeper)   throws KeeperException, InterruptedException {  zooKeeper.create("/chaigy4", "nihao".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); }}</span>

0 0
原创粉丝点击