How to delete data in HBase in bulk?

One common method in HBase for programmatically deleting data in bulk is supported.

  1. Create a list of Delete objects.
  2. Loop through the row keys of the data to be deleted, create a Delete object, and add it to a list of Delete objects.
  3. Use the delete method of Table to pass a list of Delete objects to it for the bulk deletion of data.

Below is an example code demonstrating how to use the Java API to batch delete data in HBase.

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
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 java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HBaseBulkDeleteExample {

    public static void main(String[] args) throws IOException {
        // 创建HBase配置
        Configuration conf = HBaseConfiguration.create();
        // 创建连接
        Connection connection = ConnectionFactory.createConnection(conf);
        // 获取表
        Table table = connection.getTable(TableName.valueOf("your_table_name"));

        // 创建Delete对象列表
        List<Delete> deleteList = new ArrayList<>();

        // 添加要删除的行键
        deleteList.add(new Delete(Bytes.toBytes("row_key1")));
        deleteList.add(new Delete(Bytes.toBytes("row_key2")));
        // ... 添加更多的行键

        // 批量删除数据
        table.delete(deleteList);

        // 关闭资源
        table.close();
        connection.close();
    }

}

Please note that the example code above is only for bulk deleting data in HBase. You will need to replace “your_table_name” with the actual table name, and add the row keys to be deleted as needed.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds