HBase在CentOS中的数据迁移流程与方法
在CentOS环境下迁移HBase数据,需根据数据量、实时性要求及集群环境选择合适方法。常见方式包括Shell工具导出导入、HBase自带工具(Export/Import、CopyTable、Snapshot)、复制(Replication)及Bulk Load,以下是详细步骤及注意事项:
zk1:2181,zk2:2181,zk3:2181)可互相访问,防火墙开放相应端口。tar -czvf hbase-backup.tar.gz /hbase/data备份源集群数据目录,防止数据丢失。适用于少量数据迁移,步骤简单但效率较低:
backup 'source_table', 'backup_table'创建备份,或使用export 'source_table', '/hdfs/export/path'将数据导出至HDFS。hdfs dfs -get /hdfs/export/path /local/path下载至目标集群本地;若为Shell备份文件,用scp传输。import 'target_table', '/local/path'(或importtsv处理CSV文件)将数据导入目标表。适用于10T以下数据的跨集群迁移,通过MapReduce任务实现高效传输:
hbase export 'source_table' '/hdfs/source/export/path',将表数据导出为SequenceFile格式至HDFS。hadoop distcp将导出的HDFS数据复制至目标集群HDFS:hadoop distcp hdfs://source:8020/source/path hdfs://target:8020/target/path。hbase import 'target_table' '/hdfs/target/import/path',将数据导入目标表。适用于实时或增量同步,通过MapReduce读取源表数据并写入目标表:
hbase org.apache.hadoop.hbase.mapreduce.CopyTable -Dhbase.client.scanner.caching=200 -Dmapreduce.local.map.tasks.maximum=16 --peer.adr=target-zk1,target-zk2,target-zk3:/hbase source_table,将源表数据同步至目标集群的source_table。适用于大规模数据且需最小化停机时间的场景,通过快照实现数据一致性:
hbase snapshot create -n my_snapshot -t source_table,为源表创建快照。hbase org.apache.hadoop.hbase.snapshot.ExportSnapshot -snapshot my_snapshot -copy-from hdfs://source:8020/hbase/.hbase-snapshot/my_snapshot -copy-to hdfs://target:8020/hbase/.hbase-snapshot/,将快照数据复制至目标集群HDFS。hbase shell,输入restore_snapshot 'my_snapshot'恢复快照至目标表。适用于需要实时同步的场景,通过HBase复制功能将源集群数据自动同步至目标集群:
hbase-site.xml,添加hbase.replication=true(开启复制),并配置源集群ZooKeeper信息hbase.replication.source.zookeeper.quorum=source-zk1,source-zk2,source-zk3、hbase.replication.source.zookeeper.property.clientPort=2181。hbase-site.xml,添加hbase.replication=true(开启复制),并配置目标集群ZooKeeper信息hbase.replication.target.zookeeper.quorum=target-zk1,target-zk2,target-zk3、hbase.replication.target.zookeeper.property.clientPort=2181。add_peer 'peer1', 'target-zk1,target-zk2,target-zk3:/hbase',建立与目标集群的复制关系。start_replication 'peer1',开始同步数据;可通过status 'replication'监控同步状态。hbase shell,使用count 'target_table'统计行数,与源表对比;或使用scan 'target_table'抽查数据是否一致。hbase master status)及RegionServer状态(hbase regionserver status),确保服务正常运行。hbase.client.scanner.caching(增加缓存行数)、mapreduce.local.map.tasks.maximum(增加Map任务数)提升速度。