码迷,mamicode.com
首页 > 编程语言 > 详细

zookeeper生成节点、删除节点 For Java

时间:2015-01-11 23:03:31      阅读:556      评论:0      收藏:0      [点我收藏+]

标签:

源码地址https://github.com/Bellonor/myhadoop2.x/tree/master/myhadoop2.x/src/main/java/com/jamesfen/zookeeper

各个jar包依赖见maven 的pom文件

https://github.com/Bellonor/myhadoop2.x/tree/master/myhadoop2.x/src/main/java/com/jamesfen/zookeeper


package com.jamesfen.zookeeper;

import java.io.IOException;

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;

public class ZkNodeOperation {

	public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
		//String zkServer = "192.168.58.13:2181";

		ZooKeeper zk = new ZooKeeper("192.168.58.11:" + 2181, 3000,
				new Watcher() {
					// 监控所有被触发的事件
					public void process(WatchedEvent event) {
						System.out.println("已经触发了" + event.getType() + "事件!");
					}
				});
		// 创建一个目录节点
		zk.create("/panguoyuan-dir", "panguoyuan-data".getBytes(),
				Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		// 创建一个子目录节点
		zk.create("/panguoyuan-dir/childone", "childone".getBytes(),
				Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		System.out.println(new String(zk
				.getData("/panguoyuan-dir", false, null)));
		// 取出子目录节点列表
		System.out.println(zk.getChildren("/panguoyuan-dir", true));
		// 修改子目录节点数据
		zk.setData("/panguoyuan-dir/childone", "modifyDataOne".getBytes(), -1);
		System.out.println("目录节点状态:[" + zk.exists("/panguoyuan-dir", true)
				+ "]");
		// 创建另外一个子目录节点
		zk.create("/panguoyuan-dir/childtwo", "childtwo".getBytes(),
				Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
		System.out.println(new String(zk.getData("/panguoyuan-dir/childtwo",
				true, null)));
		// 删除子目录节点
		zk.delete("/panguoyuan-dir/childtwo", -1);
		zk.delete("/panguoyuan-dir/childone", -1);
		// 删除父目录节点
		zk.delete("/panguoyuan-dir", -1);
		// 关闭zk连接
		zk.close();
	}

}


zookeeper生成节点、删除节点 For Java

标签:

原文地址:http://blog.csdn.net/jameshadoop/article/details/42619735

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!