HBase按照TimeStamp删除数据

#!/bin/bash

#两种时间输入,一种是输入起始日期,另一种是直接输入hbase里面数据的起始时间戳
if [ $# != 5 ];then
        echo 'usage:sh byTimestampRange.sh table "d:pri_key" d "2018-01-22 17:11:52" "2018-01-22 17:14:53"'
        echo 'usage:sh byTimestampRange.sh table "d:pri_key" t "1532599799357" "1532599799357"'
        exit
fi
table=$1
column=$2
ttype=$3
stime=$4
etime=$5

#get startTimestamp,endTimestamp, hbase里面单元格の时间戳为毫秒
# 命令data -d "@去除后3位的时间戳" 可以查看hbase里面单元格的时间对应的日期形式

#输入为起始日期,先转化为字符串形式的时间戳,到秒,然后开始时间戳补上000,结束时间戳补上999,因为hbase里面时间戳是到毫秒级的
if [ $ttype == "d" ];then
        startSec=`date -d "$stime" +%s`
        endSec=`date -d "$etime" +%s`
        sTimestamp=$startSec"000"
        eTimestamp=$endSec"999"
        #sTimestamp=$((startSec*1000+`date "+%N"`/1000000))
        #eTimestamp=$((endSec*1000+`date "+%N"`/1000000))
#直接使用输入的时间戳
elif [ $ttype == "t" ];then
        sTimestamp=$stime
        eTimestamp=$etime
else
        echo "timetype:d or t"
        exit
fi

#echo $table
#echo $column
#echo $ttype
#echo $sTimestamp
#echo $eTimestamp

currentTime=`date +%s`

echo "scan,get rowkeys,scan: '$table',{ COLUMNS => '$column',TIMERANGE => [$sTimestamp,$eTimestamp]}"
#notice:[sTimestamp,eTimestamp)!!!
echo "scan '$table',{ COLUMNS => '$column',TIMERANGE => [$sTimestamp,$eTimestamp]}" | 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

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

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