ZooKeeper API 開發
使用maven開發 ZooKeeper API
- 配置 pom.xml 文件
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
</dependencies>
- 開發實例
package codes.admin.bigdata.zk;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
import static java.lang.Long.MAX_VALUE;
public class ZkClient {
private ZooKeeper zkCli;
private static final String CONNECT_STRING = "hadoop141:2181,hadoop142:2181,hadoop143:2181";
private static final int SESSION_TIMEOUT = 2000;
@Before
public void before() throws IOException {
zkCli = new ZooKeeper(CONNECT_STRING, SESSION_TIMEOUT, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println(watchedEvent.getType());
try {
zkCli.getChildren("/data",true);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
@Test
public void ls() throws KeeperException, InterruptedException {
List<String> children = zkCli.getChildren("/", e->{
System.out.println("自定義回調函數!");
});
System.out.println("-------------------------");
for (String child:children
) {
System.out.println(child);
}
System.out.println("-------------------------");
Thread.sleep(MAX_VALUE);
}
@Test
public void create() throws InterruptedException, KeeperException {
String idea = zkCli.create("/Idea", "Idea2020".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(idea);
Thread.sleep(MAX_VALUE);
}
@Test
public void exist() throws KeeperException, InterruptedException {
Stat exists = zkCli.exists("/Idea0000000006", false);
System.out.println(exists == null ? "not exist" : "exist" );
}
@Test
public void get() throws KeeperException, InterruptedException {
byte[] data = zkCli.getData("/1230000000004", false, new Stat());
String dara = new String(data);
System.out.println(dara);
}
@Test
public void set() throws KeeperException, InterruptedException {
Stat stat = zkCli.setData("/234", "hello world".getBytes(), 0);
System.out.println(stat.getDataLength());
}
@Test
public void delete() throws KeeperException, InterruptedException {
Stat exists = zkCli.exists("/234", false);
if (exists != null){
zkCli.delete("/234",exists.getVersion());
}
}
public void register() throws KeeperException, InterruptedException {
byte[] data = zkCli.getData("/aa", new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
try {
register();
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, null);
System.out.println(new String(data));
}
@Test
public void testRegister() throws KeeperException, InterruptedException {
register();
Thread.sleep(MAX_VALUE);
}
}