开发者学堂课程【分布式数据库 HBase 快速入门:删除数据】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/101/detail/1751
删除数据
内容简介:
1. 删除表的操作方法
2. 删除列的两种操作方法
3. 批量删除
1.删除表的操作方法
在命令行当中,删除操作有两种:
一种是删除某 rowkey 的全部数据,要用 deleteall;
hbase(main):016:0>deleteall ’student’,‘1001‘,
一种是删除某 rowkey 的某一列数据,要用 delete。
而在 API 中,删除操作的参数有:表名、rowkey、cf、cn。
Public static void delete(String tablename,String rowkey,String cf,String cn){
connection getTable()
public static void delete (String tableName, String rowKey, String cf,String cn) throws
I0Exception {
//获取 table 对象
Table table = connection. getTable (TableName. valueOf(tableName));
// 创建 delete 对象
Delete delete = new Delete (Bytes. toBytes(rowKey));
//执行删除操作
table. delete (delete);
table. close() ;
2.删除列的两种操作方法
删除列共有两种方法:
1)
delete. addColumns (Bytes. toBytes(cf), Bytes. toBytes(cn)) ;
2)
delete. addColumn (Bytes. toBytes(cf), Bytes. toBytes(cn)) ;
1)删除指定列的所有版本
* Delete all versions of the specified column.
* @param family family name
* @param qualifier column qualifier
* @return this for invocation chaining
*/
public Delete addColumns(final byte [ ] family, final byte [ ] qualifier){
addColumns(family,qualifier,this.ts);
return this;
2)删除指定列的最新版本
* Delete the latest version of the specified column.
This is an expensive call in that on the serverside, it first does a
* get to find the latest versions timestamp.Then it adds a delete using
* the fetched ce1ls timestamp.
* @param family family name
* @aram qualifier column qualifier
* @return this for invocation chaining
*/
public Delete addColumn(final byte [] family, final byte [ ] qualifier) {
this. deleteColumn (family, qualifier, this. ts);
return this;
在公司中建议用第一种放法,因为第二种方法在使用过程中,如果删除了最新版本,老版本的数据依然能被获取到。
在多版本情况下,使用第二种方法时,一定要加上时间戳。
3.批量删除
与 put 相同,一个 delete 里可以放多个列的数据,delete 对象与 rowkey 是一一对应的关系。