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

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