hive数据迁移(公司都是内部表,只针对内部表)
eg:从a机器迁移数据库或者table到b机器,我的方法是a机器跑个脚本,使用hive自带的export命令 导出hive表到hdfs,然后使用hadoop的distcp(distributed copy)命令拷贝到另一个集群的hdfs(需要提前做好免密).然后b机器使用hive自带的import命令导入表(结构+数据).
a机器运行:
内网ip,不泄密
#!/bin/bash
# hive内部表从服务器a (fromIp)拷贝到服务器B(toIp). 此处是A服务器 fromIp
# 两个脚本里面 hdfsTransforPath 路径必须相同,下面会涉及路径删除,请注意格式# fromIp:导出hive表的服务器地址
fromIp="192.168.100.160"
# toIp:导入hive表的服务器地址
toIp="192.168.100.142"# tableListForMigration:需要导出的hive表的列表,请写全库名.表名,中间空格分隔
tableListForMigration=(ods.table1 dwd.table2)# 导出的hive表在hdfs中存储的路径
hdfsTransforPath="/inout"
################################上面变量需要修改####################################hadoop fs -rm -r "${hdfsTransforPath}"
hadoop fs -mkdir "${hdfsTransforPath}"for aTable in ${tableListForMigration[@]}
do
hive -e "export table ${aTable} to '${hdfsTransforPath}/${aTable}' "
donehadoop distcp -skipcrccheck -overwrite hdfs://${fromIp}/${hdfsTransforPath} hdfs://${toIp}/${hdfsTransforPath};
hadoop fs -rm -r "${hdfsTransforPath}"
b机器运行:
#!/bin/bash
# hive内部表从服务器a拷贝到服务器B 此处是B服务器 toIp
# 两个脚本里面 hdfsTransforPath 路径必须相同,下面会涉及路径删除,请注意格式# 导出的hive表在hdfs中存储的路径
hdfsTransforPath="/inout"# tableListForMigration:需要导出的hive表的列表,请写全库名.表名,中间空格分隔
#tableListForMigration=(bps.parameter_info dwd.pid_his)
tableListForMigration=(ods.pid_update)################################上面变量需要修改####################################for aTable in ${tableListForMigration[@]}
do
hive -e "import table ${aTable} from '${hdfsTransforPath}/${aTable}' "
donehadoop fs -rm -r "${hdfsTransforPath}"
--------------
上面发现使用 数据库.数据表名 的数组进行迁移,如果想迁移整个数据库,可以通过下面的脚本获得所有数据库列表中的表名数组
#!/bin/bash
# desc: 输入数据库列表,返回 数据库名.table名 组成的shell数组,用来拷贝该输出给 hive迁移脚本使用# 请把当前目录切换为一个空白目录,因为会生成新文件
# 最终结果在文件 dbName.tbName.txt 中
# 只需要设置一个参数 数据库数组dbNameList 即可dbNameList=(数据库name1 数据库name2)###########################上面是参数###############################
# 该文件使用追加模式,所以使用前先删除或者清空
rm dbName.tbName.txt
i=1
for dbName in ${dbNameList[@]}
dohive -e "use $dbName;show tables;" > $i.txtsleep 3cat $i.txt |while read tbNamedoecho -e "${dbName}.${tbName} \c" >> dbName.tbName.txtdonei=$(($i+1))
done