彷徨 | zookeeper的API编程操作(数据的增删改查以及节点的监控)

本文详细介绍了如何使用Java API在ZooKeeper中进行数据的创建、读取、更新和删除操作。通过实例演示了节点的创建、数据的获取与修改、子节点的检索以及节点的删除等关键功能。
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.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.ZooDefs.Ids;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * zookeeper的数据的增删改查
 * @author Administrator
 *
 */

public class ZKCRUD_DuanHaiTao {
	
	private static final String connectString = "hadoop01:2181,hadoop02:2181,hadoop03:2181,hadoop04:2181" ;
	
	private int sessionTimeout = 2000;
	
	ZooKeeper zkClient = null ;
	/**
	 * 初始化资源
	 * @throws Exception 
	 */
	@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) {
					e.printStackTrace();
				}
			}
		});
	}
	
	
	/**
	 * 数据的增加
	 */
	@Test
	public void testCreate() throws Exception{
		//1:路径 2:数据 3:权限 4:节点的类型 临时的和持久的(PERSISTENT_SEQUENTIAL EPHEMERAL_SEQUENTIAL)
		zkClient.create("/eclipse/e", "eclipse".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
		Thread.sleep(15000);
	}
	
	/**
	 * 获取子节点
	 * @throws Exception 
	 * @throws KeeperException 
	 */
	@Test
	public void getChildren() throws KeeperException, Exception {
		List<String> children = zkClient.getChildren("/", false);
		for (String string : children) {
			System.out.println(string);
		}
	}
	
	/**
	 * 判断节点是否存在
	 * @throws Exception 
	 * @throws KeeperException 
	 */
	@Test
	public void testExist() throws KeeperException, Exception {
		Stat exists = zkClient.exists("/eclipse", false);
		System.out.println(exists);
	}
	
	/**
	 * 获取节点数据
	 * @throws Exception 
	 * @throws KeeperException 
	 */
	@Test
	public void testGetData() throws KeeperException, Exception {
		byte[] data = zkClient.getData("/eclipse", false, null);
		System.out.println(new String(data));
	}
	
	/**
	 * 删除节点
	 * @throws Exception 
	 * @throws InterruptedException 
	 */
	@Test
	public void deleteData() throws InterruptedException, Exception {
		zkClient.delete("/w", -1);
	}
	
	/**
	 * 修改数据
	 * @throws Exception 
	 * @throws KeeperException 
	 */
	@Test
	public void setData() throws KeeperException, Exception {
		zkClient.setData("/w", "hello".getBytes(), -1);
	}
	
	/**
	 * 监听
	 * @throws Exception 
	 * @throws KeeperException 
	 */
	@Test
	public void watch() throws KeeperException, Exception {
		List<String> children = zkClient.getChildren("/", true);
		for (String string : children) {
			System.out.println(string);
		}
		Thread.sleep(2*60*1000);
	}
	
	/**
	 * 释放资源
	 * @throws Exception 
	 */
	@After
	public void close() throws Exception {
		zkClient.close();
		
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值