hbase数据同步工具—HashTable/SyncTable

简介: 本文介绍hbase数据同步工具—HashTable/SyncTable,实现集群内部或跨集群之间的数据同步操作

HashTable/SyncTable是一个同步hbase表数据的工具,其通过过程分为两步,这两步都是mapreduce job。和CopyTable工具一样,他也可以用来在同一个或者不同的集群之间同步部分或者全部的表数据。只不过,相比CopyTable来说,本工具在同步不同集群之间的表数据时表现更好。它不是复制某个区间范围的表数据,而是首先在源集群执行HashTable基于源数据表生成哈希序列,然后在目标集群执行SyncTable基于源数据表、源数据表生成的哈希序列、目标表、目标表生成的哈希序列,对两个表生成的哈希序列进行对比,从而找出缺失的数据。那么在同步的时候就只需要同步缺失的数据就可以了,这可以极大减少带宽和数据传输。

step1 HashTable

首先在源集群执行HashTable:

HbaseTable使用方法:

Usage: HashTable [options] <tablename> <outputpath>

Options:
 batchsize     the target amount of bytes to hash in each batch
               rows are added to the batch until this size is reached
               (defaults to 8000 bytes)
 numhashfiles  the number of hash files to create
               if set to fewer than number of regions then
               the job will create this number of reducers
               (defaults to 1/100 of regions -- at least 1)
 startrow      the start row
 stoprow       the stop row
 starttime     beginning of the time range (unixtime in millis)
               without endtime means from starttime to forever
 endtime       end of the time range.  Ignored if no starttime specified.
 scanbatch     scanner batch size to support intra row scans
 versions      number of cell versions to include
 families      comma-separated list of families to include

Args:
 tablename     Name of the table to hash
 outputpath    Filesystem path to put the output data

Examples:
 To hash 'TestTable' in 32kB batches for a 1 hour window into 50 files:
 $ bin/hbase org.apache.hadoop.hbase.mapreduce.HashTable --batchsize=32000 --numhashfiles=50 --starttime=1265875194289 --endtime=1265878794289 --families=cf2,cf3 TestTable /hashes/testTable

batchsize属性定义基于单个给定的region在单个哈希值中有多少cell data数据。这个属性的设置直接影响到同步的效率。因为这可能会导致SyncTable映射器任务执行的扫描次数减少(这是进程的下一步)。经验法则是,不同步的单元格数量越少(找到差异的概率越低),可以确定更大的批大小值。也就是说,如果未同步的数据少了,那么这个值就可以设置大一些。反之亦然。

step2 SyncTable

HashTable在源集群执行完之后,就可以在目标集群执行SyncTable了。就像replication和其他同步任务一样,需要目标集群能够访问所有源集群的regionServers/DataNodes节点。

SyncTable使用方法:

Usage: SyncTable [options] <sourcehashdir> <sourcetable> <targettable>

Options:
 sourcezkcluster  ZK cluster key of the source table
                  (defaults to cluster in classpath's config)
 targetzkcluster  ZK cluster key of the target table
                  (defaults to cluster in classpath's config)
 dryrun           if true, output counters but no writes
                  (defaults to false)

Args:
 sourcehashdir    path to HashTable output dir for source table
                  if not specified, then all data will be scanned
 sourcetable      Name of the source table to sync from
 targettable      Name of the target table to sync to

Examples:
 For a dry run SyncTable of tableA from a remote source cluster
 to a local target cluster:
 $ bin/hbase org.apache.hadoop.hbase.mapreduce.SyncTable --dryrun=true --sourcezkcluster=zk1.example.com,zk2.example.com,zk3.example.com:2181:/hbase hdfs://nn:9000/hashes/tableA tableA tableA

dryrun选项在只读操作以及表对比中时非常有用的,它可以显示两个表的差异数量而不对表做任何改变,它可以作为VerifyReplication工具的替代品

默认情况下,SyncTable会让目标表成为源表的复制品。

将doDeletes设置为false将修改默认行为,以不删除源表上没有而目标表有的数据。相同的,将doPuts设置为false也将修改默认的行为,以不增加源表有而目标表没有的数据。而如果同时将doDeletes和doPuts同时设置为false,那么其效果就和设置dryrun为true一样的。

在Two-Way Replication或者其他场景下,比如源端和目标端集群数据都有其他数据输入的情况下,建议将doDeletes选项设置为false。

实例

这边以同集群不同表为例,不同集群类似:

源表:

表名:Student
表数据

hbase(main):010:0> scan 'Student'
ROW                                       COLUMN+CELL
 0001                                     column=Grades:BigData, timestamp=1604988333715, value=80
 0001                                     column=Grades:Computer, timestamp=1604988336890, value=90
 0001                                     column=Grades:Math, timestamp=1604988339775, value=85
 0001                                     column=StuInfo:Age, timestamp=1604988324791, value=18
 0001                                     column=StuInfo:Name, timestamp=1604988321380, value=Tom Green
 0001                                     column=StuInfo:Sex, timestamp=1604988328806, value=Male
1 row(s) in 0.0120 seconds

表结构:

hbase(main):011:0> describe 'Student'
Table Student is ENABLED
Student

COLUMN FAMILIES DESCRIPTION

{NAME => 'Grades', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}

{NAME => 'StuInfo', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}

2 row(s) in 0.0400 seconds

执行HashTable

hbase org.apache.hadoop.hbase.mapreduce.HashTable Student /tmp/hash/Student

执行完之后可以看到 /tmp/hash/Student目录下:

drwxr-xr-x+  - hadoop supergroup          0 2020-12-17 16:09 /tmp/hash/Student/hashes
-rw-r--r--+  2 hadoop supergroup          0 2020-12-17 16:09 /tmp/hash/Student/hashes/_SUCCESS
drwxr-xr-x+  - hadoop supergroup          0 2020-12-17 16:09 /tmp/hash/Student/hashes/part-r-00000
-rw-r--r--+  2 hadoop supergroup        158 2020-12-17 16:09 /tmp/hash/Student/hashes/part-r-00000/data
-rw-r--r--+  2 hadoop supergroup        220 2020-12-17 16:09 /tmp/hash/Student/hashes/part-r-00000/index
-rw-r--r--+  2 hadoop supergroup         80 2020-12-17 16:08 /tmp/hash/Student/manifest
-rw-r--r--+  2 hadoop supergroup        153 2020-12-17 16:08 /tmp/hash/Student/partitions

创建新表,命名为Student_2

create 'Student_2','StuInfo','Grades'

现往表Student_2中插入Student表中的部分数据:

put 'Student_2', '0001', 'StuInfo:Name', 'Tom Green', 1
put 'Student_2', '0001', 'StuInfo:Age', '18'
put 'Student_2', '0001', 'StuInfo:Sex', 'Male'

那么现在Student和Student_2两个表的数据是这样子的:

hbase(main):015:0> scan 'Student_2'
ROW                                       COLUMN+CELL
 0001                                     column=StuInfo:Age, timestamp=1608192992466, value=18
 0001                                     column=StuInfo:Name, timestamp=1, value=Tom Green
 0001                                     column=StuInfo:Sex, timestamp=1608192995476, value=Male
1 row(s) in 0.0180 seconds

hbase(main):016:0> scan 'Student'
ROW                                       COLUMN+CELL
 0001                                     column=Grades:BigData, timestamp=1604988333715, value=80
 0001                                     column=Grades:Computer, timestamp=1604988336890, value=90
 0001                                     column=Grades:Math, timestamp=1604988339775, value=85
 0001                                     column=StuInfo:Age, timestamp=1604988324791, value=18
 0001                                     column=StuInfo:Name, timestamp=1604988321380, value=Tom Green
 0001                                     column=StuInfo:Sex, timestamp=1604988328806, value=Male
1 row(s) in 0.0070 seconds

现在通过SyncTable将Student同步到Student_2中:

执行

hbase org.apache.hadoop.hbase.mapreduce.SyncTable --dryrun=false --sourcezkcluster=hadoop:2181:/hbase hdfs://hadoop:8020/tmp/hash/Student Student Student_2

执行完成任务之后可以看到两个表同步了:

hbase(main):001:0> scan 'Student_2'
ROW                                       COLUMN+CELL
 0001                                     column=Grades:BigData, timestamp=1604988333715, value=80
 0001                                     column=Grades:Computer, timestamp=1604988336890, value=90
 0001                                     column=Grades:Math, timestamp=1604988339775, value=85
 0001                                     column=StuInfo:Age, timestamp=1604988324791, value=18
 0001                                     column=StuInfo:Name, timestamp=1604988321380, value=Tom Green
 0001                                     column=StuInfo:Sex, timestamp=1604988328806, value=Male
1 row(s) in 0.2620 seconds

hbase(main):002:0> scan 'Student'
ROW                                       COLUMN+CELL
 0001                                     column=Grades:BigData, timestamp=1604988333715, value=80
 0001                                     column=Grades:Computer, timestamp=1604988336890, value=90
 0001                                     column=Grades:Math, timestamp=1604988339775, value=85
 0001                                     column=StuInfo:Age, timestamp=1604988324791, value=18
 0001                                     column=StuInfo:Name, timestamp=1604988321380, value=Tom Green
 0001                                     column=StuInfo:Sex, timestamp=1604988328806, value=Male
1 row(s) in 0.0210 seconds
相关实践学习
云数据库HBase版使用教程
&nbsp; 相关的阿里云产品:云数据库 HBase 版 面向大数据领域的一站式NoSQL服务,100%兼容开源HBase并深度扩展,支持海量数据下的实时存储、高并发吞吐、轻SQL分析、全文检索、时序时空查询等能力,是风控、推荐、广告、物联网、车联网、Feeds流、数据大屏等场景首选数据库,是为淘宝、支付宝、菜鸟等众多阿里核心业务提供关键支撑的数据库。 了解产品详情:&nbsp;https://cn.aliyun.com/product/hbase &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
4月前
|
SQL 分布式计算 Oracle
数据同步工具DataX的安装
数据同步工具DataX的安装
428 0
|
4月前
|
消息中间件 SQL 分布式计算
一篇文章搞定数据同步工具SeaTunnel
一篇文章搞定数据同步工具SeaTunnel
534 0
|
4月前
|
存储 关系型数据库 MySQL
DataX: 阿里开源的又一款高效数据同步工具
DataX 是由阿里巴巴集团开源的一款大数据同步工具,旨在解决不同数据存储之间的数据迁移、同步和实时交换的问题。它支持多种数据源和数据存储系统,包括关系型数据库、NoSQL 数据库、Hadoop 等。 DataX 提供了丰富的数据读写插件,可以轻松地将数据从一个数据源抽取出来,并将其加载到另一个数据存储中。它还提供了灵活的配置选项和高度可扩展的架构,以适应各种复杂的数据同步需求。
|
存储 文件存储 对象存储
S3存储服务间数据同步工具Rclone迁移教程
目前大多项目我们都会使用各种存储服务,例如oss、cos、minio等。当然,因各种原因,可能需要在不同存储服务间进行数据迁移工作,所以今天就给大家介绍一个比较通用的数据迁移工具Rclone。
S3存储服务间数据同步工具Rclone迁移教程
|
4月前
|
大数据 数据管理 分布式数据库
探索 HBase GUI 工具,助您轻松驾驭大数据世界!
从此告别繁琐,迎接大数据时代的新利器! #HBase #GUI #数据管理 #工具分享
95 2
探索 HBase GUI 工具,助您轻松驾驭大数据世界!
|
7月前
|
SQL 分布式计算 关系型数据库
HBase ImportTSV工具使用
HBase ImportTSV工具使用
77 0
|
7月前
|
canal SQL 关系型数据库
大数据同步工具Canal 2
大数据同步工具Canal
195 0
|
7月前
|
canal 消息中间件 关系型数据库
大数据同步工具Canal 1
大数据同步工具Canal
368 0
|
9月前
|
算法 Linux
Linux系统【文件传输】rsync命令 – 远程数据同步工具
rsync命令来自于英文词组“remote sync”的缩写,其功能是用于远程数据同步。rsync命令能够基于网络(含局域网和互联网)快速的实现多台主机间的文件同步工作,并与scp或ftp发送完整文件不同,rsync有独立的文件内容差异算法,会在传送前对两个文件进行比较,只传送两者内容间的差异部分,因此速度更快。
149 2
|
12月前
|
存储 SQL JSON
阿里又开源一款数据同步工具 DataX,稳定又高效,好用到爆!下
阿里又开源一款数据同步工具 DataX,稳定又高效,好用到爆!下

热门文章

最新文章