java根据zookeeper源码实现节点增删改查

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
 
import java.io.IOException;
import java.util.List;
 
/*
 */
public class MyzkTest {
    //zk的链接地址
    public static final String zkconnect = "服务器ip:2181";
    //超时时间
    public static final int timeout = 15000;
 
    public static void main(String[] args) throws Exception {
        ZooKeeper zooKeeper = connect();
        //create(zooKeeper,"/myzktest","yuanzhihui");
        //delete(zooKeeper,"/myzktest");
        //setdata(zooKeeper,"/myzktest","xiaoyuan");
        query(zooKeeper,"/myzktest");
    }
    //连接方法
    public static ZooKeeper connect() throws IOException {
        ZooKeeper zk = new ZooKeeper(zkconnect,timeout,null);
        System.out.println("zk连接成功");
        return zk;
    }
 
    //操作:添加
    public static void create(ZooKeeper zooKeeper,String node,String data) throws InterruptedException, KeeperException {
        System.out.println("开始创建节点:"+"节点数据为:"+data);
        List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
        CreateMode createMode = CreateMode.PERSISTENT;
        zooKeeper.create(node,data.getBytes(),acl,createMode);
        System.out.println("zk节点添加成功");
    }
 
    //操作:删除
    public static void delete(ZooKeeper zooKeeper,String node) throws Exception{
        //先查一下这个节点是否存在
        Stat stat =zooKeeper.exists(node,false);
        System.out.println("开始删除节点:"+node+"原来的版本号是"+stat.getVersion());
        zooKeeper.delete(node,stat.getVersion());
        System.out.println("节点删除成功!");
    }
 
    //、操作 修改
    public static void setdata(ZooKeeper zooKeeper,String node,String data) throws Exception{
        //查节点
        Stat stat = zooKeeper.exists(node,false);
        System.out.println("节点"+node+"原来版本号是"+stat.getVersion());
        zooKeeper.setData(node,data.getBytes(),stat.getVersion());
        System.out.println("修改成功");
    }
 
    //操作  查询
    public static void query(ZooKeeper zooKeeper,String node) throws Exception{
        String result = new String(zooKeeper.getData(node,false,null));
        System.out.println("准备查询数据:"+node+"节点数据为"+result);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮您回答这个问题。下面是使用 Java 操作 HBase 数据库的增删改查示例代码: 1. 导入必要的包 ``` import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.CellUtil; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; ``` 2. 创建 HBase 配置对象 ``` Configuration configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum", "localhost"); // HBase 的 ZooKeeper 地址 configuration.set("hbase.zookeeper.property.clientPort", "2181"); // HBase 的 ZooKeeper 端口号 ``` 3. 创建 HBase 连接对象和表对象 ``` Connection connection = ConnectionFactory.createConnection(configuration); Table table = connection.getTable(TableName.valueOf("test_table")); // test_table 是表名 ``` 4. 插入数据 ``` Put put = new Put(Bytes.toBytes("row1")); // row1 是行键名 put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"), Bytes.toBytes("value1")); // cf 是列族名,col1 是列名,value1 是值 table.put(put); ``` 5. 查询数据 ``` Get get = new Get(Bytes.toBytes("row1")); // row1 是行键名 Result result = table.get(get); for (Cell cell : result.listCells()) { String cf = Bytes.toString(CellUtil.cloneFamily(cell)); // 获取列族名 String col = Bytes.toString(CellUtil.cloneQualifier(cell)); // 获取列名 String value = Bytes.toString(CellUtil.cloneValue(cell)); // 获取值 System.out.println(cf + ":" + col + "=" + value); } ``` 6. 修改数据 ``` Put put = new Put(Bytes.toBytes("row1")); // row1 是行键名 put.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1"), Bytes.toBytes("new_value")); // cf 是列族名,col1 是列名,new_value 是新的值 table.put(put); ``` 7. 删除数据 ``` Delete delete = new Delete(Bytes.toBytes("row1")); // row1 是行键名 delete.addColumn(Bytes.toBytes("cf"), Bytes.toBytes("col1")); // cf 是列族名,col1 是列名 table.delete(delete); ``` 以上就是使用 Java 操作 HBase 数据库的增删改查示例代码,希望对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值