首先用Java操作zookeeper之前需要先创建一个Maven项目,然后导入如下依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>zookeeper3</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
java客户端操作zookeeper基础命令
public class Demo {
static ZooKeeper zooKeeper ;
public static void main(String[] args) throws Exception {
zooKeeper = new ZooKeeper("linux01:2181,linux02:2181,linux03:2181",2000,null);
byte[] data = zooKeeper.getData("/hello0000000000", null, null);
String string = new String(data);
System.out.println(s);
String s = data.toString();
System.out.println(s);
修改节点的value,修改的信息需要以byte数组的形式写出
zooKeeper.setData("/hello0000000000","haha".getBytes(),-1);
zooKeeper.create("/h0000000002/h1","hehe".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zooKeeper.create("/h0000000002/h1/h2","hehe".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
zooKeeper.create("/h0000000002/h1/h2/h3","hehe".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
List<String> children = zooKeeper.getChildren("/h0000000002", null);
for (String child : children) {
System.out.println(child);
}
删除没有子节点的的节点
zooKeeper.delete("/h0000000003",-1);
rmr("/h0000000002");
zooKeeper.close();
}
public static void rmr(String path) throws Exception {
List<String> children = zooKeeper.getChildren(path, null);
if (children!=null&&children.size()>0){
for (String child : children) {
rmr(path+"/"+child);
}
}
zooKeeper.delete(path,-1);
}
}
java客户端操作zookeeper监听节点value数据变化
public class Demo02 {
static ZooKeeper zooKeeper;
static {
try {
zooKeeper = new ZooKeeper("linux01:2181", 2000, null);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
if (zooKeeper==null){
System.out.println("000");
}
byte[] data = zooKeeper.getData("/hello0000000000", new Watcher() {
public void process(WatchedEvent event) {
System.out.println(event.getPath()+"节点数据发生了变化:"+event.getType());
byte[] data1 = new byte[0];
try {
data1 = zooKeeper.getData("/hello0000000000", this, null);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("变化后的数据为:"+new String(data1));
}
}, null);
System.out.println("原始数据:"+new String(data));
Thread.sleep(Integer.MAX_VALUE);
zooKeeper.close();
}
}
java客户端操作zookeeper,模拟监听Namenode主节点中从节点datanode数量的变化
public class NameNode {
static ZooKeeper zk = null;
public void register() throws Exception {
zk = new ZooKeeper("linux01:2181,linux02:2181,linux03:2181", 2000, null);
}
public void datanodeList() throws Exception {
List<String> list = zk.getChildren("/Namenode", new Watcher() {
public void process(WatchedEvent event) {
try {
List<String> children = zk.getChildren("/Namenode", this);
List<String> hosts = new ArrayList<String>();
for (String datanode : children) {
byte[] data = zk.getData("/Namenode/" + datanode, null, null);
hosts.add(new String(data));
}
System.out.println("当前正在服务的datanode有:" + hosts);
} catch (Exception e) {
e.printStackTrace();
}
}
});
List<String> hosts = new ArrayList<String>();
for (String datanode : list) {
byte[] data = zk.getData("/Namenode/" + datanode, null, null);
hosts.add(new String(data));
}
System.out.println("当前正在服务的datanode有:" + hosts);
}
public void service() throws InterruptedException {
System.out.println("正在服务");
Thread.sleep(Integer.MAX_VALUE);
}
public static void main(String[] args) throws Exception {
NameNode nn = new NameNode() ;
nn.register();
nn.datanodeList();
nn.service();
}
}
public class DataNode {
static ZooKeeper zk = null;
public void register(String hostName) throws Exception {
zk = new ZooKeeper("linux01:2181,linux02:2181,linux03:2181", 2000, null);
zk.create("/Namenode/datanode",hostName.getBytes() , ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println("你的宝宝上线了。。。。"+hostName);
}
public void service() throws InterruptedException {
System.out.println("正在服务");
Thread.sleep(Integer.MAX_VALUE);
}
public static void main(String[] args) throws Exception {
DataNode dn = new DataNode() ;
dn.register(args[0]);
dn.register(args[1]);
dn.service();
}
}