[转]快速导出hive表结构脚本

转载至:
https://blog.csdn.net/qq_28178971/article/details/73896666
转载请注明源出处。

传入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};
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值