How do you delete a row of data in HBase?
To delete a row of data in HBase, you can use the Delete operation. The steps to delete a row of data are as follows:
- Create a Delete object, specifying the row key to be deleted.
- Create a new object named delete and initialize it with the row key “rowkey”.
- You can choose to delete all columns in a specified column family or selected columns within a specified column family.
- Delete all columns in the specified column family: delete.addFamily(Bytes.toBytes(“columnFamily”));
- Delete certain columns in a specified column family: delete.addColumn(Bytes.toBytes(“columnFamily”), Bytes.toBytes(“columnQualifier”));
- Call the delete method of the Table to perform the deletion operation.
- Create a table object by connecting to a table with a specific name, then delete a specified record from that table.
The complete sample code is shown below:
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class HBaseDeleteExample {
public static void main(String[] args) throws Exception {
Connection connection = ConnectionFactory.createConnection();
Table table = connection.getTable(TableName.valueOf("tableName"));
// 创建一个Delete对象,指定要删除的行键
Delete delete = new Delete(Bytes.toBytes("rowkey"));
// 删除指定列族的所有列
delete.addFamily(Bytes.toBytes("columnFamily"));
// 删除指定列族中的某些列
delete.addColumn(Bytes.toBytes("columnFamily"), Bytes.toBytes("columnQualifier"));
// 删除数据
table.delete(delete);
table.close();
connection.close();
}
}
Please note that the above code needs to be replaced with your actual table name, row key, column family, and column qualifier.