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

posted @ 2018-08-20 10:55  远去的列车  阅读(2722)  评论(0编辑  收藏  举报