如何存档配置单元表?

mm5n2pyu  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(370)

是否有方法检查在90天前创建的配置单元外部表,并将这些表与底层hdfs数据一起删除。这可以在unix脚本中实现吗?

yhuiod9q

yhuiod9q1#

如果配置单元表路径为 /path/your_hive_table_path/ 具体如下:

  1. hadoop --cluster your-hadoop-cluster fs -ls /path/your_hive_table_path/
  2. drwxrwxrwx+ - h_mifi supergroup 0 2019-01-24 10:33 /path/your_hive_table_path//mifidw_car_insurance_expire_month_data
  3. drwxrwxrwx+ - h_mifi supergroup 0 2019-01-24 10:39 /path/your_hive_table_path//mifidw_car_owner
  4. drwxr-xr-x+ - h_mifi supergroup 0 2019-05-30 03:01 /path/your_hive_table_path//push_credit_card_mine_result_new
  5. drwxr-xr-x+ - h_mifi supergroup 0 2019-05-30 03:41 /path/your_hive_table_path//push_live_payment_bill_mine_result_new

我们可以得到表文件的最新更新日期,如下所示:

  1. hadoop --cluster your-hadoop-cluster fs -ls /path/your_hive_table_path/ | awk -F'[ ]+' '{print $6}'
  2. 2019-01-24
  3. 2019-01-24
  4. 2019-05-30
  5. 2019-05-30

我们需要一个 loop 检查每个表是否超过90天并执行 remove 以及 drop 操作。下面是完整的shell脚本,我已经测试过了,效果不错,希望对你有所帮助。

  1. hadoop --cluster your-hadoop-cluster fs -ls /path/your_hive_table_path/ | grep '/path/your_hive_table_path/' | while read line
  2. do
  3. #Get the update date of hive table
  4. date_str=`echo $line | awk -F'[ ]+' '{print $6}'`
  5. #get the path of hive table
  6. table_path=`echo $line | awk -F'[ ]+' '{print $8}'`
  7. #Get the table name of hive table
  8. table_name=`echo $table_path | awk -F'/' '{print $7}' `
  9. today_date_stamp=`date +%s`
  10. table_date_stamp=`date -d $date_str +%s`
  11. stamp_diff=`expr $today_date_stamp - $table_date_stamp`
  12. #Get the diff days from now
  13. days_diff=`expr $stamp_diff / 86400`
  14. #if diff days is greater than 90, rm and drop.
  15. if [ $days_diff -gt 90 ];then
  16. #remove the hdfs file
  17. hadoop --cluster your-hadoop-cluster fs -rm $table_path
  18. #drop the hive table
  19. hive -e"drop table $table_name"
  20. fi
  21. done
展开查看全部

相关问题