當(dāng)前位置:首頁 > IT技術(shù) > 其他 > 正文

Hive跨集群和版本遷移
2022-05-31 17:11:41

公司重新搭建CDH6.3.0,并把舊集群Hive1.1遷移新集群Hive2.1,記錄一下過程。

一. 遷移Hive數(shù)據(jù)和MySQL中的matastore

通過DistCp拷貝Hive數(shù)據(jù)到新集群,并從MySQL中導(dǎo)出Hive的元數(shù)據(jù)上傳到新集群,最后在新集群MySQL中導(dǎo)入Hive元數(shù)據(jù),并更新Hive版本,修改信息。

1. 遷移Hive數(shù)據(jù)和MySQL中的matastore

版本差異大,使用htfp

hadoop distcp -skipcrccheck -update htfp://hadoop-master-001:50070/user/hive/* 
hdfs://cdh-master-001:8020/user/hive

#因為從Hadoop2.6遷移到Hadoop3.0版本,使用hftp方式。
#源集群的格式是 hftp://<dfs.http.address>/<path> ,默認(rèn)設(shè)置dfs.http.address是 <namenode>:50070。
#新的webhdfs協(xié)議代替了hftp后,源地址和目標(biāo)地址都可以使用http協(xié)議webhdfs,可以完全兼容 。

hadoop distcp -skipcrccheck -update webhdfs://hadoop-master-001:50070/user/hive/*
webhdfs://cdh-master-001:50070/user/hive

2. 在源集群上MySQL導(dǎo)出Hive metastore

mysqldump -uroot -p123456 --databases hive > mysql_hive.sql

#--skip-lock-tables,導(dǎo)出時會鎖定所有表,如果不鎖表,一邊導(dǎo)出一邊錄入數(shù)據(jù),會出問題

3. 在新集群使用Hive用戶導(dǎo)入metastore

mysql -uhive -p123456 --default-character-set=utf8  hive < mysql_hive.sql

4. 升級Hive庫

Hive版本相同不用升級。要根據(jù)版本序列升級,不能跨版本。

mysql -uroot -proot risk -hcdh-master < mysqlupgrade-1.1.0-to-1.2.1.mysql.sql
mysql -uroot -proot risk -hcdh-master < mysqlupgrade-1.2.1-to-2.0.0.mysql.sql
mysql -uroot -proot risk -hcdh-master < mysqlupgrade-2.0.0-to-2.1.1.mysql.sql

5. 修改metastore的集群信息

如果新集群名字跟源集群相同可以不用修改,否則需要修改hive庫的DBS和SDS表內(nèi)容。

#查看HDFS上數(shù)據(jù)存放位置
use hive;
select * from DBS;

update DBS set DB_LOCATION_URI = replace(DB_LOCATION_URI,
'hdfs://hadoop-master-001:8020',
'hdfs://cdh-master-001:8020') ;

update SDS set LOCATION = replace(LOCATION ,
'hdfs://hadoop-master-001:8020',
'hdfs://cdh-master-001:8020') ;

二. export / import +?distcp

使用export將Hive表及元數(shù)據(jù)文件導(dǎo)出到HDFS文件系統(tǒng),通過Distcp命令將導(dǎo)出的元數(shù)據(jù)文件遷移到新集群的HDFS文件中,最后在新集群中通過import命令導(dǎo)入表。

1. 導(dǎo)出Hive數(shù)據(jù)到HDFS

導(dǎo)出的數(shù)據(jù)包括_metadata和data文件夾,如果有分區(qū),data為分區(qū)文件夾

#!/bin/bash

##自動導(dǎo)出hive表到HDFS

#輸入數(shù)據(jù)庫
DB=$1

#獲取hive建表語句
tables=$(hive -e "use $DB; show tables;")

# echo "============== 開始生成hive-export.hql =============="
hive -e "use $DB;show tables" | awk '{printf "export table %s to |/tmp/bak/hive-export/%s|; ",$1,$1}'
| sed "s/|/'/g" > /user/bak/hive/hive-export.hql


# echo "============== hive-export.hql生成成功! =============="
# echo "================== 開始導(dǎo)出hive數(shù)據(jù) =================="
hive -database $DB -f "/hadoop/bak/hive/hive-export.hql"

# awk{printf "|%s|",$1},管道符兩個豎杠用于指示格式的起始與結(jié)束,$1替換%s, 換行符結(jié)尾
# sed "s/|/'/g",sed s/被替換內(nèi)容/替換內(nèi)容/,g(GLOBAL)全部替換,無g只替代每行第一個

2. 拷貝導(dǎo)出的Hive數(shù)據(jù)到新集群

注意:導(dǎo)出數(shù)據(jù)之前,需要先確認(rèn)hive的數(shù)據(jù)格式是orc還是parquet格式,因為orc格式hive的高版本不兼容低版本

原集群是CDH5.7、Hadoop2.6、HDFS端口50070,新集群是CDH6.3.0、Hadoop3.0、HDFS端口9870。采用webhdfs協(xié)議傳輸,記得原集群HDFS集群需要添加新集群服務(wù)器的host。

hadoop distcp webhdfs://hadoop-master-001:50070/tmp/hive-export/ 
webhdfs://cdh-master-001:9870/tmp/hive-export/

3. 修改導(dǎo)出腳本為導(dǎo)入腳本

cp hive_export.hql hive_import.sql
sed -i 's/export table/import table/g' hive-import.hql
sed -i 's/ to / from /g' hive-import.hql

4. 上傳導(dǎo)入腳本后在新集群執(zhí)行

hive -database cp_data -f hive-import.sql

三、數(shù)據(jù)遷移(因為新集群和阿里云的對象存儲打通了,所以我的數(shù)據(jù)都放到了oss上)

1.按項目遷移代碼

-----------------------格式轉(zhuǎn)換后臺shell腳本
#!/bin/bash
echo 'start'
for t in `cat flag.txt` #同一行中的內(nèi)容如果有空格,會被拆分成多個
do
echo "$t"
table=$t
echo '' >./$table.sql
echo '
use tools;
set spark.dynamicAllocation.enabled=false;--關(guān)閉executor動態(tài)分配功能,防止單個任務(wù)分配的資源太多
set spark.executor.instances=3;--設(shè)置可用的executor個數(shù)(3個)
set spark.executor.cores=5;--設(shè)置executor擁有的core數(shù)(5C)
set spark.executor.memory=8000000000b;--設(shè)置executor擁有的內(nèi)存數(shù)(8G)
set mapreduce.map.memory.mb=3072;
set mapreduce.reduce.memory.mb=3072;
set hive.exec.dynamic.partition=true;
set hive.exec.dynamic.partition.mode=nonstrict;
set hive.exec.max.dynamic.partitions.pernode=10000;
set hive.exec.max.dynamic.partitions=10000;
set hive.exec.max.created.files=10000;
set hive.input.format=org.apache.hadoop.hive.ql.io.CombineHiveInputFormat;
set mapred.max.split.size=50000000;
set mapred.min.split.size.per.node=50000000;
set mapred.min.split.size.per.rack=50000000;
' >>./$table.sql
echo "insert overwrite table ${table}_tran partition(pt) select * from $table;" >>./$table.sql
done
echo 'end'

--------------------------------把hdfs文件從老集群遷移到新集群--------------------------------

--在新集群worker節(jié)點上執(zhí)行
--刪除新集群上的路徑
#!/bin/bash
echo 'start'
for t in `cat move_flag.txt` #同一行中的內(nèi)容如果有空格,會被拆分成多個
do
echo "$t"
table=$t
hadoop distcp hdfs://hadoop-master-001:50070/group/user/tools/meta/hive-temp-table/${table}_tran/* oss://bigdata/group/user/tools/meta/hive-temp-table/${table}
done
echo 'end'

nohup ./move.sh &> move.log &
nohup ./move2.sh &> move2.log &

2.重新建表及導(dǎo)入數(shù)據(jù)?

--重新創(chuàng)建表
drop table if exists xxx;
create external table if not exists xxx (
`xxx` string comment 'xxx',
`xxx` string comment 'xxx'
`xxx` string comment 'xxx',
`xxx` string comment 'xxx',
`xxx` string comment 'xxx',
`xxx` string comment 'xxx',
`xxx` string comment 'xxx',
`xxx` string comment 'xxx'
)
comment 'xxx'
PARTITIONED BY (pt STRING)
row format delimited
fields terminated by '