zookeeper
zookeeper概念
zookeeper 是 apache hadoop 项目下的一个子项目,是一个树形目录服务。
zookeeper 翻译过来就是 动物园管理员,他是用来管 hadoop(大象)、hive(蜜蜂)、pig(小 猪)的管理员。简称zk
zookeeper 是一个分布式的、开源的分布式应用程序的协调服务。
zookeeper 提供的主要功能包括:
配置管理:对项目的公共配置文件进行统一管理
分布式锁:一个数据库可以被多个对象使用加锁,对象的其他属性不能使用这个数据库
集群管理:可以管理多个对象
zookeeper 安装与配置
下载安装
环境准备
zookeeper服务器是用java创建的,它运行在jvm之上。需要安装jdk 7或更高版本。
上传
将下载的zookeeper放到/opt/zookeeper目录下
#上传zookeeper alt+p
put f:/setup/apache-zookeeper-3.5.6-bin.tar.gz
#打开 opt目录
cd /opt
#创建zookeeper目录
mkdir zookeeper
#将zookeeper安装包移动到 /opt/zookeeper
mv apache-zookeeper-3.8.6-bin.tar.gz /opt/zookeeper/
解压
将tar包解压到/opt/zookeeper目录下
tar -zxvf apache-zookeeper-3.8.6-bin.tar.gz
配置启动
配置zoo.cfg
进入到conf目录拷贝一个zoo_sample.cfg并完成配置
#进入到conf目录
cd /opt/zookeeper/apache-zookeeper-3.8.6-bin/conf/
#拷贝
cp zoo_sample.cfg zoo.cfg
修改zoo.cfg
#打开目录
cd /opt/zookeeper/
#创建zookeeper存储目录
mkdir zkdata
#修改zoo.cfg
vim /opt/zookeeper/apache-zookeeper-3.5.6-bin/conf/zoo.cfg
修改存储目录:datadir=/opt/zookeeper/zkdata
port在后面会用到
启动zookeeper
cd /opt/zookeeper/apache-zookeeper-3.8.6-bin/bin/
#启动
./zkserver.sh start
started代表成功
查看zookeeper状态
./zkserver.sh status
zookeeper启动成功。standalone代表zk没有搭建集群,现在是单节点
如果zookeeper没有启动
zookeeper 命令操作
数据模型
zookeeper 是一个树形目录服务,其数据模型和unix的文件系统目录树很类似,拥有一个层次化结构。
这里面的每一个节点都被称为: znode,每个节点上都会保存自己的数据和节点信息。
节点可以拥有子节点,同时也允许少量(1mb)数据存储在该节点之下。
节点可以分为四大类:
persistent 持久化节点
ephemeral 临时节点 :-e
persistent_sequential 持久化顺序节点 :-s
ephemeral_sequential 临时顺序节点 :-es
服务端命令
•启动 zookeeper 服务: ./zkserver.sh start
•查看 zookeeper 服务状态: ./zkserver.sh status
•停止 zookeeper 服务: ./zkserver.sh stop
•重启 zookeeper 服务: ./zkserver.sh restart
zookeeper客户端常用命令
•连接zookeeper服务端
./zkcli.sh –server ip:port
•断开连接
quit
•查看命令帮助
help
•显示指定目录下节点
ls 目录
•创建节点
create /节点path value
•获取节点值
get /节点path
•设置节点值
set /节点path value
•删除单个节点
delete /节点path
•删除带有子节点的节点
deleteall /节点path
创建临时有序节点
•创建临时节点
create -e /节点path value
•创建顺序节点
create -s /节点path value
•查询节点详细信息
ls –s /节点path
•czxid:节点被创建的事务id
•ctime: 创建时间
•mzxid: 最后一次被更新的事务id
•mtime: 修改时间
•pzxid:子节点列表最后一次被更新的事务id
•cversion:子节点的版本号
•dataversion:数据版本号
•aclversion:权限版本号
•ephemeralowner:用于临时节点,代表临时节点的事务id,如果为持久节点则为0
•datalength:节点存储的数据的长度
•numchildren:当前节点的子节点个数
zookeeper javaapi 操作
curator介绍
•curator 是 apache zookeeper 的java客户端库。
•常见的zookeeper java api :
•原生java api
•zkclient
•curator
•curator 项目的目标是简化 zookeeper 客户端的使用。
•curator 最初是 netfix 研发的,后来捐献了 apache 基金会,目前是 apache 的顶级项目。
•官网:http://curator.apache.org/
javaapi操作建立连接
1,搭建项目
创建项目curator-zk
引入pom和日志文件
资料文件夹下pom.xml和log4j.properties
2、创建测试类,使用curator连接zookeeper
@before
public void testconnect() {
//重试策略
retrypolicy retrypolicy = new exponentialbackoffretry(3000, 10);
//2.第二种方式
//curatorframeworkfactory.builder();
curatorframework client = curatorframeworkfactory.builder()
.connectstring("127.0.0.1:2181")
.sessiontimeoutms(60 * 1000)
.connectiontimeoutms(15 * 1000)
.retrypolicy(retrypolicy)
.namespace("bukaedu")
.build();
//开启连接
client.start();
}
创建节点
/**
* 创建节点:create 持久 临时 顺序 数据
* 1. 基本创建 :create().forpath("")
* 2. 创建节点 带有数据:create().forpath("",data)
* 3. 设置节点的类型:create().withmode().forpath("",data)
* 4. 创建多级节点 /app1/p1 :create().creatingparentsifneeded().forpath("",data)
*/
@test
public void testcreate() throws exception {
//2. 创建节点 带有数据
//如果创建节点,没有指定数据,则默认将当前客户端的ip作为数据存储
string path = client.create().forpath("/app2", "hehe".getbytes());
system.out.println(path);
}@test
public void testcreate2() throws exception {
//1. 基本创建
//如果创建节点,没有指定数据,则默认将当前客户端的ip作为数据存储
string path = client.create().forpath("/app1");
system.out.println(path);
}
@test
public void testcreate3() throws exception {
//3. 设置节点的类型
//默认类型:持久化
string path = client.create().withmode(createmode.ephemeral).forpath("/app3");
system.out.println(path);
}
@test
public void testcreate4() throws exception {
//4. 创建多级节点 /app1/p1
//creatingparentsifneeded():如果父节点不存在,则创建父节点
string path = client.create().creatingparentsifneeded().forpath("/app4/p1");
system.out.println(path);
}
查询节点
/**
* 查询节点:
* 1. 查询数据:get: getdata().forpath()
* 2. 查询子节点: ls: getchildren().forpath()
* 3. 查询节点状态信息:ls -s:getdata().storingstatin(状态对象).forpath()
*/
@test
public void testget1() throws exception {
//1. 查询数据:get
byte[] data = client.getdata().forpath("/app1");
system.out.println(new string(data));
}
@test
public void testget2() throws exception {
// 2. 查询子节点: ls
list<string> path = client.getchildren().forpath("/");
system.out.println(path);
}
@test
public void testget3() throws exception {
stat status = new stat();
system.out.println(status);
//3. 查询节点状态信息:ls -s
client.getdata().storingstatin(status).forpath("/app1");
system.out.println(status);
}
修改节点
/**
* 修改数据
* 1. 基本修改数据:setdata().forpath()
* 2. 根据版本修改: setdata().withversion().forpath()
* * version 是通过查询出来的。目的就是为了让其他客户端或者线程不干扰我。
*
* @throws exception
*/
@test
public void testset() throws exception {
client.setdata().forpath("/app1", "buka".getbytes());
}
@test
public void testsetforversion() throws exception {
stat status = new stat();
//3. 查询节点状态信息:ls -s
client.getdata().storingstatin(status).forpath("/app1");
int version = status.getversion();//查询出来的 3
system.out.println(version);
client.setdata().withversion(version).forpath("/app1", "hehe".getbytes());
}
发表评论