shell脚本导出所有hive库名、hive表名及建表语句

#!/usr/bin/bash

# 获取当前脚本所在路径
cur_dir="$(cd "$(dirname "${
       BASH_SOURCE[0]}")" && pwd)"
cd ${cur_dir}

# 导出所有hive数据库名
hive -e "show databases;" > ${cur_dir}/all_database.db
# 删除导出文件中的警告信息
sed -i '/^WARN:/d' ${cur_dir}/all_database.db

# 导出各个hive库下的所有表
for database in `cat ${
       cur_dir}/all_database.db`
do
    hive -e "use ${database};show tables;" > ${cur_dir}/${database}.tb
    sed -i '/^WARN:/d' ${cur_dir}/${database}.tb
    # 导出建表语句
    for table in `cat ${
       cur_dir}/${
       database}.tb`
    do
        hive -e "show create table ${database}.${table};" >> ${cur_dir}/${database}_all_DDL.sql
        sed -i '/^WARN:/d' ${cur_dir}/${database}_all_DDL.sql
        echo ";" >> ${cur_dir}/${database}_all_DDL.sql 
    done
done

你可能感兴趣的:(shell,hive)