geomesa shell command中无法删除数据,改用hbase 批量删除数据

最近发现好geomesa 对应的数据库的hbase 库。数据比较大,geomesa-hbase有如下几种方式删除

1.使用geomesa-hbase shell ’delete-features‘ command 通过时间cql 删除数据(ps:这种靠geomesa层的命令并没有成功)

2.通过hbase shell 方式来删除实体数据.(这个需要理解geomesa层在hbase存储的数据结构),目前成功了

1.数据脚本记录

数据脚本

geomesa层的t_gaia_rprrt_pro_earth表的想清除2020-08-25 00:00:00 到2020-10-25 23:17:06相关hbase表数据执行脚本如下脚本如下

sh deletehbasetable.sh 2020-08-25 00:00:00 2020-10-25 23:17:06 t_gaia_rprrt_pro_earth_recommendPointRelated_attr_rPRId_dtg_v8 d:

sh deletehbasetable.sh 2020-08-25 00:00:00 2020-10-25 23:17:06 t_gaia_rprrt_pro_earth_recommendPointRelated_attr_rpId_dtg_v8 d:

sh deletehbasetable.sh 2020-08-25 00:00:00 2020-10-25 23:17:06 t_gaia_rprrt_pro_earth_recommendPointRelated_attr_rrId_dtg_v8 d:

sh deletehbasetable.sh 2020-08-25 00:00:00 2020-10-25 23:17:06 t_gaia_rprrt_pro_earth_recommendPointRelated_id_v4 d:

geomesa层的t_gaia_rprrt_pro_earth表清除2020-10-25 23:17:06 到2020-11-25 23:17:06

sh deletehbasetable.sh 2020-10-25 23:17:06 2020-11-25 23:17:06 t_gaia_rprrt_pro_earth_recommendPointRelated_attr_rPRId_dtg_v8 d:

sh deletehbasetable.sh 2020-10-25 23:17:06 2020-11-25 23:17:06 t_gaia_rprrt_pro_earth_recommendPointRelated_attr_rpId_dtg_v8 d:

sh deletehbasetable.sh 2020-10-25 23:17:06 2020-11-25 23:17:06 t_gaia_rprrt_pro_earth_recommendPointRelated_attr_rrId_dtg_v8 d:

sh deletehbasetable.sh 2020-10-25 23:17:06 2020-11-25 23:17:06 t_gaia_rprrt_pro_earth_recommendPointRelated_id_v4 d:

 

2.脚本相关讲解

其中关键的脚本 deletehbasetable.sh  主要是通过hbase的rowkey来删除数据,具体实现步骤如下2步骤。

1.通过bash shell的hbase shell 的scan 命令获取要删除的rowkey列表文件。

2.通过执行python脚本的命令获取第一步中的所有rowkey的值组成 hbase shell deleteall的命令 输出到一个文件。

3.最后用sh执行 hbase shell xxx.txt 执行hbase shell脚本。

#的执行 样例 sh deletehbasetable.sh 2020-10-25 15:16:06 2020-10-25 18:17:06 t_gaia_rprrt_dev_earth_recommendPointRelated_attr_rPRId_dtg_v8

echo '--------------程序从这里开始------------'

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

#basepath=$(cd <code>dirname $0</code>; pwd)



echo '---------------正在创建缓存文件夹--------------'

firstTime="_$1_$2"

mkdir $basepath/CacheOfdelete$firstTime

#touch $basepath/CacheOfdelete$firstTime/data$firstTime.txt

touch $basepath/CacheOfdelete$firstTime/record$firstTime.txt

touch $basepath/CacheOfdelete$firstTime/delete$firstTime.sh



#current1="2018-01-17 16:25:18"

#current2="2018-01-17 16:29:50"

current1="$1 $2"

current2="$3 $4"



tablename="$5"



echo 开始时间:$current1

echo 结束时间:$current2

startSec=`date -d "$current1" +%s`

endSec=`date -d "$current2" +%s`



startTimestamp=$((startSec*1000+10#`date "+%N"`/1000000))

endTimestamp=$((endSec*1000+10#`date "+%N"`/1000000))



echo $tablename

echo $startTimestamp

echo $endTimestamp

#echo $startTimestamp > $basepath/CacheOfdelete$firstTime/data$firstTime.txt

##echo $endTimestamp >> $basepath/CacheOfdelete$firstTime/data$firstTime.txt



# #######第一步:通过时间戳找到要删除的数据

# 注:这里只有rowkey和其中一列,因为目的是找到rowkey

echo "scan '$tablename',{ COLUMNS => '$6',TIMERANGE => [$startTimestamp,$endTimestamp]}" | hbase shell > $basepath/CacheOfdelete$firstTime/record$firstTime.txt

# ######第二步:构建删除数据的shell

#echo "#!/bin/bash " >> $basepath/CacheOfdelete$firstTime/aa.sh

#cat $basepath/CacheOfdelete$firstTime/record$firstTime.txt|awk '{print "deleteall '\'$tablename\''", ",", "\""$1"\""}' >> $basepath/CacheOfdelete$firstTime/delete$firstTime.sh

python ./createDeleteHbasedatashelltxt.py $basepath/CacheOfdelete$firstTime/record$firstTime.txt $tablename $basepath/CacheOfdelete$firstTime/delete$firstTime.txt



#echo "EOF " >> $basepath/CacheOfdelete$firstTime/delete$firstTime.sh



# ########第三步:执行删除shell

#sh $basepath/CacheOfdelete$firstTime/delete$firstTime.sh  >> $basepath/CacheOfdelete$firstTime/delete_logs.txt

hbase shell $basepath/CacheOfdelete$firstTime/delete$firstTime.txt

exit()

echo '---------------正在删除缓存文件夹--------------'

rm -rf $basepath/CacheOfdelete$firstTime

echo '--------------程序到这里结束------------'

其中第二步骤中的Python脚本主要用于生成hbase shell脚本的文本。createDeleteHbasedatashelltxt.py如下:

# -*- coding: UTF-8 -*-
import sys,os

recordfile = sys.argv[1]
tablename = sys.argv[2]
hbaseshellfile = sys.argv[3]
print(recordfile)
print(tablename)
with open(hbaseshellfile,"a") as shellfile:
    with open(recordfile) as f:
          for line in f.readlines():
             res = line.split(" column")
             print(res)
             if len(res) > 1 :
             	#hbase  的scan 出来的rowkey只是显示在控制台的形态,实际形态是16进制的ASCII值
                textfix = res[0].replace('\"', '\\\"')
                textfix = textfix.replace('#', '\\x23')
                textfix = textfix.replace('$', '\\x24')
                textfix = textfix.replace('{', '\\x7B')
                textfix = textfix.replace('^', '\\x5E')
                textfix = textfix.replace('@', '\\x40')
                text = 'deleteall '+"\'"+tablename+"\'"+','+'\"'+textfix.strip()+'\"'+'\n'
                print(text)
                shellfile.write(text)    
             else:
             	print('kong')
    shellfile.write("exit()")

 


版权声明:本文为yuqi1110原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。