Hadoop 數(shù)據(jù)遷移用法詳解
目錄
- 一般用法
- hive數(shù)據(jù)遷移
- Export/Import方式
- 同步元數(shù)據(jù)
- 總結(jié)
數(shù)據(jù)遷移使用場景
- 冷熱集群數(shù)據(jù)分類存儲,詳見上述描述.
- 集群數(shù)據(jù)整體搬遷.當(dāng)公司的業(yè)務(wù)迅速的發(fā)展,導(dǎo)致當(dāng)前的服務(wù)器數(shù)量資源出現(xiàn)臨時緊張的時候,為了更高效的利用資源,會將原A機房數(shù)據(jù)整體遷移到B機房的,原因可能是B機房機器多,而且B機房本身開銷較A機房成本低些等.
- 數(shù)據(jù)的準(zhǔn)實時同步.數(shù)據(jù)的準(zhǔn)實時同步與上一點的不同在于第二點可以一次性操作解決,而準(zhǔn)實時同步需要定期同步,而且要做到周期內(nèi)數(shù)據(jù)基本完全一致.數(shù)據(jù)準(zhǔn)實時同步的目的在于數(shù)據(jù)的雙備份可用,比如某天A集群突然宣告不允許再使用了,此時可以將線上使用集群直接切向B的同步集群,因為B集群實時同步A集群數(shù)據(jù),擁有完全一致的真實數(shù)據(jù)和元數(shù)據(jù)信息,所以對于業(yè)務(wù)方使用而言是不會受到任何影響的.
hadoop 集群間拷貝數(shù)據(jù):
需要將數(shù)據(jù)源集群的/etc/hosts中的hadoop節(jié)點拷貝到目標(biāo)集群所有節(jié)點的/etc/hosts中,保證新集群所有節(jié)點可以ping同老集群所有節(jié)點;
- hadoop distcp hdfs://qcloud-hadoop02:9000/hive/warehouse/hm2.db/helper/dt=2018-10-17 /data
說明:我們這里是apache hadoop 到cdh數(shù)據(jù)遷移,這個命令仍然是可以用的。
一般用法
1、遷移之前需要把兩個集群的所有節(jié)點都互通/etc/hosts文件(重要,包括各個數(shù)據(jù)節(jié)點)
2、配置當(dāng)前集群主節(jié)點到老集群各個節(jié)點的ssh免密登陸(可選)
3、由于老集群是HDP2.7.1,新集群是cdh5.8.5,版本不同,不能用hdfs協(xié)議直接拷貝,需要用http協(xié)議 即不能用:distcp hdfs://src:50070/foo /user而要用:distcp hftp://src:50070/foo /user最終的命令為:
- hadoop distcp hftp://192.168.57.73:50070/hive3/20171008 /hive3/
4、如果兩個集群的版本相同,則可以使用hdfs協(xié)議,命令如下:
- hadoop distcp hdfs://namenodeip:9000/foo hdfs://namenodeip:9000/foo
5、由于遷移數(shù)據(jù)運行了mr任務(wù),對集群資源有一定的消耗
DistCp優(yōu)勢特性
1 帶寬限流
DistCp是支持帶寬限流的,使用者可以通過命令參數(shù)bandwidth來為程序進(jìn)行限流,原理類似于HDFS中數(shù)據(jù)Balance程序的限流.
2 增量數(shù)據(jù)同步
對于增量數(shù)據(jù)同步的需求,在DistCp中也得到了很好的實現(xiàn).通過update,append和diff2個參數(shù)能很好的解決.官方的參數(shù)使用說明:
- Update: Update target, copying only missing files or directories
- Append: Reuse existing data in target files and append new data to them if possible.
- Diff: Use snapshot diff report to identify the difference between source and target.
第一個參數(shù),解決了新增文件目錄的同步;第二參數(shù),解決已存在文件的增量更新同步;第三個參數(shù)解決刪除或重命名文件的同步.
這里需要額外解釋一下diff的使用需要設(shè)置2個不同時間的snapshot進(jìn)行對比,產(chǎn)生相應(yīng)的DiffInfo.在獲取快照文件的變化時,只會選擇出DELETE和RENAME這2種類型的變化信息.
相同hadoop版本同步數(shù)據(jù)
- hadoop distcp -skipcrccheck -update -m 20 hdfs://dchadoop002.dx:8020/user/dc/warehouse/test /user/dc/warehouse/test
不同hadoop版本同步數(shù)據(jù)
- hadoop distcp -skipcrccheck -update -m 20 hftp://ns1/user/test /user/dc/test
參數(shù):
- -m 表示并發(fā)數(shù)
- -skipcrccheck 跳過hdfs校驗
- -update 更新文件
理源路徑的方式與默認(rèn)值不同,有些細(xì)節(jié)需要注意。這里給出一些 -update和 -overwrite的例子。考慮從/source/first/ 和 /source/second/ 到 /target/的拷貝,源路徑包括:
- hdfs://nn1:8020/source/first/1
- hdfs://nn1:8020/source/first/2
- hdfs://nn1:8020/source/second/10
- hdfs://nn1:8020/source/second/20
當(dāng)不使用-update或-overwrite選項時,DistCp默認(rèn)會在/target下創(chuàng)建/first和/second目錄。因此將在/target之前先創(chuàng)建目錄。
從而:
- hadoop distcp hdfs://nn1:8020/source/first hdfs://nn1:8020/source/second hdfs://nn2:8020/target
上述命令將在/target中生成以下內(nèi)容:
- hdfs://nn2:8020/target/first/1
- hdfs://nn2:8020/target/first/2
- hdfs://nn2:8020/target/second/10
- hdfs://nn2:8020/target/second/20
當(dāng)指定-update或-overwrite時,源目錄的內(nèi)容將復(fù)制到目標(biāo),而不是源目錄本身。
從而:
- distcp -update hdfs://nn1:8020/source/first hdfs://nn1:8020/source/second hdfs://nn2:8020/target
上述命令將在/ target中生成以下內(nèi)容:
- hdfs://nn2:8020/target/1
- hdfs://nn2:8020/target/2
- hdfs://nn2:8020/target/10
- hdfs://nn2:8020/target/20
如果設(shè)置了這兩個選項,每個源目錄的內(nèi)容都會和目標(biāo)目錄的內(nèi)容做比較。如果兩個源文件夾都包含一個具有相同名稱的文件(例如“0”),那么這兩個源文件將在目的地映射到同一個目錄:/target/0。DistCp碰到這類沖突的情況會終止操作并退出?,F(xiàn)在,請考慮以下復(fù)制操作:
- distcp hdfs://nn1:8020/source/first hdfs://nn1:8020/source/second hdfs://nn2:8020/target
其中源路徑/大小:
- hdfs://nn1:8020/source/first/1 32
- hdfs://nn1:8020/source/first/2 32
- hdfs://nn1:8020/source/second/10 64
- hdfs://nn1:8020/source/second/20 32
和目的路徑/大小:
- hdfs://nn2:8020/target/1 32
- hdfs://nn2:8020/target/10 32
- hdfs://nn2:8020/target/20 64
會產(chǎn)生:
- hdfs://nn2:8020/target/1 32
- hdfs://nn2:8020/target/2 32
- hdfs://nn2:8020/target/10 64
- hdfs://nn2:8020/target/20 32
文件“1”因為文件長度和內(nèi)容匹配而被跳過。文件“2”被復(fù)制,因為它不存在/target中。因為目標(biāo)文件內(nèi)容與源文件內(nèi)容不匹配,文件“10”和文件“20”被覆蓋。如果使用-update 選項,文件“1”也被覆蓋。
3 高效的性能
執(zhí)行的分布式特性
高效的MR組件
hive數(shù)據(jù)遷移
1.hive數(shù)據(jù)export到hdfs
- export table hm2.helper to '/tmp/export/hm2/helper';
如下:
- hive> export table hm2.helper to '/tmp/export/hm2/helper';
- Copying data from file:/app/data/hive/tmp/scratchdir/ce4c15d9-6875-40ed-add4-deedd75a4a92/hive_2018-10-26_10-58-21_552_8465737459112285307-1/-local-10000/_metadata
- Copying file: file:/app/data/hive/tmp/scratchdir/ce4c15d9-6875-40ed-add4-deedd75a4a92/hive_2018-10-26_10-58-21_552_8465737459112285307-1/-local-10000/_metadata
- Copying data from hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=13/msgtype=helper
- Copying data from hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=14/msgtype=helper
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00001
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00003
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00004
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00005
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00006
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00007
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00008
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00009
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00010
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00011
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00012
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00013
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00014
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00015
- Copying data from hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-13/hour=13/msgtype=helper
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-13/hour=13/msgtype=helper/part-m-00002
- Copying data from hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-13/hour=14/msgtype=helper
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-13/hour=14/msgtype=helper/part-m-00000
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-13/hour=14/msgtype=helper/part-m-00002
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-13/hour=14/msgtype=helper/part-m-00006
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-13/hour=14/msgtype=helper/part-m-00016
- Copying data from hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-22/hour=08/msgtype=helper
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-22/hour=08/msgtype=helper/part-m-00006
- Copying data from hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-22/hour=09/msgtype=helper
- Copying file: hdfs://nameser/hive/warehouse/hm2.db/helper/dt=2018-06-22/hour=09/msgtype=helper/part-m-00000
- OK
- Time taken: 1.52 seconds
2.集群間數(shù)據(jù)復(fù)制
需要保證原始集群目錄有讀權(quán)限,新的集群復(fù)制保存目錄有寫權(quán)限:
- 兩個集群都要賦權(quán)
- hdfs dfs -chmod -R 777 /tmp/export/*
- hdfs dfs -chmod -R 777 /tmp/export/*
數(shù)據(jù)復(fù)制
- hadoop distcp hdfs://qcloud-test-hadoop01:9000/tmp/export/hm2 /tmp/export
3.數(shù)據(jù)導(dǎo)入hive
在源hive show create table tbName顯示建表語句,用語句在目標(biāo)hive建表,然后倒入數(shù)據(jù):
- import table hm2.helper from '/tmp/export/hm2/helper';
成功:
- hive> import table hm2.helper from '/tmp/export/hm2/helper';
- Copying data from hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=13/msgtype=helper
- Copying data from hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=14/msgtype=helper
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00001
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00003
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00004
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00005
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00006
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00007
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00008
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00009
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00010
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00011
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00012
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00013
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00014
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-12/hour=14/msgtype=helper/part-m-00015
- Copying data from hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-13/hour=13/msgtype=helper
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-13/hour=13/msgtype=helper/part-m-00002
- Copying data from hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-13/hour=14/msgtype=helper
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-13/hour=14/msgtype=helper/part-m-00000
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-13/hour=14/msgtype=helper/part-m-00002
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-13/hour=14/msgtype=helper/part-m-00006
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-13/hour=14/msgtype=helper/part-m-00016
- Copying data from hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-22/hour=08/msgtype=helper
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-22/hour=08/msgtype=helper/part-m-00006
- Copying data from hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-22/hour=09/msgtype=helper
- Copying file: hdfs://qcloud-cdh01.2144.com:8020/tmp/export/hm2/helper/dt=2018-06-22/hour=09/msgtype=helper/part-m-00000
- Loading data to table hm2.helper partition (dt=2018-06-12, hour=13, msgtype=helper)
- Loading data to table hm2.helper partition (dt=2018-06-12, hour=14, msgtype=helper)
- Loading data to table hm2.helper partition (dt=2018-06-13, hour=13, msgtype=helper)
- Loading data to table hm2.helper partition (dt=2018-06-13, hour=14, msgtype=helper)
- Loading data to table hm2.helper partition (dt=2018-06-22, hour=08, msgtype=helper)
- Loading data to table hm2.helper partition (dt=2018-06-22, hour=09, msgtype=helper)
- OK
- Time taken: 4.966 seconds
這樣就可以在新的hive中執(zhí)行:
- select count(*) from hm2.helper;
只導(dǎo)出某一個分區(qū)
- 導(dǎo)出數(shù)據(jù)
- export table hm2.helper partition(dt='2017-12-16') to '/tmp/export/helper_2017-12-16' ;
- 數(shù)據(jù)復(fù)制
- hadoop distcp hdfs://dc1.xx.com:8020/tmp/export/ hdfs://dc2.xx.com:8020/tmp/export
- 數(shù)據(jù)導(dǎo)入
- import table hm2.helper partition(dt='2017-12-16') from '/tmp/export/helper_2017-12-16'
與load data [local] inpath path path2 剪切數(shù)據(jù)不同,import命令其實是從目標(biāo)/tmp/export/hm2/helper復(fù)制到/user/hive/warehouse/hm2.db/helper,這時候可以把/tmp/export/hm2/helper目錄刪掉了。
==可以使用hive export/import 進(jìn)行hive數(shù)據(jù)的批量遷移,本實驗測試了text,orc,parquet,分區(qū)表,并測試了不同版本的導(dǎo)入導(dǎo)出。理論上hive導(dǎo)入導(dǎo)出的數(shù)據(jù)遷移不受版本,數(shù)據(jù)格式以及表的限制,可以得出結(jié)論可以適應(yīng)hive export/import進(jìn)行任何hive數(shù)據(jù)的遷移==
參考鏈接:https://blog.csdn.net/u9999/article/details/78830818
hbase數(shù)據(jù)遷移
HBase數(shù)據(jù)遷移是很常見的操作,目前業(yè)界主要的遷移方式主要分為以下幾類:
從上面圖中可看出,目前的方案主要有四類,Hadoop層有一類,HBase層有三類。實際中用了hbase層的Export / Import方法,這里介紹一下。
Export/Import方式
源(測試)集群每個節(jié)點可以識別目標(biāo)集群每個節(jié)點
源集群hbase執(zhí)行
- hbase org.apache.hadoop.hbase.mapreduce.Export 'hm2:test' hdfs://qcloud-hadoop02:9000/tmp/hbase_export/test
注意:這里路徑需要帶hdfs://nameser/path ,否則就export 到本地了,下同。
目標(biāo)集群hbase執(zhí)行
- hbase org.apache.hadoop.hbase.mapreduce.Import 'hm2:test' hdfs://qcloud-hadoop02:9000/tmp/hbase_export/test
或者
目標(biāo)集群每個節(jié)點可以識別源(測試)集群每個節(jié)點
源集群hbase執(zhí)行
- hbase org.apache.hadoop.hbase.mapreduce.Export 'hm2:test' hdfs://qcloud-test-hadoop01:9000/tmp/hbase_export/test
目標(biāo)集群hbase執(zhí)行
- hbase org.apache.hadoop.hbase.mapreduce.Import 'hm2:test' hdfs://qcloud-test-hadoop01:9000/tmp/hbase_export/test
同步元數(shù)據(jù)
因為分區(qū)信息發(fā)生了改變,元信息沒有同步。
數(shù)據(jù)導(dǎo)入到指定的文件夾之后,修復(fù)分區(qū)和表的元信息,(沒有使用rbuy的各種腳本,0.9之后就D了,)
- hbase hbck -fixTableOrphans 'hm2:test'
- hbase hbck -fixMeta 'hm2:test'
- hbase hbck -fixAssignments 'hm2:test'
- hbase hbck -repair 'hm2:test'
總結(jié)
上文把HBase數(shù)據(jù)遷移過程中常用的一些方法作了一個大概介紹,總結(jié)起來就四點:
- DistCp: 文件層的數(shù)據(jù)同步,也是我們常用的
- CopyTable: 這個涉及對原表數(shù)據(jù)Scan,然后直接Put到目標(biāo)表,效率較低
- Export/Import: 類似CopyTable, Scan出數(shù)據(jù)放到文件,再把文件傳輸?shù)侥繕?biāo)集群作Import
- Snapshot: 比較常用 , 應(yīng)用靈活,采用快照技術(shù),效率比較高
具體應(yīng)用時,要結(jié)合自身表的特性,考慮數(shù)據(jù)規(guī)模、數(shù)據(jù)讀寫方式、實時數(shù)據(jù)&離線數(shù)據(jù)等方面,再選擇使用哪種。
資料
https://www.cnblogs.com/felixzh/p/5920153.html http://hadoop.apache.org/docs/r1.0.4/cn/quickstart.html