Cleanup Script – Delete Hive temp Tables

Delete Hive temp tables created on daily basis
Here the table list are added to "/home/hdfs/scripts/script9/table_list.txt"

Entries in above file are as:
 /apps/hive/warehouse/pv_profile_recommended_bkp_hr_*
 /apps/hive/warehouse/pv_profile_recommended_v2_*
 /apps/hive/warehouse/scv_item_id_mis_*
 /apps/hive/warehouse/scv_item_id_mis_hr_*
 /apps/hive/warehouse/spv_item_id_mis_*
 /apps/hive/warehouse/spv_item_id_mis_*
 /apps/hive/warehouse/temp_item_performance_scv_scc_spv_*
 /apps/hive/warehouse/temp_item_performance_scv_scc_spv_final_*
 /apps/hive/warehouse/temp_item_performance_scv_scc_spv_hr_*
 /apps/hive/warehouse/temp_item_performance_scv_scc_spv_hr_final_*
 /apps/hive/warehouse/temp_strcc_hr_*
 /apps/hive/warehouse/temp_strcv_*
 /apps/hive/warehouse/temp_strcv_hr_*
 /apps/hive/warehouse/temp_strlv_*
 /apps/hive/warehouse/temp_strpv_hr_*
$ cat delete_temp_tables.sh


#!/usr/bin/env bash
### Delete temp tables created by datalytics team ###

IFS=$'\n'

for delete_dir in `cat /home/hdfs/scripts/script9/table_list.txt`
do
hadoop fs -rm -r ${delete_dir}
table=`echo "${delete_dir}" | tr -s "/" | cut -d "/" -f5`
hive -e "SHOW TABLES IN default LIKE '${table}';" >> /home/hdfs/scripts/script9/temp_tables_delete_list.txt
done

for delete_table in `cat /home/hdfs/scripts/script9/temp_tables_delete_list.txt`
do
hive -e "DROP TABLE IF EXISTS ${delete_table};"
done

hadoop fs -rm -r /user/hdfs/.Trash/Current/apps/hive/warehouse/*
rm -rf /home/hdfs/scripts/script9/temp_tables_delete_list.txt

previous_4th_date=$(date -d "`date +%Y%m%d` -4 day" +%Y%m%d)

hadoop fs -rm -r /apps/hive/warehouse/temp_strcv_${previous_4th_date}
hadoop fs -rm -r /apps/hive/warehouse/temp_strcc_${previous_4th_date}
hadoop fs -rm -r /apps/hive/warehouse/temp_strpv_${previous_4th_date}

hive -e "DROP TABLE IF EXISTS temp_strcv_${previous_4th_date};"
hive -e "DROP TABLE IF EXISTS temp_strcc_${previous_4th_date};"
hive -e "DROP TABLE IF EXISTS temp_strpv_${previous_4th_date};"
0 Comments

There are no comments yet

Leave a comment

Your email address will not be published. Required fields are marked *