前言
HBase數據遷移方案有很多種,但今天我們來通過Snapshot方式來實現HBase的數據遷移(即將A集群HBase的數據遷移到B集群)。
HBase Snapshots允許你對一個表進行快照(即可用副本),它不會對Region Servers產生很大的影響,它進行復制和恢復操作的時候不包括數據拷貝。
導出快照到另外的集群也不會對Region Servers產生影響。
一、使用場景
snapshot是HBase非常核心的一個功能,使用snapshot的不同用法可以實現很多功能,比如:
1.全量/增量備份
任何數據庫都需要有備份的功能來實現數據的高可靠性,snapshot可以非常方便的實現表的在線備份功能,並且對在線業務請求影響非常小。
使用備份數據,用戶可以在異常發生的情況下快速回滾到指定快照點。增量備份會在全量備份的基礎上使用binlog進行周期性的增量備份。
- 使用場景一:通常情況下,對重要的業務數據,建議至少每天執行一次snapshot來保存數據的快照記錄,並且定期清理過期快照,這樣如果業務發生重要錯誤需要回滾的話是可以回滾到之前的一個快照點的。
- 使用場景二:如果要對集群做重大的升級的話,建議升級前對重要的表執行一次snapshot,一旦升級有任何異常可以快速回滾到升級前。
2.數據遷移
可以使用ExportSnapshot功能將快照導出到另一個集群,實現數據的遷移。
- 使用場景一:機房在線遷移,通常情況是數據在A機房,因為A機房機位不夠或者機架不夠需要將整個集群遷移到另一個容量更大的B集群,而且在遷移過程中不能停服。
基本遷移思路是先使用snapshot在B集群恢復出一個全量數據,再使用replication技術增量復制A集群的更新數據,等待兩個集群數據一致之后將客戶端請求重定向到B機房。
- 使用場景二:使用snapshot將表數據導出到HDFS,再使用Hive\Spark等進行離線OLAP分析,比如審計報表、月度報表等。
二、使用方法
開啟快照支持
在0.95+之后的版本都是默認開啟的,在0.94.6+是默認關閉
<property> <name>hbase.snapshot.enabled</name> <value>true</value> </property>
創建快照
- 在原集群上,用snapshot命令創建快照
hbase> snapshot 'ZAT_TRACE',’SNAPSHOT_ZAT_TRACE_20200904093200'
- 查看創建的快照,用list_snapshots命令
hbase> list_snapshots
- 如果快照創建有問題,可以先刪除,用delete_snapshot命令
hbase> delete_snapshot 'SNAPSHOT_ZAT_TRACE_20200904093200'
- 創建完快照后在/hbase根目錄會產生一個目錄
[user_w@emr-header-1 ~]$ hadoop fs -ls hdfs://172.16.120.199:9000/hbase/.hbase-snapshot/ #子目錄下有如下幾個文件 Found 2 items drwxr-x--x - hbase hadoop 0 2018-11-29 10:54 hdfs://172.16.120.199:9000/hbase/.hbase-snapshot/.tmp drwxr-x--x - hbase hadoop 0 2018-11-29 10:54 hdfs://172.16.120.199:9000/hbase/.hbase-snapshot/SNAPSHOT_ZAT_TRACE_20200904093200
數據遷移
在上面創建好快照后,使用ExportSnapshot命令進行數據遷移,ExportSnapshot也是HDFS層的操作,本質還是利用MR進行遷移,
這個過程主要涉及IO操作並消耗網絡帶寬,在遷移時要指定下map數和帶寬,不然容易造成機房其它業務問題,
如果是單獨的MR集群,可以在MR集群上使用如下命令:
hbase org.apache.hadoop.hbase.snapshot.ExportSnapshot \ -snapshot SNAPSHOT_ZAT_TRACE_20200904093200 \
-copy-from hdfs://172.16.120.199:9000/hbase \ -copy-to hdfs://172.16.120.213:9000/hbase \
-mappers 16 \
-bandwidth 1024\
快照恢復數據
- 恢復指定快照,恢復過程會替代原有數據,將表還原到快照點,快照點之后的所有更新將會丟失。它需要先禁用表,再進行恢復
hbase> disable 'ZAT_TRACE' hbase> restore_snapshot 'SNAPSHOT_ZAT_TRACE_20200904093200' hbase> enable 'ZAT_TRACE'
- 根據快照恢復出一個新表,恢復過程不涉及數據移動,可以在秒級完成。
hbase> clone_snapshot 'SNAPSHOT_ZAT_TRACE_20200904093200','ZAT_TRACE_NEW'
- 檢查HBase數據是否遷移成功
hbase> scan 'ZAT_TRACE'
hbase> count 'ZAT_TRACE'
驗證數據
通過HBase自帶的VerifyReplication工具,驗證兩個集群的數據是否一致:
hbase org.apache.hadoop.hbase.mapreduce.replication.VerifyReplication -mappers 10 -bandwidth 1024 1 ZAT_TRACE
數據一致結果:

數據不一致結果:

注:只有GOODROWS時,說明集群間的數據一致;存在BADROWS或ONLY_IN_PEER_TABLE_ROW時,說明集群間的數據不一致。
驗證命令說明:
#命令: hbase org.apache.hadoop.hbase.mapreduce.replication.VerifyReplication [--starttime=timestamp1] [--stoptime=timestamp] [--families=comma separated list of families] peerId tableName #命令參考: bash-4.2$ ./bin/hbase org.apache.hadoop.hbase.mapreduce.replication.VerifyReplication Usage: verifyrep [--starttime=X] [--endtime=Y] [--families=A] [--row-prefixes=B] [--delimiter=] [--recomparesleep=] [--batch=] [--verbose] [--sourceSnapshotName=P] [--sourceSnapshotTmpDir=Q] [--peerSnapshotName=R] [--peerSnapshotTmpDir=S] [--peerFSAddress=T] [--peerHBaseRootAddress=U] Options: starttime beginning of the time range without endtime means from starttime to forever endtime end of the time range versions number of cell versions to verify batch batch count for scan, note that result row counts will no longer be actual number of rows when you use this option raw includes raw scan if given in options families comma-separated list of families to copy row-prefixes comma-separated list of row key prefixes to filter on delimiter the delimiter used in display around rowkey recomparesleep milliseconds to sleep before recompare row, default value is 0 which disables the recompare. verbose logs row keys of good rows sourceSnapshotName Source Snapshot Name sourceSnapshotTmpDir Tmp location to restore source table snapshot peerSnapshotName Peer Snapshot Name peerSnapshotTmpDir Tmp location to restore peer table snapshot peerFSAddress Peer cluster Hadoop FS address peerHBaseRootAddress Peer cluster HBase root location Args: peerid Id of the peer used for verification, must match the one given for replication tablename Name of the table to verify Examples: To verify the data replicated from TestTable for a 1 hour window with peer #5 $ hbase org.apache.hadoop.hbase.mapreduce.replication.VerifyReplication --starttime=1265875194289 --endtime=1265878794289 5 TestTable
驗證結果參考:

引用:
