为了账号安全,请及时绑定邮箱和手机立即绑定

一起学HBase——总结HBase中的PUT、GET、DELETE操作

标签:
Hbase

统的关系型数据库有CRUD增删改查操作,同样对于NoSQL列式数据库也有CRUD操作。本文对HBase中常用的Scan、GET、PUT、DELETE操作的用法做个总结。

Put操作

Put相当于传统数据库的add操作,就是在数据库中添加一条或多条记录。
Put操作分为两类,一类是一次操作一条记录,另外一类是一次操作多条数据。

HBase提供一个Put类,通过该类的对象就可以在HBase中添加数据。

Put类提供的构造函数如下:

Put(byte[] row)  
Put(byte[] row,RowLock rowLock)  
Put(byte[] row,long ts)  
Put(byte[] row,long ts,RowLock rowLock)

通过上面的构造函数可以知道在创建对象时需要提供一个byte[]类型的参数row,该参数的主要作用是作为一行数据的行键(row key),和关系型数据库中的主键是一样的,是一行数据的唯一键。

Put提供如下的add方法将数据写入到HBase中:

Put add(byte[] family,byte[] qualifier,byte[] value)  
Put add(byte[] family,byte[] qualifier,long ts,byte[] value)  
Put add(KeyValuee kv)throws IOException

使用Put add(byte[] family,byte[] qualifier,byte[] value)方法添加数据时,因为没有指定时间戳,因此时间戳由region服务器提供。

使用Put add(byte[] family,byte[] qualifier,long ts,byte[] value) 方法添加数据时,由时间戳由参数ts指定。

使用Put add(KeyValue kv)throws IOException添加数据时,将行键、列族、列限定符、时间戳和单元格值封装为一个KeyValue对象。

将一行数据输入到HBase中的代码:

public class HBasePut{    public static void main(String[] args)throws IOExcetion{        //通过加载hbase-default.xml和hbase-site.xml文件的配置生成Configuration对象
        Configuration conf = HBaseConfiguration.create();        
        //创建HTable对象,用于操作HBase中的bigdata17_table表
        HTable table = new HTable(conf,"bigdata17_table");        //创建Put对象,并制定这行数据的行键bigdata17_row1
        Put put = new Put(Bytes.toBytes("bigdata17_row1"));        //添加名为colfam1:qual1列,并设置值为val1
        put.add(Bytes.toBytes("colfaml"),Bytes.toBytes("qual1"),Bytes.toBytes("val1"));        //将数据插入到表bigdata17_table
        table.put(put);
    }
}

将多行数据插入到HBase表中的代码:

List<Put> puts = new ArrayList<Put>();

Put put1 =  new Put(Bytes.toBytes("bigdata17_row1"));
put1.add(Bytes.toBytes("colfaml"),Bytes.toBytes("qual1"),Bytes.toBytes("val1"));
puts.add(put1);

Put put2 =  new Put(Bytes.toBytes("bigdata17_row2"));
put2.add(Bytes.toBytes("colfam2"),Bytes.toBytes("qual2"),Bytes.toBytes("val2"));
puts.add(put2);

Put put3 =  new Put(Bytes.toBytes("bigdata17_row2"));
put3.add(Bytes.toBytes("colfam1"),Bytes.toBytes("qual2"),Bytes.toBytes("val3"));
puts.add(put3);

table.put(puts);

和一次添加一条数据的区别是,使用了ArrayList列表,将要插入的多条数据存储于ArrayList中。

Get操作

get操作用于从HBase中读取数据,和Put一样,也有两类操作,一次读取一条记录,一次读取多条记录。

Get提供的构造函数如下的:
Get(byte[] row)
Get(byte[] row,RowLock rowLock)
通过上述的构造函数可知在创建Get对象时必须指定行键。RowLock参数的Get构造函数允许用户设置行锁。

HBase读取数据的方法:
Result get(Get get)throws IOException

Get类提供给了如下的方法用于设置精确坐标读取某个单元格的数据:

Get addFamily(byte[] family)Get addColumn(byte[] family,byte[] qualifier)Get setTimeRange(long minStamp,long maxStamp)throws IOExceptionGet setTimeStamp(long timestamp)Get setMaxVersions()Get setMaxVersions(int maxVersion)throws IOException

addFamily方法限制了一次只能读取一个指定的列族,多次调用可以取得多个列族。
addColumn方法用于获取指定列的数据。
setTimeStamp方法限制了读取指定时间戳的数据。
setTimeRange用于读取某个时间戳范围内的数据。
setMaxVersions()方法读取当前最大版本的数据。
setMaxVersions(int maxVersion)throws IOException方法用于读取指定版本的数据。

get方法读取到最新版本的数据,也是版本号最大的数据。在调用get方法之前调用setMaxVersions(int maxVersion),就可以读取到所有版本的数据。

读取一行数据的代码:

Configuration conf = HBaseConfiguration().create();
HTable table = new HTable(conf,"bigdata17_table");Get get = new Get(Bytes.toBytes("bigdata17_row1"));get.addColumn(Bytes.toBytes("colfam1"),Bytes.toBytes("qual1"));
Result result = table.get(get);byte[] val = result.getValue(Bytes.toBytes("colfam1"),Bytes.toBytes("qual1"));
System.out.println("Value:" + Bytes.toString(val));

HBase提供读取多行数据的方法:
Result[] get(List gets)throws IOException

下面是读取多行数据的代码:

byte[] colfam1 = Byte.toBytes("colfam1");byte[] qual1 = Byte.toBytes("qual1");byte[] qual2 = Byte.toBytes("qual2");byte[] row1 = Byte.toBytes("bigdata17Row1");byte[] row2 = Byte.toBytes("bigdata17Row2");

Configuration conf = HBaseConfiguration().create();
HTable table = new HTable(conf,"bigdata17_table");

List<Get> gets = new ArrayList<Get>();
Get get1 = new Get(row1);
get1.addColumn(colfam1,qual1);
gets.add(get1);

Get get2 = new Get(row2);
get2.addColumn(colfam1,qual1);
gets.add(get2);

Get get3 = new Get(row3);
get3.addColumn(colfam1,qual2);
gets.add(get3);

Result[] results = table.get(gets);//循环results数组,读取每个返回的Result结果for(Result result : results){
    String row = Bytes.toString(result.getRow());    byte[] val1 = null;    if(result.containColumn(colfam1,qual1)){
        val1 = result.getValue(colfam1,qual1);
        System.out.println("Value1:" + Bytes.toString(val1));
    }    
    byte[] val2 = null;    if(result.containColumn(colfam1,qual2)){
        val2 = result.getValue(colfam1,qual2);
        System.out.println("Value2:" + Bytes.toString(val2));
    }
}//使用KeyValue方式迭代Results中的数据for(Result result : results){    for(KeyValue kv : result.raw()){
        System.out.println("Row:" + Bytes.toString(kv.getRow()) + " Value:" + Bytes.toString(kv.getValue()));
    }
}

Delete操作

HBase同样也提供删除数据的操作,由Delete类来执行。

Delete类提供了两个构造函数:
Delete(byte[] row)
Delete(byte[] row,long timestamp,RowLock rowLock)

Delete类提供了如下的方法用于在删除数据的过程中缩小数据的范围,类似关系型数据库的where条件:

Delete deleteFamily(byte[] family)删除整个列族的所有版本数据  
Delete deleteFamily(byte[] family,long timesamp)删除时间戳早于给定timesamp的数据,包含timesamp。  
Delete deleteColumns(byte[] family,byte[] qualifier)删除列族中指定列的所有版本数据。  
Delete deleteColumns(byte[] family,byte[] qualifier,long timestamp)删除和时间戳版本匹配的数据以及比指定时间戳早的版本的数据Delete deleteColumn(byte[] family,byte[] qualifier)只删除指定列中的最新版本的数据Delete deleteColumn(byte[] family,byte[] qualifier,long timestamp)只删除指定列中的和timpstamp匹配版本的数据。void setTimestamp()设置时间戳,这个方法在调用其他3种方法时经常被忽略。但是如果不指定列族或列,则次调用与删除整行不同,它会删除匹配时间戳的或者比给定时间戳旧的所有列族中的所有列。

deleteColumns()和deleteColumn()的区别是,前者删除多个版本的数据,后者只删除指定版本的数据。

Delete同样也提供删除单行和多行的数据。

删除单条数据的代码如下:

Delete delete = new Delete(Bytes.toBytes(bigdata1iRow1));

delete.setTimestamp(1);

delete.deleteColumn(Bytes.toBytes("colfam1"),Bytes.toBytes("qual1"),1);

delete.deleteColumns(Bytes.toBytes("colfam2"),Bytes.toBytes("qual1"));

delete.deleteColumn(Bytes.toBytes("colfam2"),Bytes.toBytes("qual3"),15);

delete.deleteFamily(Bytes.toBytes("colfam3"));

delete.deleteFamily(Bytes.toBytes("colfam3"),3);

table.delete(delete);

删除多条数据的代码:

List<Delete> deletes = new ArrayList<Delete>();

Delete delete1 = new Delete(Bytes.toBytes("bigdata17Row1"));
delete1.setTimestamp(4);
deletes.add(delete1);

Delete delete2 = new Delete(Bytes.toBytes("bigdata17Row2"));
delete2.deleteColumn(Bytes.toBytes("colfam1"),Bytes.toBytes("qual1"),1);
deletes.add(delete2);

Delete delete3 = new Delete(Bytes.toBytes("bigdata17Row3"));delete.deleteFamily(Bytes.toBytes("colfam3"));delete.deleteFamily(Bytes.toBytes("colfam3"),3);
deletes.add(delete3);

table.delete(deletes);


作者:Summer哥 
出处:www.bigdata17.com 


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
JAVA开发工程师
手记
粉丝
33
获赞与收藏
206

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消