ZooKeeper的示例代码

<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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值