Hbase 简明教程
HBase - Delete Data
Deleting a Specific Cell in a Table
使用 delete 命令,您可以在表中删除特定单元格。 delete 命令的语法如下所示:
delete ‘<table name>’, ‘<row>’, ‘<column name >’, ‘<time stamp>’
Deleting All Cells in a Table
使用 “deleteall” 命令,您可以删除一行中的所有 cell。下面给出了 deleteall 命令的语法。
deleteall ‘<table name>’, ‘<row>’,
Example
这是一个“deleteall”命令的示例,在这里我们正在删除 emp 表的第一行所有 cell。
hbase(main):007:0> deleteall 'emp','1'
0 row(s) in 0.0240 seconds
使用 scan 命令验证表。下面给出了删除表后的表快照。
hbase(main):022:0> scan 'emp'
ROW COLUMN + CELL
2 column = personal data:city, timestamp = 1417524574905, value = chennai
2 column = personal data:name, timestamp = 1417524556125, value = ravi
2 column = professional data:designation, timestamp = 1417524204, value = sr:engg
2 column = professional data:salary, timestamp = 1417524604221, value = 30000
3 column = personal data:city, timestamp = 1417524681780, value = delhi
3 column = personal data:name, timestamp = 1417524672067, value = rajesh
3 column = professional data:designation, timestamp = 1417523187, value = jr:engg
3 column = professional data:salary, timestamp = 1417524702514, value = 25000
Deleting Data Using Java API
您可以使用 HTable 类的 delete() 方法从 HBase 表中删除数据。按照以下步骤从表中删除数据。
Step 1: Instantiate the Configuration Class
Configuration 类将 HBase 配置文件添加到其对象中。您可以使用 HbaseConfiguration 类的 create() 方法创建配置对象,如下所示。
Configuration conf = HbaseConfiguration.create();
Step 2: Instantiate the HTable Class
你有一个名为 HTable 的类,是 Table 在 HBase 中的一个实现。该类用于与单个 HBase 表通信。在实例化该类时,它将接受配置对象和表名作为参数。你可以如下所示实例化 HTable 类。
HTable hTable = new HTable(conf, tableName);
Step 3: Instantiate the Delete Class
通过传递要删除的行 rowid(以字节数组格式)来实例化 Delete 类。您还可以将时间戳和 Rowlock 传递到此构造函数。
Delete delete = new Delete(toBytes("row1"));
Step 4: Select the Data to be Deleted
您可以使用 Delete 类的 delete 方法删除数据。这个类有各种删除方法。使用这些方法选择要删除的列或列系列。查看显示 Delete 类方法用法以下示例。
delete.deleteColumn(Bytes.toBytes("personal"), Bytes.toBytes("name"));
delete.deleteFamily(Bytes.toBytes("professional"));
Step 6: Close the HTableInstance
删除数据后,关闭 HTable 实例。
table.close();
下面给出从 HBase 表中删除数据的完整程序。
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;
public class DeleteData {
public static void main(String[] args) throws IOException {
// Instantiating Configuration class
Configuration conf = HBaseConfiguration.create();
// Instantiating HTable class
HTable table = new HTable(conf, "employee");
// Instantiating Delete class
Delete delete = new Delete(Bytes.toBytes("row1"));
delete.deleteColumn(Bytes.toBytes("personal"), Bytes.toBytes("name"));
delete.deleteFamily(Bytes.toBytes("professional"));
// deleting the data
table.delete(delete);
// closing the HTable object
table.close();
System.out.println("data deleted.....");
}
}
编译并执行上述程序,如下所示:
$javac Deletedata.java
$java DeleteData
输出应如下所示:
data deleted