快速导出hive表结构脚本:

来源:互联网 发布:硕鼠有mac版吗 编辑:程序博客网 时间:2024/05/18 01:30

传入hive database参数:

#!/bin/bashDATABASE=$1hive -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/bashdatabases='default final first init result'for DATABASE in ${databases}dohive -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    donedone

刚发现,上面的脚本导出的每个库里的hive表名和表结构都是重复,这是因为两个hive -e之间没有关联性,hive -e 只适合短语句。

使用 hive -f 可实现快速导出hive表结构。

hive.sh

DATABASES='dcl ddl default'for databases in ${DATABASES}dohive -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    donedone

list_tables.sql

use ${hiveconf:database};show tables;

show_create.sql

use ${hiveconf:database};show create table ${hiveconf:table};