1. 程式人生 > >hbase根據時間戳刪除資料

hbase根據時間戳刪除資料

hbase shell 或者指令碼刪除資料一般都根據rowkey進行操作,因此,此處還是要先根據時間戳範圍找出rowkey,然後進行deleteall 操作,以下簡單分四步進行操作

#第一步:通過時間戳找到要刪除的資料
#第二步:構建刪除資料的shell
#第三步:給delete_all_by_rowkey.sh 加可執行許可權  執行刪除shell
#!/bin/bash -l
echo '--------------程式從這裡開始------------'
#  ${1} ns:table_name
#  ${2} columns
#  ${3} ttl
#  ${4} stop_date
#  ${5} start_time : if ${4} do not input, the start time is defaults to 0;

table_name=${1}
columns=${2}
ttl=${3}
stop_date=${4}
start_date=0
if [ $# -eq 5 ];then
    start_date=${5}
fi
echo "
   table_name : ${table_name}
   columns : ${columns}
   ttl : ${ttl}
   stop_date : ${stop_date}
   start_date : ${start_date}
"

base_path=$(cd `dirname $0`; pwd)

echo '---------------正在建立快取資料夾--------------'

mkdir -p ${base_path}/cache_of_delete/${table_name}/
touch ${base_path}/cache_of_delete/${table_name}/rowkey.txt
touch ${base_path}/cache_of_delete/${table_name}/delete_all_by_rowkey.sh

# #######第一步:通過時間戳找到要刪除的資料
# 注:這裡只有rowkey和其中一列,因為目的是找到rowkey
echo " scan '${table_name}',{COLUMNS=>'${columns}', TIMERANGE=>[${start_date},${stop_date}]}" | hbase shell  | grep 'column' | grep 'timestamp'  |awk '{print $1}' >  ${base_path}/cache_of_delete/${table_name}/rowkey.txt

# ######第二步:構建刪除資料的shell
echo '#!/bin/bash -l ' > ${base_path}/cache_of_delete/${table_name}/delete_all_by_rowkey.sh
echo 'exec hbase shell <<EOF ' >> ${base_path}/cache_of_delete/${table_name}/delete_all_by_rowkey.sh

 
cat ${base_path}/cache_of_delete/${table_name}/rowkey.txt|awk '{print "deleteall '\'${table_name}\''", ",", "'\''"$1"'\''"}'  >> ${base_path}/cache_of_delete/${table_name}/delete_all_by_rowkey.sh


echo "EOF " >> ${base_path}/cache_of_delete/${table_name}/delete_all_by_rowkey.sh


# ########第三步:給delete_all_by_rowkey.sh 加可執行許可權  執行刪除shell
chmod +x ${base_path}/cache_of_delete/${table_name}/delete_all_by_rowkey.sh
#sh ${base_path}/cache_of_delete/${table_name}/delete_all_by_rowkey.sh >> ${base_path}/cache_of_delete/${table_name}/delete.log

#  ##### 第四步: 修改hbase的TTL值
echo '#!/bin/bash -l ' > ${base_path}/cache_of_delete/${table_name}/alter_ttl.sh
echo 'exec hbase shell <<EOF ' >> ${base_path}/cache_of_delete/${table_name}/alter_ttl.sh
echo 'desc '${table_name}' '>> ${base_path}/cache_of_delete/${table_name}/alter_ttl.sh
echo 'disable   '${table_name}' '  >> ${base_path}/cache_of_delete/${table_name}/alter_ttl.sh
echo 'alter '${table_name}', { NAME=>'f',TTL=>'${ttl}'}   '>> ${base_path}/cache_of_delete/${table_name}/alter_ttl.sh
echo 'enable   '${table_name}' '  >> ${base_path}/cache_of_delete/${table_name}/alter_ttl.sh
echo 'desc '${table_name}' '>> ${base_path}/cache_of_delete/${table_name}/alter_ttl.sh
echo "EOF " >> ${base_path}/cache_of_delete/${table_name}/alter_ttl.sh
chmod +x  ${base_path}/cache_of_delete/${table_name}/alter_ttl.sh
#sh ${base_path}/cache_of_delete/${table_name}/alter_ttl.sh

echo '---------------正在刪除快取資料夾--------------'

#rm -rf ${base_path}/cache_of_delete/${table_name}/delete_all_by_rowkey.sh
echo '--------------程式到這裡結束------------'