前提:用VMWare搭建三台Centos虚拟机,配置主机名,本文三台虚拟机分别为hadoop102,hadoop103,hadoop104,配置hadoop集群及zookeeper集群,分别开启虚拟机中的zkServer.sh进程,电脑主机能与虚拟机通信情况下进行以下操作
1.创建一个maven工程
pom.xml文件如下:
<?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>cn.tod</groupId>
<artifactId>zookeeperDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
</dependencies>
</project>
2.在resources文件夹下配置log4j.properties
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
3.编写ZooKeeperClient.java类
package cn.tod;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
public class ZooKeeperClient {
private static String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
private static int sessionTimeout = 4000;
private ZooKeeper zkClient = null;
@Before
public void init() throws IOException {
zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
public void process(WatchedEvent watchedEvent) {
System.out.println(watchedEvent.getType() + "---" + watchedEvent.getPath());
try {
ZooKeeperClient.this.zkClient.getChildren("/", true);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
@After
public void close() throws InterruptedException {
zkClient.close();
}
@Test
public void create() throws KeeperException, InterruptedException {
String path = "/tod";
String data = "huashi";
// String nodeCreated = zkClient.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
// String nodeCreated = zkClient.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
String nodeCreated = zkClient.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(nodeCreated + " done!");
}
@Test
public void getChildren() throws KeeperException, InterruptedException {
String path = "/";
List<String> children = zkClient.getChildren( path, true);
for (String child : children) {
System.out.println(child);
}
// Thread.sleep(Long.MAX_VALUE);
}
@Test
public void exist() throws KeeperException, InterruptedException {
String path = "/tod";
Stat stat = zkClient.exists(path, false);
System.out.println(stat == null ? path + " no exit!" : path + " exit!");
}
@Test
public void delete() throws KeeperException, InterruptedException {
// String path = "/tod";
String path = "/tod0000000002";
zkClient.delete(path, zkClient.exists(path, false).getVersion());
}
@Test
public void set() throws KeeperException, InterruptedException {
String path = "/tod";
String data = "shenda";
Stat stat = zkClient.setData(path, data.getBytes(), zkClient.exists(path, false).getVersion());
System.out.println(stat);
}
@Test
public void get() throws KeeperException, InterruptedException {
String path = "/tod";
byte[] data = zkClient.getData(path, false, zkClient.exists(path, false));
System.out.println(new String(data));
}
}
PS:注意
private static int sessionTimeout = 4000;
因为zookeeper集群默认的连接时长是2000ms,所以客户端的连接时长必须比2000ms长,否则会报异常,如果不信,可以尝试一波,小坑。