使用 delete 命令,可以删除表中的特定单元格. 删除命令的语法如下:
delete ‘<table name>’, ‘<row>’, ‘<column name >’, ‘<time stamp>’
此处是删除特定单元格的示例.这里我们删除工资.
hbase(main):006:0> delete 'emp', '1', 'personal data:city', 1417521848375 0 row(s) in 0.0060 seconds
使用"deleteall"命令,可以删除行中的所有单元格.下面给出了deleteall命令的语法.
deleteall ‘<table name>’, ‘<row>’,
以下是"deleteall"命令的示例,其中我们将删除emp表的row1的所有单元格./p>
hbase(main):007:0> deleteall 'emp','1' 0 row(s) in 0.0240 seconds
使用扫描验证表格命令.删除表格后的表格快照如下:
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
您可以使用 HTable的 delete()方法从HBase表中删除数据上课.按照下面给出的步骤从表中删除数据.
配置类将HBase配置文件添加到其对象.您可以使用 HbaseConfiguration 类的 create()方法创建配置对象,如下所示.
Configuration conf = HbaseConfiguration.create();
你有一个名为 HTable 的类, HBase中Table的实现.此类用于与单个HBase表进行通信.在实例化此类时,它接受配置对象和表名作为参数.你可以实例化如下所示的HTable类.
HTable hTable = new HTable(conf, tableName);
通过传递删除类来实例化要删除的行的rowid,采用字节数组格式.您还可以将timestamp和Rowlock传递给此构造函数.
Delete delete = new Delete(toBytes("row1"));
您可以使用
delete.deleteColumn(Bytes.toBytes("personal"), Bytes.toBytes("name")); delete.deleteFamily(Bytes.toBytes("professional"));
通过传递删除删除所选数据实例到 HTable 类的 delete()方法,如下所示.
table.delete(delete);
删除数据后,关闭 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