Hadoop 數據遷移用法詳解

數據遷移使用場景

  • 冷熱集羣數據分類存儲,詳見上述描述.
  • 集羣數據整體搬遷.當公司的業務迅速的發展,導致當前的服務器數量資源出現臨時緊張的時候,爲了更高效的利用資源,會將原A機房數據整體遷移到B機房的,原因可能是B機房機器多,而且B機房本身開銷較A機房成本低些等.
  • 數據的準實時同步.數據的準實時同步與上一點的不同在於第二點可以一次性操作解決,而準實時同步需要定期同步,而且要做到週期內數據基本完全一致.數據準實時同步的目的在於數據的雙備份可用,比如某天A集羣突然宣告不允許再使用了,此時可以將線上使用集羣直接切向B的同步集羣,因爲B集羣實時同步A集羣數據,擁有完全一致的真實數據和元數據信息,所以對於業務方使用而言是不會受到任何影響的.

hadoop 集羣間拷貝數據:

需要將數據源集羣的/etc/hosts中的hadoop節點拷貝到目標集羣所有節點的/etc/hosts中,保證新集羣所有節點可以ping同老集羣所有節點;

hadoop distcp hdfs://qcloud-hadoop02:9000/hive/warehouse/hm2.db/helper/dt=2018-10-17 /data

說明:我們這裏是apache hadoop 到cdh數據遷移,這個命令仍然是可以用的。

一般用法

1、遷移之前需要把兩個集羣的所有節點都互通/etc/hosts文件(重要,包括各個數據節點)

2、配置當前集羣主節點到老集羣各個節點的ssh免密登陸(可選)

3、由於老集羣是HDP2.7.1,新集羣是cdh5.8.5,版本不同,不能用hdfs協議直接拷貝,需要用http協議
即不能用: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協議,命令如下:

hadoop distcp hdfs://namenodeip:9000/foo hdfs://namenodeip:9000/foo

5、由於遷移數據運行了mr任務,對集羣資源有一定的消耗

DistCp優勢特性

  • 1 帶寬限流

DistCp是支持帶寬限流的,使用者可以通過命令參數bandwidth來爲程序進行限流,原理類似於HDFS中數據Balance程序的限流.


  • 2 增量數據同步

對於增量數據同步的需求,在DistCp中也得到了很好的實現.通過update,append和diff2個參數能很好的解決.官方的參數使用說明:

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.

第一個參數,解決了新增文件目錄的同步;第二參數,解決已存在文件的增量更新同步;第三個參數解決刪除或重命名文件的同步.

這裏需要額外解釋一下diff的使用需要設置2個不同時間的snapshot進行對比,產生相應的DiffInfo.在獲取快照文件的變化時,只會選擇出DELETE和RENAME這2種類型的變化信息.

相同hadoop版本同步數據

hadoop distcp -skipcrccheck -update -m 20 hdfs://dchadoop002.dx:8020/user/dc/warehouse/test /user/dc/warehouse/test

不同hadoop版本同步數據

hadoop distcp -skipcrccheck -update -m 20 hftp://ns1/user/test /user/dc/test

參數:

-m 表示併發數
-skipcrccheck 跳過hdfs校驗
-update 更新文件

理源路徑的方式與默認值不同,有些細節需要注意。
這裏給出一些 -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

當不使用-update或-overwrite選項時,DistCp默認會在/target下創建/first和/second目錄。因此將在/target之前先創建目錄。

從而:

hadoop distcp hdfs://nn1:8020/source/first hdfs://nn1:8020/source/second hdfs://nn2:8020/target

上述命令將在/target中生成以下內容:

hdfs://nn2:8020/target/first/1
hdfs://nn2:8020/target/first/2
hdfs://nn2:8020/target/second/10
hdfs://nn2:8020/target/second/20

當指定-update或-overwrite時,源目錄的內容將複製到目標,而不是源目錄本身

從而:

distcp -update hdfs://nn1:8020/source/first hdfs://nn1:8020/source/second hdfs://nn2:8020/target

上述命令將在/ target中生成以下內容:

hdfs://nn2:8020/target/1
hdfs://nn2:8020/target/2
hdfs://nn2:8020/target/10
hdfs://nn2:8020/target/20

如果設置了這兩個選項,每個源目錄的內容都會和目標目錄的內容做比較。如果兩個源文件夾都包含一個具有相同名稱的文件(例如“0”),那麼這兩個源文件將在目的地映射到同一個目錄:/target/0。DistCp碰到這類衝突的情況會終止操作並退出。
現在,請考慮以下複製操作:

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

會產生:

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”因爲文件長度和內容匹配而被跳過。
文件“2”被複制,因爲它不存在/target中。因爲目標文件內容與源文件內容不匹配,文件“10”和文件“20”被覆蓋。如果使用-update
選項,文件“1”也被覆蓋。

  • 3 高效的性能

執行的分佈式特性

高效的MR組件

hive數據遷移

1.hive數據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.集羣間數據複製

需要保證原始集羣目錄有讀權限,新的集羣複製保存目錄有寫權限:

兩個集羣都要賦權
hdfs dfs -chmod -R 777 /tmp/export/*
hdfs dfs -chmod -R 777 /tmp/export/*

數據複製

hadoop distcp hdfs://qcloud-test-hadoop01:9000/tmp/export/hm2 /tmp/export

3.數據導入hive

在源hive show create table tbName顯示建表語句,用語句在目標hive建表,然後倒入數據:

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中執行:

select count(*) from hm2.helper;

只導出某一個分區

導出數據
export table hm2.helper partition(dt='2017-12-16') to '/tmp/export/helper_2017-12-16' ;
數據複製
hadoop distcp hdfs://dc1.xx.com:8020/tmp/export/ hdfs://dc2.xx.com:8020/tmp/export
數據導入
import table hm2.helper partition(dt='2017-12-16') from '/tmp/export/helper_2017-12-16'

與load data [local] inpath path path2 剪切數據不同,import命令其實是從目標/tmp/export/hm2/helper複製到/user/hive/warehouse/hm2.db/helper,這時候可以把/tmp/export/hm2/helper目錄刪掉了。

可以使用hive export/import 進行hive數據的批量遷移,本實驗測試了text,orc,parquet,分區表,並測試了不同版本的導入導出。理論上hive導入導出的數據遷移不受版本,數據格式以及表的限制,可以得出結論可以適應hive export/import進行任何hive數據的遷移

參考鏈接:https://blog.csdn.net/u9999/article/details/78830818


hbase數據遷移

HBase數據遷移是很常見的操作,目前業界主要的遷移方式主要分爲以下幾類:
distcp

從上面圖中可看出,目前的方案主要有四類,Hadoop層有一類,HBase層有三類。實際中用了hbase層的Export / Import方法,這裏介紹一下。

Export/Import方式

源(測試)集羣每個節點可以識別目標集羣每個節點

  • 源集羣hbase執行
hbase org.apache.hadoop.hbase.mapreduce.Export 'hm2:test' hdfs://qcloud-hadoop02:9000/tmp/hbase_export/test

注意:這裏路徑需要帶hdfs://nameser/path ,否則就export 到本地了,下同。

  • 目標集羣hbase執行
hbase org.apache.hadoop.hbase.mapreduce.Import 'hm2:test' hdfs://qcloud-hadoop02:9000/tmp/hbase_export/test

或者

目標集羣每個節點可以識別源(測試)集羣每個節點

  • 源集羣hbase執行
hbase org.apache.hadoop.hbase.mapreduce.Export 'hm2:test' hdfs://qcloud-test-hadoop01:9000/tmp/hbase_export/test
  • 目標集羣hbase執行
hbase org.apache.hadoop.hbase.mapreduce.Import 'hm2:test' hdfs://qcloud-test-hadoop01:9000/tmp/hbase_export/test

同步元數據

因爲分區信息發生了改變,元信息沒有同步。

數據導入到指定的文件夾之後,修復分區和表的元信息,(沒有使用rbuy的各種腳本,0.9之後就D了,)

hbase hbck -fixTableOrphans 'hm2:test'
hbase hbck -fixMeta 'hm2:test'
hbase hbck -fixAssignments 'hm2:test'
hbase hbck -repair 'hm2:test'

總結

上文把HBase數據遷移過程中常用的一些方法作了一個大概介紹,總結起來就四點:

  • DistCp: 文件層的數據同步,也是我們常用的
  • CopyTable: 這個涉及對原表數據Scan,然後直接Put到目標表,效率較低
  • Export/Import: 類似CopyTable, Scan出數據放到文件,再把文件傳輸到目標集羣作Import
  • Snapshot: 比較常用 , 應用靈活,採用快照技術,效率比較高

具體應用時,要結合自身表的特性,考慮數據規模、數據讀寫方式、實時數據&離線數據等方面,再選擇使用哪種。

資料

https://www.cnblogs.com/felixzh/p/5920153.html
http://hadoop.apache.org/docs/r1.0.4/cn/quickstart.html

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