Shell写出Hive中所有库下的所有表的DDL

在企业中可能会遇到统计Hive库下所有库下所有表的DDL,这里我们使用shell写个脚本进行统计。

#! /bin/bash

current_dir=$(cd $(dirname $0);pwd)
echo "当前路径:${current_dir}"

# 导出Hive库名
hive -e "show databases;" > ${current_dir}/all_database.db
# 删除all_database.db 文件中的各其他日志记录
sed -i '/^[0-9Logging]/d' all_database.db
for database in `cat $current_dir/all_database.db`
do
        # 要采用追加模式,否则遍历的时候会覆盖
        echo "create database if not exists $database;" >> ${current_dir}/create_database_all_DDL.sql
        # 进入数据库中,遍历库下所有的表
        hive -e "use $database;show tables" >$current_dir/$database.tb
        # 删除$database.tb文件中多于的日志,只保留表名
        sed -i '/^[0-9Logging]/d' $current_dir/$database.tb
        # 遍历数据库下所有的表
        for table in `cat $current_dir/$database.tb`
        do
                echo "------------$database.$table---------------" >> ${current_dir}/${database}_all_DDL.sql
                hive -e "show create table $database.$table" >> ${current_dir}/${database}_all_DDL.sql
                # 删除${database}_all_DDL.sql文件中多于的日志
                sed -i '/^[0-9Logging]/d' ${current_dir}/${database}_all_DDL.sql
                # 打印换行符
                echo -e "\n" >> ${current_dir}/${database}_all_DDL.sql
        done
done

sed -i '/^[0-9Logging]/d'
在这里对上面这个语句进行一个解释
-i                是对文本进行编辑
/^[0-9Logging]/   匹配以数字开头或者是以Logging开头的内容
/d                是执行删除操作
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值