ZooKeeper中API的简单使用

本文介绍了ZooKeeper的API使用,包括输出子节点、创建节点、判断节点存在、获取节点数据、设置节点数据、查询节点状态和删除节点等操作。通过示例展示了如何在客户端进行这些操作。
摘要由CSDN通过智能技术生成

Zookeeper的API

1,输出子节点
首先我们从客户端命令行中看到有以下子节点:
在这里插入图片描述则使用API操作:

package zyl;

import java.io.IOException;
import java.util.List;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.Before;
import org.junit.Test;

public class ZKCliet {
	private ZooKeeper zkCli;
	private static final String CONNECT_STRING = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
	private static final int SESSTION_TIMEOUT = 2000;

	@Before
	public void before() throws IOException {
		zkCli = new ZooKeeper(CONNECT_STRING, SESSTION_TIMEOUT, new Watcher() {

			public void process(WatchedEvent arg0) {
				// TODO Auto-generated method stub
				System.out.println("默认回调函数");

			}
		});
	}

	@Test
	public void ls() throws KeeperException, InterruptedException {
		List<String> children = zkCli.getChildren("/", true);
		System.out.println("=================输出");
		for (String chiString : children) {
			System.out.println(chiString);
		}
		System.out.println("=================结束");
	}

}

运行结果:
在这里插入图片描述2,创建节点

	// 创建子节点
	@Test
	public void create() throws Exception {

		// 参数1:要创建的节点的路径; 参数2:节点数据 ; 参数3:节点权限 ;参数4:节点的类型
		String nodeCreated = zkCli.create("/yulong", "BIT".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	}

客户端命令行中可以看到创建成功:
在这里插入图片描述3,判断节点是否存在:

	// 判断znode是否存在
	@Test
	public void exist() throws Exception {

		Stat stat = zkCli.exists("/eclipse", false);

		System.out.println(stat == null ? "not exist" : "exist");
	}

3,get

	@Test
	public void get() throws KeeperException, InterruptedException {
		byte[] data = zkCli.getData("/yulong", true, new Stat());

		String string = new String(data);

		System.out.println(string);

	}

4,set

	@Test
	public void set() throws KeeperException, InterruptedException {

		Stat stat = zkCli.setData("/yulong", "defabc".getBytes(), 0);

		System.out.println(stat.getDataLength());

	}

5,stat

    @Test
    public void stat() throws KeeperException, InterruptedException {
        Stat exists = zkCli.exists("/Idea", false);
        if (exists == null) {
            System.out.println("节点不存在");
        } else {
            System.out.println(exists.getDataLength());
        }
    }

6,delete

  @Test
    public void delete() throws KeeperException, InterruptedException {
        Stat exists = zkCli.exists("/zxx0000000007", false);
        if (exists != null)
            zkCli.delete("/zxx0000000007", exists.getVersion());
    }

整个project的pom文件:

<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>
		<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.10</version>
		</dependency>
</dependencies>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值