传入hive database参数:
#!/bin/bash
DATABASE=$1
hive -e "use ${DATABASE};"
hive -e "show tables;" > ${DATABASE}tables.txt
cat tables.txt | while read eachline
do
hive -e "show create table ${eachline};" >> ${DATABASE}-tables.txt
echo >> ${DATABASE}-tables.txt
done
循环遍历所有database:
#!/bin/bash
databases='default final first init result'
for DATABASE in ${databases}
do
hive -e "use ${DATABASE};"
hive -e "show tables;" > ${DATABASE}tables.txt
cat ${DATABASE}tables.txt | while read eachline
do
hive -e "show create table ${eachline};" >> ${DATABASE}-tables.txt
echo >> ${DATABASE}-tables.txt
done
done
刚发现,上面的脚本导出的每个库里的hive表名和表结构都是重复,这是因为两个hive -e之间没有关联性,hive -e 只适合短语句。
使用 hive -f 可实现快速导出hive表结构。
hive.sh
DATABASES='dcl ddl default'
for databases in ${DATABASES}
do
hive -hiveconf database=${databases} -S -f list_tables.sql > ${databases}_tables_name.txt
cat ${databases}_tables_name.txt | while read eachline
do
hive -hiveconf database=${databases} -hiveconf table=${eachline} -S -f show_create.sql >> ${databases}_tables_structure.txt
echo >> ${databases}_tables_structure.txt
done
done
list_tables.sql
use ${hiveconf:database};
show tables;
show_create.sql
use ${hiveconf:database};
show create table ${hiveconf:table};