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:

  1. Create a Delete object, specifying the row key to be deleted.
  2. Create a new object named delete and initialize it with the row key “rowkey”.
  3. You can choose to delete all columns in a specified column family or selected columns within a specified column family.
  4. Delete all columns in the specified column family: delete.addFamily(Bytes.toBytes(“columnFamily”));
  5. Delete certain columns in a specified column family: delete.addColumn(Bytes.toBytes(“columnFamily”), Bytes.toBytes(“columnQualifier”));
  6. Call the delete method of the Table to perform the deletion operation.
  7. 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.

bannerAds