使用zkClient操作zookeeper

本文展示了一个使用Java实现的ZooKeeper客户端示例,包括连接配置、节点创建与删除、数据更新与读取等功能。通过一系列单元测试,详细介绍了如何在ZooKeeper中进行节点操作、监听数据变化及子节点变更。
package cn.bigdata;

import java.util.List;

import org.I0Itec.zkclient.DataUpdater;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.CreateMode;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestZKclient {
	private ZkClient zkClient = null;

	/**
	 * 创建zookeeper连接
	 */
	@Before
	public void connection() {
		// zookeeper地址和超时时间
		zkClient = new ZkClient("hadoop05:2181,hadoop06:2181,hadoop07:2181", 2000);
	}

	/**
	 * 关闭zookeeper连接
	 */
	@After
	public void close() {
		try {
			Thread.sleep(300);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		zkClient.close();
	}

	/**
	 * 创建单个节点
	 */
	@Test
	public void testCreateZnode() {
		String create = zkClient.create("/zkClient", "abc", CreateMode.PERSISTENT);
		System.out.println("创建节点:" + create);

	}

	/**
	 * 创建多个节点
	 */
	@Test
	public void testCreateMoreZnode() {
		zkClient.createPersistent("/zkClient/zkClient1/zkClient1-1", true);
		System.out.println("创建多层节点成功");
	}

	/**
	 * 删除节点
	 */
	@Test
	public void testDeleteZnode() {
		boolean delete = zkClient.delete("/zkClient");
		System.out.println("删除节点:" + delete);
	}

	/**
	 * 递归删除某个节点及其子节点 删除一个节点,和原生api一样,只能从最底层节点一个一个删除
	 */
	@Test
	public void testDeleteRecursiveZnode() {
		boolean deleteRecursive = zkClient.deleteRecursive("/zkClient");
		System.out.println("递归删除节点:" + deleteRecursive);
	}

	/**
	 * 更新节点
	 */
	@Test
	public void testUpdateZnode() {
		zkClient.updateDataSerialized("/zkClient", new DataUpdater<String>() {
			@Override
			public String update(String currentData) {
				// 返回之前znode的data
				System.out.println("currentData:" + currentData);
				// 设置新的data
				return "bbbbbbb";
			}
		});
	}

	/**
	 * 查询节点
	 */
	@Test
	public void testGetZnode() {
		List<String> children = zkClient.getChildren("/");
		for (String string : children) {
			System.out.println(string);
		}
	}

	/**
	 * 查询创建时间
	 */
	@Test
	public void testCreationTime() {
		long creationTime = zkClient.getCreationTime("/zkClient");
		System.out.println("/zkClient的创建时间:" + creationTime);
	}

	/**
	 * 查询节点内容
	 */
	@Test
	public void testGetData() {
		String readData = zkClient.readData("/zkClient", true);
		System.out.println("/zkClient的节点内容:" + readData);
	}

	/**
	 * 查询子节点 统计子节点个数
	 */
	@Test
	public void testChild() {
		int countChildren = zkClient.countChildren("/zkClient");
		System.out.println("/zkClient共有" + countChildren + "个子节点!");
		List<String> children = zkClient.getChildren("/zkClient");
		for (String string : children) {
			System.out.println(string);
		}
	}

	/**
	 * 注册节点更改监听 利用watch机制做订阅,使用异步操作处理节点
	 */
	@Test
	public void testDataChangesListener() {
		zkClient.subscribeDataChanges("/zkClient", new IZkDataListener() {
			@Override
			public void handleDataDeleted(String dataPath) throws Exception {
				System.out.println("删除节点" + dataPath + "成功");

			}

			@Override
			public void handleDataChange(String dataPath, Object data) throws Exception {
				System.out.println("节点名称:" + dataPath + "-->修改后的值:" + data);
			}
		});
		for (int i = 0; i < 5; i++) {
			zkClient.updateDataSerialized("/zkClient", new DataUpdater<String>() {
				@Override
				public String update(String currentData) {
					return currentData + 1;
				}
			});
		}
	}

	/**
	 * 注册子节点改变监听
	 */
	@Test
	public void testChildChangesListener() throws InterruptedException {
		zkClient.subscribeChildChanges("/zkClient", new IZkChildListener() {
			@Override
			public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
				System.out.println(parentPath + "子节点被修改!");
				for (String string : currentChilds) {
					System.out.println("现在为" + string);
				}
			}
		});
		for (int i = 0; i < 5; i++) {
			zkClient.create("/zkClient/test", "abc", CreateMode.EPHEMERAL_SEQUENTIAL);
			Thread.sleep(100);
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值