hbase delete.deleteColumns问题

delete.deleteColumns是删除某个列簇里的所有时间戳版本

但是删除不了自己加入的时间戳

转载于:https://www.cnblogs.com/sunpengblog/p/11370284.html

好的,以下是用HBase设计的用户粉丝数据表userfollow的建表、插入数据和查询操作的代码实现: 1. 建表 首先需要创建一个Java项目,并导入HBase的相关依赖包。 然后可以使用HBaseAdmin类创建表: ```java Configuration conf = HBaseConfiguration.create(); HBaseAdmin admin = new HBaseAdmin(conf); HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf("userfollow")); tableDesc.addFamily(new HColumnDescriptor("follower")); tableDesc.addFamily(new HColumnDescriptor("followed")); admin.createTable(tableDesc); ``` 这里定义了一个名为“userfollow”的表,包含两个列族“follower”和“followed”。 2. 插入数据 接下来可以使用HBase的Put类插入数据: ```java Configuration conf = HBaseConfiguration.create(); HTable table = new HTable(conf, "userfollow"); // 用户Mary有一个粉丝Alice Put put1 = new Put(Bytes.toBytes("Mary")); put1.add(Bytes.toBytes("follower"), Bytes.toBytes("Alice"), Bytes.toBytes("1")); table.put(put1); // 用户Jack有两个粉丝Bob和Ben Put put2 = new Put(Bytes.toBytes("Jack")); put2.add(Bytes.toBytes("follower"), Bytes.toBytes("Bob"), Bytes.toBytes("1")); put2.add(Bytes.toBytes("follower"), Bytes.toBytes("Ben"), Bytes.toBytes("1")); table.put(put2); // Jack还关注了Mary Put put3 = new Put(Bytes.toBytes("Jack")); put3.add(Bytes.toBytes("followed"), Bytes.toBytes("Mary"), Bytes.toBytes("1")); table.put(put3); ``` 这里使用了Put类的add方法,将数据插入到表中。 3. 查询操作 可以使用HBase的Get类和Scan类来进行查询操作。 (1) 查看Jack被哪些人关注,关注了谁 ```java Configuration conf = HBaseConfiguration.create(); HTable table = new HTable(conf, "userfollow"); // 查看Jack被哪些人关注 Get get1 = new Get(Bytes.toBytes("Jack")); Result result1 = table.get(get1); byte[] followed = result1.getValue(Bytes.toBytes("followed"), Bytes.toBytes("Mary")); if (followed != null) { System.out.println("Jack关注了Mary"); } // 查看Jack有哪些粉丝 Scan scan = new Scan(); scan.addColumn(Bytes.toBytes("follower"), Bytes.toBytes("")); scan.setFilter(new PrefixFilter(Bytes.toBytes("Jack"))); ResultScanner scanner = table.getScanner(scan); for (Result result2 : scanner) { byte[] follower = result2.getRow(); System.out.println(Bytes.toString(follower) + "关注了Jack"); } scanner.close(); ``` 这里使用了Get类和Scan类,分别查询了Jack被哪些人关注和Jack有哪些粉丝。 (2) Bob取关了Jack ```java Configuration conf = HBaseConfiguration.create(); HTable table = new HTable(conf, "userfollow"); Delete delete = new Delete(Bytes.toBytes("Jack")); delete.deleteColumns(Bytes.toBytes("follower"), Bytes.toBytes("Bob")); table.delete(delete); ``` 这里使用了Delete类,将Bob从Jack的粉丝列表中删除。 (3) 扫描全表数据 ```java Configuration conf = HBaseConfiguration.create(); HTable table = new HTable(conf, "userfollow"); Scan scan = new Scan(); ResultScanner scanner = table.getScanner(scan); for (Result result : scanner) { byte[] row = result.getRow(); byte[] follower = result.getValue(Bytes.toBytes("follower"), null); byte[] followed = result.getValue(Bytes.toBytes("followed"), null); System.out.println(Bytes.toString(row) + "的粉丝:" + Bytes.toString(follower) + ",关注:" + Bytes.toString(followed)); } scanner.close(); ``` 这里使用了Scan类,扫描了全表数据并输出结果。 以上就是用HBase设计用户粉丝数据表userfollow的建表、插入数据和查询操作的代码实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值