HBase按照行键范围删除数据

Posted darange

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HBase按照行键范围删除数据相关的知识,希望对你有一定的参考价值。

#!/bin/bash
#TOOL_PATH=$(cd "$(dirname "$0")"; pwd)
#TOOL_PATH_TMP=$(cd "$(dirname "$0")"; pwd)
if [ $# != 3 ];then
        echo ‘usage:sh byRowKeyRange.sh table startrowkey endrowkey‘
        exit
fi
table=$1
srowkey=$2
erowkey=$3

echo $table
echo $srowkey
echo $erowkey

currentTime=`date +%s`

echo "scan,get rowkeys,scan: ‘$table‘,{ STARTROW => ‘$srowkey‘,ENDROW => ‘$erowkey‘,FILTER => "FirstKeyOnlyFilter() AND KeyOnlyFilter()" } "
#notice:[srowkey,erowkey)!!!
echo "scan ‘$table‘,{ STARTROW => ‘$srowkey‘,ENDROW => ‘$erowkey‘,FILTER => "FirstKeyOnlyFilter() AND KeyOnlyFilter()" }" | hbase shell > ./scanresult-$currentTime.txt

#删除scan结果文件前面没用的6行
sed -i ‘1,6d‘ scanresult-$currentTime.txt

#删除最后一个空行
sed -i ‘$d‘ scanresult-$currentTime.txt

#删除scan统计条数的行,现在位置在最后
sed -i ‘$d‘ scanresult-$currentTime.txt

#判断下是否查询结果,没有则直接退出

tmpCount=$(wc -l ./scanresult-$currentTime.txt | awk ‘{print $1}‘)

if [ "$tmpCount" -eq 0 ];then
        echo "0 rows deleted"
        rm -rf ./scanresult-$currentTime.txt
        exit
fi


#生成hbase 删除语句
cat scanresult-$currentTime.txt|awk ‘{print $1}‘ | while read rowkey
do
echo -e "deleteall ‘${table}‘,‘${rowkey}‘" >> ./delete-$currentTime.txt
done

totalCount=$(wc -l ./delete-$currentTime.txt | awk ‘{print $1}‘)

echo "exit" >> ./delete-$currentTime.txt

#执行hbase删除
hbase shell ./delete-$currentTime.txt

echo "$totalCount records deleted"

#rm -rf ./scanresult-$currentTime.txt
#rm -rf ./delete-$currentTime.txt

KeyOnlyFilter:只会返回每行的行键+列簇+列,而不返回值

FirstKeyOnlyFilter:找每一行的第一列数据,找到之后,就会停止扫描,会返回列值

参考:https://blog.csdn.net/nyistzp/article/details/76922512

以上是关于HBase按照行键范围删除数据的主要内容,如果未能解决你的问题,请参考以下文章

Hbase 性能行键与列限定符

HBase从入门到精通11:HBase数据保存过程和Region分裂

HBase

HBase 系列—— HBase 系统架构及数据结构

HBase基本操作

如何根据两列的范围查询 HBase?