进入zookeeper安装目录下的bin目录内,通过命令启动
./zkServer.sh start
通过命令查看节点状态
./zkServer.sh status
通过命令关闭
./zkServer.sh stop
Zookeeper的命令行工具类似于linux Shell。当Zookeeper服务启动以后,启动一个客户端,连接到Zookeeper服务。
进入zookeeper安装目录下的bin目录内:
./zkCli.sh -server master:2181
Zookeeper常用的shell命令:
命令基本语法 | 功能描述 |
---|---|
help | 显示所有操作命令 |
ls path [watch] | 使用 ls 命令来查看当前znode中所包含的内容 |
ls2 path [watch] | 查看当前节点数据并能看到更新次数等数据 |
create | 普通创建 -s 含有序列 -e 临时(重启或者超时消失) |
get path [watch] | 获得节点的值 |
set | 设置节点的具体值 |
stat | 查看节点状态 |
delete | 删除节点 |
rmr | 递归删除节点 |
在项目的pom.xml文件添加以下内容,以引入Zookeeper的Java API依赖包:
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.6</version>#与集群的版本一致
</dependency>
</dependencies>
package com.hadoop.zookeeper;
import org.apache.zookeeper.*;
import org.apache.zookeeper.ZooDefs.Ids;
import java.io.IOException;
public class CreatePath {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
//参数1:Zookeeper连接字符串 ip地址:端口,有多个的话需要全部写上,用逗号隔开
//参数2:连接超时实间
//参数3:观察者对象(回调方法)
ZooKeeper zk = new ZooKeeper("192.168.100.100:2181", 30000, new Watcher() {
public void process(WatchedEvent event) {
System.out.println(event.getState());
System.out.println(event.getType());
System.out.println(event.getPath());
}
});
//CreateMode.PERSISTENT:持久化
String path = zk.create("/fu","fxl".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(path);
}
}
在idea控制台看到:
在zookeeper shell 里查看创建成功:
package com.hadoop.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
public class CreatePath {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
//参数1:Zookeeper连接字符串 ip地址:端口,有多个的话需要全部写上,用逗号隔开
//参数2:连接超时实间
//参数3:观察者对象(回调方法)
ZooKeeper zk = new ZooKeeper("192.168.100.100:2181",30000,null);
//参数1:节点路径
//参数2:需要修改的元数据,并转成字节数组
//参数3:版本号,-1代表所有版本
Stat stat = zk.setData("/fu","hello Zookeeper".getBytes(),-1);
//输出节点版本号
System.out.println(stat.getVersion());
}
}
package com.hadoop.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
public class CreatePath {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
//参数1:Zookeeper连接字符串 ip地址:端口,有多个的话需要全部写上,用逗号隔开
//参数2:连接超时实间
//参数3:观察者对象(回调方法)
ZooKeeper zk = new ZooKeeper("192.168.100.100:2181",30000,null);
Stat stat = new Stat();
//返回指定路径上的节点数据和节点状态,节点的状态会放入stat对象中
byte[] bytes = zk.getData("/fu",null,stat);
//输出节点元数据
System.out.println(new String(bytes));
}
}
package com.hadoop.zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
public class CreatePath {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
//参数1:Zookeeper连接字符串 ip地址:端口,有多个的话需要全部写上,用逗号隔开
//参数2:连接超时实间
//参数3:观察者对象(回调方法)
ZooKeeper zk = new ZooKeeper("192.168.100.100:2181",30000,null);
//删除节点
zk.delete("/fu",-1);
}
}