Shell-备份表结构,表数据,存储过程

表结构备份

10.138.87.7所有库的表结构备份,导出到文件复制表结构的语句

脚本1:

#!/bin/bash
hive -e "show tables;" > tables.txt

sleep(2)

     cat tables.txt |while read eachline
     do
     hive -e "show create table $eachline" >>tablesDDL.txt
     done

 

脚本2  针对hive的:

#!/bin/bash

# bak the hive tables 

hive -e "show databases "> database.txt

rm -rf bak_table.hql bak_table_create.txt

while read database

    do

    echo "the database is :$database "

    echo "xxxxxxxxxxxxxxxxxxxxxxxxx database: $database xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">> bak_table_create.txt

    hive -e "show tables in $database" > tables.txt

    echo "! echo xxxxxxxxxxxxxxxxxxxx database: ${database} xxxxxxxxxxxxxxxxxxxxxxxxxxxx;" >> bak_table.hql

    while read table

        do

        echo "! echo ..................... table: ${database}.${table} ......................;" >> bak_table.hql

        echo "show create table ${database}.${table};" >>bak_table.hql

        echo ! echo "\;" ; >>bak_table.hql

        done < tables.txt

#    hive -f "bak_table.hql" >> bak_table_create.txt

#    rm -rf bak_table.hql

    done < database.txt

    hive -f "bak_table.hql" >> bak_table_create.txt

注:hive -e 执行查询

hive -f  调用文件传递参数

shell中的

创建

>> 追加

show create table t_name;     查看表结构

脚本3:针对impala的(用的)

#!/bin/bash

impala-shell -B -q "show databases "> database.txt

rm -rf bak_table.hql bak_table_create.txt

while read database

    do

    echo "the database is :$database "

    echo "xxxxxxxxxxxxxx database: $database xxxxxxxxxxx">> bak_table_create.txt

    impala-shell -B -q "show tables in $database" > tables.txt

    echo "! echo xxxxx database: ${database} xxxxxx;" >> bak_table.hql

    while read table

        do

        echo "! echo ..... table: ${database}.${table} .....;" >> bak_table.hql

        impala-shell -B -q "show create table ${database}.${table};" >>bak_table.hql

        echo ! echo "\;" ; >>bak_table.hql

        done < tables.txt

#    hive -f "bak_table.hql" >> bak_table_create.txt

#    rm -rf bak_table.hql

    done < database.txt

hive -f "bak_table.hql" >> bak_table_create.txt

chmod 777 copy.sh  赋权限

注:impala-shell -B -q 'select * from t_name;' -o a.txt

impala-shell -f 执行文件

-q              不进入命令行执行sql

 

备份需要的存储过程(穷举了需要的存过)

#!/bin/bash
proc_names=(proc_name1 proc_name2 proc_name3 proc_name4 proc_name5)
#proc_names[@]这样才能获取数组中所有  proc_names只能获取第一个
for proc_name in ${proc_names[@]}
do
  beeline -u "jdbc:hive://ip:port" -e "desc plsql function extended ${proc_name}" > /home/target/beifen/${proc_name}.sql
done

 

将hadoop上某个文件down至本地已有目录下(备份表数据)
  hdfs dfs -get [文件目录] [本地目录]                      

所有就hdfs dfs –get /user/t/ok.txt  /home/t/*

 

将数据放入,再将外表创建上去的时候

     ERROR: ImpalaRuntimeException: Error making 'createTable' RPC to Hive Metastore:

   原因是权限不足:  hadoop fs -chmod -R 777 /hadoop/data/hive

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章