hadoop源码学习(二)之ZooKeeper

   要能够熟练使用hadoop,就得对其原理和源码有些了解。hadoop中比较重要的概念是NameNode,DataNode,去看这些类时,又会发现其使用了ZooKeeper包,这样就可以将hadoop的分解为若干个部分,理解起来就会稍微简单些了。

  可以理解zookeeper封装了一个数据树。

  zookeeper既提供了一个程序可以跑起来,也提供了一个java类库,用户可以调用,使用zookeeper,对于java程序员,这都是非常友好的。

// import java classes
import java.io.IOException;
import java.util.concurrent.CountDownLatch;

// import zookeeper classes
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.AsyncCallback.StatCallback;
import org.apache.zookeeper.KeeperException.Code;
import org.apache.zookeeper.data.Stat;

public class ZooKeeperConnection {

   // declare zookeeper instance to access ZooKeeper ensemble
   private ZooKeeper zoo;
   final CountDownLatch connectedSignal = new CountDownLatch(1);

   // Method to connect zookeeper ensemble.
   public ZooKeeper connect(String host) throws IOException,InterruptedException {
	
    // ZooKeeper中Watcher和WatchedEvent的作用。
  
// 可以看到这是异步获取zookeeper,需要WatchedEvent触发 zoo = new ZooKeeper(host,5000,new Watcher() { public void process(WatchedEvent we) { if (we.getState() == KeeperState.SyncConnected) { connectedSignal.countDown(); } } }); connectedSignal.await(); return zoo; } // Method to disconnect from zookeeper server public void close() throws InterruptedException { zoo.close(); } }

  学习zookeeper产品,就得从Zookeeper的类开始,可以看到,这个连接过程,使用了zookeeper的很多代码库,对于学习zookeeper还是很有帮助的。

  使用Zookeeper的监听机制,是否要和Zookeeper一直保持连接?是的?或许不是。

参考文档:https://blog.csdn.net/hll19950830/article/details/80241189

https://www.w3cschool.cn/zookeeper/zookeeper_api.html

posted @ 2019-03-02 22:12  懂得了才能做一些改变  阅读(163)  评论(0编辑  收藏  举报