2021-05-10

         ZooKeeper  Api

对于用Java编写程序,我们有ZooKeeper API ,其中我们有ZooKeeper类,该类中包含许多编写程序的方法。
connet():连接ZooKeeper Ensemble
create():创建znode
exists():检查znode是否存在
getData():得到一个znode的数据
setData():更新或设置一个znode的数据
getChildren():得到一个znode的所有子级
delete():删除znode
close():以适当的方式关闭连接

在IDEA中创建一个Java项目:
1.选择File—>New–>Project
2.选择Java–>选择正确的Java版本,按下一步(next)
3.再按next,输入project name,然后按Finish
4.现在需要添加ZooKeeper jars.
5.选择File–>Project Structure–>modules–>Dependencies–>+(在右边)–>Jars and Directories.
6.从系统中选择Jars,按ok
7.选中jars的复选框,按Apply和ok
8.写程序

创建znode代码:

import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

public class Create_Znode {

public static void main(String[] args) {

    // We need the path and data to create the znode
    String path = "/a/p";
    String dataofZnode = "HUEL";
    byte[] data = dataofZnode.getBytes();
    // We need to create the connection
    // We need the hostname or ip and the port number.
    String port = "niit-china:2181";
    try {
        // To create the connection
        ZooKeeper zk = new ZooKeeper(port,5000,null);
        // to create the znode
        zk.create(path,data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("Znode has created successfylly");
        zk.close();

    } catch (IOException | KeeperException | InterruptedException e) {
        e.printStackTrace();
    }
}

}
检索子集的代码:
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;

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

public class Retrieve_Children {
public static void main(String[] args) {

    String path = "/a/b/c";
    String port = "niit-china:2181";
    // To store all children we will use ArrayList object.
    List<String> ChildNodes = new ArrayList<String>();
    try {
        ZooKeeper zk = new ZooKeeper(port,5000,null);
        if(zk!=null)
        {
            ChildNodes = zk.getChildren(path,false);
            System.out.println("Znode Children are :");
            // we will use the for-each loop to display all the elements from the
            // arraylist object
            for(String s:ChildNodes)
            {
                System.out.println(s);
            }
        }
    } catch (IOException | KeeperException | InterruptedException e) {
        e.printStackTrace();
    }
}

}

删除znode:

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

public class Delete_Znode {
public static void main(String[] args) {
// Path of the node
String path = “/a/p”;
// We need to make the connection: hostname:port , niit-china:2181
String port = “niit-china:2181”;
try {
ZooKeeper zk = new ZooKeeper(port,5000,null);
zk.delete(path,4);
System.out.println(“Znode is deleted successfully”);
} catch (IOException | InterruptedException | KeeperException e) {
e.printStackTrace();
}

}

}

检测znode存在及状态:

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;

public class Exist_Znode {
public static void main(String[] args) {
// Path of the node
String path = “/a/k”;
// We need to make the connection: hostname:port , niit-china:2181
String port = “niit-china:2181”;
try {
ZooKeeper zk = new ZooKeeper(port,5000,null);
Stat stat = zk.exists(path,false);
if(stat!=null)
{
System.out.println("Znode exists and the node version is: "+stat.getVersion());
}
else
{
System.out.println(“Znode do not exist”);
}

    } catch (IOException | KeeperException | InterruptedException e) {
        e.printStackTrace();
    }
}

}

得到数据:
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;

public class Get_Data {
public static void main(String[] args) {
// Path of the node
String path = “/a/k”;
// We need to make the connection: hostname:port , niit-china:2181
String port = “niit-china:2181”;

    try {
        ZooKeeper zk = new ZooKeeper(port,5000,null);
        Stat stat = zk.exists(path,false);
        if(stat!=null)
        {
            byte [] b = zk.getData(path,null,null);
            String data = new String(b,"UTF-8");
            System.out.println("Data of node at path: "+path+" ="+data);
        }
        else
        {
            System.out.println("Znode do not exist so no data available at the path");
        }
    } catch (IOException | KeeperException | InterruptedException e) {
        e.printStackTrace();
    }
}

}

设置或更新数据:
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

public class Set_Data {
public static void main(String[] args) {
// Path of the node
String path = “/a/k”;
// We need to make the connection: hostname:port , niit-china:2181
String port = “niit-china:2181”;
byte[] data = “HUEL-Henan”.getBytes();
try {
ZooKeeper zk = new ZooKeeper(port, 5000, null);
zk.setData(path, data, zk.exists(path, false).getVersion());
System.out.println(“Data updated successfully”);

        } catch (IOException | KeeperException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值