<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>