hive批量导出表结构

hive按user导出全部库的表结果信息,并可以作为表信息迁移使用

#!/bin/bash


# 读取表create table
# $1 = dim_sycm_offical
# $2 = $eachline
# $3 = sql file name

funCreateTable(){
 echo "-- --------------$2" >> create_table_first.txt
  echo "use  $1;" >> create_table_first.txt
 	echo "drop table if exists  $2;" >> create_table_first.txt
 	
   hive -e "use $1; show create table $2;" >> create_table_first.txt
   sed -e '/WARN/d'  create_table_first.txt > create_table.txt
   
		echo ";" >> create_table.txt
		echo "msck repair table  $2;" >> create_table.txt
		
   cat create_table.txt >> $3
   
   rm create_table.txt
   rm create_table_first.txt
}


# 读取 db下的所有表
# $1=dim_sycm_offical

funReadTable(){
hive -e "use $1; show tables" >> hivetablesfirst.txt
sed -e '/WARN/d'  hivetablesfirst.txt > hivetables.txt
rm hivetablesfirst.txt
}


# 读取db下的所有 table strunct
# $1=dim_sycm_offical

funReadDBCreateTable(){
  file="$1.sql"
  
	funReadTable $1
	
	if [ -f "hivetables.txt" ]; then
	
	cat hivetables.txt | while read eachline
	
	do
	  funCreateTable $1 $eachline $file
	done
	
	fi
	  
	rm hivetables.txt
}


# 去读指定用户,指定ip,指定端口下的hive db
# $1 = 端口
# $2 = user

funUserDb(){
	if [ "whereis beeline" ]; then
		beeline -u "jdbc:hive2://localhost:$1 $2 $2" --hiveconf mapreduce.job.queuename=datacenter  -e "show databases;"  >> a.txt
	fi

	sed -e '/database_name/d'  a.txt > a1.txt
	sed -e '/default/d'  a1.txt > a2.txt
	sed -e '/-/d'  a2.txt > db.txt

	sed -i "s/|//g" db.txt
	sed -i "s/ //g" db.txt 

	rm a.txt
	rm a1.txt
	rm a2.txt

}


# 主程序入口
# $1 = 本地hive端口号

funMain(){
	user=`whoami`

	if [ "$user" = "hive" ]; then
		echo "$user can not"
	elif [ "$user" = "root" ]; then
		echo "$user can not"
	else 
		# 读取db name
		funUserDb 10000 $user
		
		# 循环读取每个db的table struct
		cat db.txt | while read eachline
		do
			funReadDBCreateTable $eachline
		done

		rm db.txt
	
	fi

}


funMain 10000

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值