Hbase如何批量删除指定数据

有时我们需要批量删除一些hbase中符合某些条件的数据,本文提供一种简单的shell命令的方式批量删除hbase里的数据。思路就是,建立hive与hbase的关联表,通过hive sql查询出符合条件的数据rowkey导出到本地文件,然后通过hbase shell批量删除。示例如下:

1 创建hbase 表

#创建表名为“test:user_blacklist”,列簇名为“user”的hbase表
#hbase 表
create 'test:user_blacklist', 'user'
# phoneix映射表
create view "user_blacklist" ("rowkey" varchar primary key,"user"."dt" varchar,"user"."total_slave_count" varchar,"user"."api_slave_rate" varchar,"user"."active_7day_slave_count" varchar,"user"."active_7day_slave_weixin_rate" varchar,"device"."did" varchar,"device"."device_user_count" varchar,"device"."same_gyroscope_count" varchar);

2 创建hive映射表

CREATE EXTERNAL TABLE test.user_blacklist (
key string, 
dt string,
total_slave_count string,
api_slave_rate string,
active_7day_slave_count string,
active_7day_slave_weixin_rate string,
device_user_count string,
same_gyroscope_count string
) 
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' 
WITH SERDEPROPERTIES
("hbase.columns.mapping" = 
":key,user:dt,user:total_slave_count,user:api_slave_rate,user:active_7day_slave_count,user:active_7day_slave_weixin_rate,device:device_user_count,device:same_gyroscope_count")
TBLPROPERTIES("hbase.table.name" = "test:user_blacklist");

3 通过hive sql导出指定rowkey删除语句

hive -e "select concat('deleteall \'test:user_blacklist\',\'',key,'\'') from test.user_blacklist where dt>=20190708 and same_gyroscope_count is not null" > del_temp.txt

4 hbase shell批量删除数据

hbase shell del_temp.txt > del.log