Hadoop(Day04) -- ZooKeeper

一.Introduction

Apache ZooKeeper is a software project of the Apache Software Foundation. It is essentially a distributed hierarchical key-value store, which is used to provide a distributed configuration service, synchronization service, and naming registry for large distributed systems.[2] ZooKeeper was a sub-project of Hadoop but is now a top-level Apache project in its own right.

ZooKeeper's architecture supports high availability through redundant services. The clients can thus ask another ZooKeeper leader if the first fails to answer. ZooKeeper nodes store their data in a hierarchical name space, much like a file system or a tree data structure. Clients can read from and write to the nodes and in this way have a shared configuration service. ZooKeeper can be viewed as an atomic broadcast system, through which updates are totally ordered. The ZooKeeper Atomic Broadcast (ZAB) protocol is the core of the system.[3]

ZooKeeper is used by companies including RackspaceYahoo!,[4] OdnoklassnikiReddit,[5] NetApp SolidFire[6] and eBay as well as open source enterprise search systems like Solr.[7]

ZooKeeper was originally developed at Yahoo for streamlining the processes running on Big Data cluster by storing the status in local log files on the ZooKeeper servers. These servers communicate with the client machines to provide them the information. ZooKeeper was developed in order to fix the bugs occurred while deploying distributed big data applications. Some of the prime features of Apache ZooKeeper are:

  • Reliable System: This system is very reliable as it keeps working even if a node fails.
  • Simple Architecture: The architecture of ZooKeeper is quite simple as there is a shared hierarchical namespace which helps coordinating the processes.
  • Fast Processing: Zookeeper is specially fast in "read-dominant" workloads (i.e. workloads in which reads are much more common than writes).
  • Scalable: The performance of ZooKeeper can be improved by adding nodes. --Wiki





二.Installation


1. Transer file under /usr/local

2. tar xzvf zookeeper-3.4.8.tar.gz

3. mv zookeeper-3.4.8 zookeeper

4. export ZOOKEEPER_HOME=/usr/local/zookeeper
    export PATH=$PATH:$JAVA_HOME/bin:$ZOOKEEPER_HOME/bin

5. srouce /etc/profile

6. cd /usr/local/zookeeper/conf/

   mv zoo_sample.cfg zoo.cfg

7. mkdir -p /usr/local/zookeeper/data
    mkdir -p /usr/local/zookeeper/log

8. Copy the following content in zoo.cfg:
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/usr/local/zookeeper/data
dataLogDir=/usr/local/zookeeper/log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=hadoop:2888:3888  

9. cd /usr/local/zookeeper/bin

zkServer.sh start

zkServer.sh stop


三.Pseudo Cluster:

1. vi zoo.sh

#!/bin/bash
mkdir /zoo_c
for i in `seq 1 5`
do
cp -r /usr/local/zookeeper /zoo_c/zoo$i
done

2. cd /zoo_c/zoo1/conf

vi zoo.conf

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial 
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between 
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=/zoo_c/zoo1/data
dataLogDir=/zoo_c/zoo1/log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the 
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
server.1=hadoop:2888:3888
server.2=hadoop:4888:5888
server.3=hadoop:6888:7888
server.4=hadoop:8888:9888
server.5=hadoop:10888:11888

3. 

4.


5.


Success:



四. Eclipse + ZooKeeper:

1.

2.


3. SimpleZkClient.java:

public class SimpleZkClient {

	private static final String connectString = "192.168.16.100:2181";
	private static final int sessionTimeout = 2000;

	ZooKeeper zkClient = null;

	@Before
	public void init() throws Exception {
		zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
			@Override
			public void process(WatchedEvent event) {
				// 收到事件通知後的回調函數(應該是我們自己的事件處理邏輯)
				System.out.println(event.getType() + "---" + event.getPath());
				try {
					zkClient.getChildren("/", true);
				} catch (Exception e) {
				}
			}
		});

	}

	/**
	 * 數據的增刪改查
	 * 
	 * @throws InterruptedException
	 * @throws KeeperException
	 */
	@Test
	// 創建數據節點到zk中
	public void testCreate() throws KeeperException, InterruptedException {
		// 參數1:要創建的節點的路徑 參數2:節點的數據 參數3:節點的權限 參數4:節點的類型
		//上傳的數據可以是任何類型,但都要轉成byte[]
		String nodeCreated = zkClient.create("/bsr", "hellozk".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	}

	//判斷znode是否存在
	@Test	
	public void testExist() throws Exception{
		Stat stat = zkClient.exists("/bsr", false);
		System.out.println(stat==null?"not exist":"exist");
		
		
	}
	
	// 獲取子節點
	@Test
	public void getChildren() throws Exception {
		List<String> children = zkClient.getChildren("/", true);
		for (String child : children) {
			System.out.println(child);
		}
		Thread.sleep(Long.MAX_VALUE);
	}

	//獲取znode的數據
	@Test
	public void getData() throws Exception {
		
		byte[] data = zkClient.getData("/bsr", false, null);
		System.out.println(new String(data));
		
	}
	
	//刪除znode
	@Test
	public void deleteZnode() throws Exception {
		
		//參數2:指定要刪除的版本,-1表示刪除所有版本
		zkClient.delete("/bsr", -1);
		
		
	}

}

4. Test:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章