SYNTAX:
if [ $# -eq 1 ]
then
echo "valid database"
db_name=$1
else
echo "please enter database"
fi
hive -e "use $olddb;show tables;">Old_db_tables.txt
files="$(<Old_db_tables.txt)"
hive -e "create database $newdb;use $newdb; "
for file1 in $files;
do
type=`eval "hive -e 'create table $newdb.$file1 as select * from $olddb.$file1;"
done
if grep -ie "failed"
then
awk {"print $1"}>error.txt
fi
done
hive -e "use $newdb;show tables;">hive_tables.txt;
EXAMPLE:
[adimulamvenkat19851609@cxln4 ~]$ cat db_create.sh
#Script for create new database from existing database and extract all tables from old DB to new DB
hive -e "use mhtprabhu;show tables;">Old_db_tables.txt
files="$(<Old_db_tables.txt)"
hive -e "create database mhtprabhu_new;use mhtprabhu_new; "
for file1 in $files;
do
hive -e "create table mhtprabhu_new.$file1 as select * from mhtprabhu.$file1;"
done
hive -e "use mhtprabhu_new;show tables; ">New_db_tables.txt
Verify :
mhtprabhu
mhtprabhu_new
[adimulamvenkat19851609@cxln4 ~]$ cat Old_db_tables.txt
emp_sample
employee_data
hari_tab
prabhu_tab
student
student4
student5
student_part
[adimulamvenkat19851609@cxln4 ~]$ cat New_db_tables.txt
emp_sample
employee_data
hari_tab
prabhu_tab
student
student4
student_p
student5art