#!/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