1.创建客户端节点
package
com.cevent.zookeeper.client;
import
org.apache.zookeeper.CreateMode;
import
org.apache.zookeeper.WatchedEvent;
import
org.apache.zookeeper.Watcher;
import
org.apache.zookeeper.ZooDefs.Ids;
import
org.apache.zookeeper.ZooKeeper;
import org.junit.Before;
import
org.junit.Test;
/**
* 通过客户端对集群进行增删改查操作
*
new ZooKeeper(connectString,
sessionTimeout, watcher)
* 连接地址的字符串 会话超时时间 监听事件
* 主机的间隔用,逗号分隔 毫秒计算:ms
* @author cevent
* @date 2020年4月19日
*/
public class ZookeeperClient {
//连接zookeeper的地址、端口号、会话超时时间、监听
private String connectString="hadoop202.cevent.com:2181,hadoop203.cevent.com:2181,hadoop204.cevent.com:2181";
private int sessionTimeout=2000;
private ZooKeeper zkClient=null;
//1.创建客户端:定义junit-before在子节点被创建之前调用获取客户端
@Before
public void zookeeperInit() throws
Exception{
System.out.println("初始化zookeeper成功了!");
zkClient= new ZooKeeper(connectString, sessionTimeout, new Watcher(){//新建监听,如果触发,即触发相应事件
@Override
public void process(WatchedEvent event) {
// 1.触发事件,事件类型及路径,节点初始化时触发
System.out.println("CEVENT
Zookeeper启动了!"+event.getType()+"-----"+event.getPath());
}
});
}
//2.创建子节点
@Test
public void createNode() throws
Exception{
//1.节点
/**
* 参数1:创建节点的路径
* 参数2:创建节点存储的数据
* 参数3:创建节点后,节点具备的权限,常用Ids.OPEN_ACL_UNSAFE
开放的权限
* 参数4:创建的模式类型
*/
String createZKNode=zkClient.create("/cevent", "se.avi".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(createZKNode);
}
}
2.terminal测试
3.获取子节点
package
com.cevent.zookeeper.client;
import
java.util.List;
import
org.apache.zookeeper.CreateMode;
import
org.apache.zookeeper.WatchedEvent;
import
org.apache.zookeeper.Watcher;
import
org.apache.zookeeper.ZooDefs.Ids;
import
org.apache.zookeeper.ZooKeeper;
import
org.junit.Before;
import
org.junit.Test;
import sun.awt.image.OffScreenImage;
/**
* 通过客户端对集群进行增删改查操作
*
new ZooKeeper(connectString,
sessionTimeout, watcher)
* 连接地址的字符串 会话超时时间 监听事件
* 主机的间隔用,逗号分隔 毫秒计算:ms
* @author cevent
* @date 2020年4月19日
*/
public class ZookeeperClient {
//连接zookeeper的地址、端口号、会话超时时间、监听
private String connectString="hadoop202.cevent.com:2181,hadoop203.cevent.com:2181,hadoop204.cevent.com:2181";
private int sessionTimeout=2000;
private ZooKeeper zkClient=null;
//1.创建客户端:定义junit-before在子节点被创建之前调用获取客户端
@Before
public void zookeeperInit() throws
Exception{
System.out.println("初始化zookeeper成功了!");
zkClient= new ZooKeeper(connectString, sessionTimeout, new Watcher(){//新建监听,如果触发,即触发相应事件
@Override
public void process(WatchedEvent event) {
// 1.触发事件,事件类型及路径
System.out.println("CEVENT
Zookeeper启动了!"+event.getType()+"-----"+event.getPath());
}
});
}
//2.创建子节点
@Test
public void createNode() throws
Exception{
//1.节点
/**
* 参数1:创建节点的路径
* 参数2:创建节点存储的数据
* 参数3:创建节点后,节点具备的权限,常用Ids.OPEN_ACL_UNSAFE
开放的权限
* 参数4:创建的模式类型
*/
String createZKNode=zkClient.create("/cevent", "se.avi".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(createZKNode);
}
//3.获取子节点
@Test
public void getNodes() throws
Exception{
List<String> childNodes= zkClient.getChildren("/", false);
for(String child:childNodes){
System.out.println("获取到的子节点:"+child);
}
}
}
4.terminal测试
5.判断节点是否存在
//4.判断节点是否存在
@Test
public void nodeIsExist() throws
Exception{
Stat isExiStat=zkClient.exists("/cevent", true);
System.out.println(isExiStat==null?"不存在":"/cevent路径存在");
}
6.terminal测试
7.监听实现
package com.cevent.zookeeper.client;
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.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import
org.junit.Before;
import
org.junit.Test;
import sun.awt.image.OffScreenImage;
/**
* 通过客户端对集群进行增删改查操作
*
new ZooKeeper(connectString,
sessionTimeout, watcher)
* 连接地址的字符串 会话超时时间 监听事件
* 主机的间隔用,逗号分隔 毫秒计算:ms
* @author cevent
* @date 2020年4月19日
*/
public class ZookeeperClient {
//连接zookeeper的地址、端口号、会话超时时间、监听
private String connectString="hadoop202.cevent.com:2181,hadoop203.cevent.com:2181,hadoop204.cevent.com:2181";
private int sessionTimeout=2000;
private ZooKeeper zkClient=null;
//1.创建客户端:定义junit-before在子节点被创建之前调用获取客户端
@Before
public void zookeeperInit() throws
Exception{
System.out.println("初始化zookeeper成功了!");
zkClient= new ZooKeeper(connectString, sessionTimeout, new Watcher(){//新建监听,如果触发,即触发相应事件
@Override
public void process(WatchedEvent event) {
// 1.触发事件,事件类型及路径
System.out.println("CEVENT
Zookeeper启动了!"+event.getType()+"-----"+event.getPath());
try {
Stat isExiStat=zkClient.exists("/cevent", true);
System.out.println(isExiStat==null?"不存在":"/cevent路径存在");
}
catch (KeeperException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
catch (InterruptedException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
});
}
//2.创建子节点
@Test
public void createNode() throws
Exception{
//1.节点
/**
* 参数1:创建节点的路径
* 参数2:创建节点存储的数据
* 参数3:创建节点后,节点具备的权限,常用Ids.OPEN_ACL_UNSAFE
开放的权限
* 参数4:创建的模式类型
*/
String createZKNode=zkClient.create("/cevent", "se.avi".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(createZKNode);
}
//3.获取子节点
@Test
public void getNodes() throws
Exception{
//参数1:路径 参数2:是否监听watch
List<String> childNodes= zkClient.getChildren("/", false);
for(String child:childNodes){
System.out.println("获取到的子节点:"+child);
}
}
//4.判断节点是否存在
@Test
public void nodeIsExist() throws Exception{
Stat isExiStat=zkClient.exists("/cevent", true);
System.out.println(isExiStat==null?"不存在":"/cevent路径存在");
//加入长时间挂起,容许其他线程采取行动
Thread.sleep(Long.MAX_VALUE);
}
}