HBase备份之导入导出

来源:互联网 发布:淘宝上传宝贝怎么插图 编辑:程序博客网 时间:2024/04/28 23:15

我们在上一篇文章《HBase复制》中讲述了如何建立主/从集群,实现数据的实时备份。但是,HBase复制只对设置好复制以后的数据生效,也即,配置好复制之后插入HBase主集群的数据才能同步复制到HBase从集群中,而对之前的历史数据,采用HBase复制这种办法是无能为力的。本文介绍如何使用HBase的导入导出功能来实现历史数据的备份。

1)将HBase表数据导出到hdfs的一个指定目录中,具体命令如下:

[plain] view plain copy
 print?
  1. $ cd $HBASE_HOME/  
  2. $ bin/hbase org.apache.hadoop.hbase.mapreduce.Export test_table /data/test_table  

其中,$HBASE_HOME为HBase主目录,test_table为要导出的表名,/data/test_table为hdfs中的目录地址。

执行结果太长,这里截取最后一部分,如下所示:

[plain] view plain copy
 print?
  1. 2014-08-11 16:49:44,484 INFO  [main] mapreduce.Job: Running job: job_1407491918245_0021  
  2. 2014-08-11 16:49:51,658 INFO  [main] mapreduce.Job: Job job_1407491918245_0021 running in uber mode : false  
  3. 2014-08-11 16:49:51,659 INFO  [main] mapreduce.Job:  map 0% reduce 0%  
  4. 2014-08-11 16:49:57,706 INFO  [main] mapreduce.Job:  map 100% reduce 0%  
  5. 2014-08-11 16:49:57,715 INFO  [main] mapreduce.Job: Job job_1407491918245_0021 completed successfully  
  6. 2014-08-11 16:49:57,789 INFO  [main] mapreduce.Job: Counters: 37  
  7.     File System Counters  
  8.         FILE: Number of bytes read=0  
  9.         FILE: Number of bytes written=118223  
  10.         FILE: Number of read operations=0  
  11.         FILE: Number of large read operations=0  
  12.         FILE: Number of write operations=0  
  13.         HDFS: Number of bytes read=84  
  14.         HDFS: Number of bytes written=243  
  15.         HDFS: Number of read operations=4  
  16.         HDFS: Number of large read operations=0  
  17.         HDFS: Number of write operations=2  
  18.     Job Counters   
  19.         Launched map tasks=1  
  20.         Rack-local map tasks=1  
  21.         Total time spent by all maps in occupied slots (ms)=9152  
  22.         Total time spent by all reduces in occupied slots (ms)=0  
  23.     Map-Reduce Framework  
  24.         Map input records=3  
  25.         Map output records=3  
  26.         Input split bytes=84  
  27.         Spilled Records=0  
  28.         Failed Shuffles=0  
  29.         Merged Map outputs=0  
  30.         GC time elapsed (ms)=201  
  31.         CPU time spent (ms)=5210  
  32.         Physical memory (bytes) snapshot=377470976  
  33.         Virtual memory (bytes) snapshot=1863364608  
  34.         Total committed heap usage (bytes)=1029177344  
  35.     HBase Counters  
  36.         BYTES_IN_REMOTE_RESULTS=87  
  37.         BYTES_IN_RESULTS=87  
  38.         MILLIS_BETWEEN_NEXTS=444  
  39.         NOT_SERVING_REGION_EXCEPTION=0  
  40.         NUM_SCANNER_RESTARTS=0  
  41.         REGIONS_SCANNED=1  
  42.         REMOTE_RPC_CALLS=3  
  43.         REMOTE_RPC_RETRIES=0  
  44.         RPC_CALLS=3  
  45.         RPC_RETRIES=0  
  46.     File Input Format Counters   
  47.         Bytes Read=0  
  48.     File Output Format Counters   
  49.         Bytes Written=243  
查看以下指定的导出目录,命令如下:

[plain] view plain copy
 print?
  1. $ cd $HADOOP_HOME/  
  2. $ bin/hadoop fs -ls /data/test_table  
其中$HADOOP_HOME为hadoop的主目录。结果如下:

[plain] view plain copy
 print?
  1. Found 2 items  
  2. -rw-r--r--   3 hbase supergroup          0 2014-08-11 16:49 /data/test_table/_SUCCESS  
  3. -rw-r--r--   3 hbase supergroup        243 2014-08-11 16:49 /data/test_table/part-m-00000  
执行以下hbase shell命令,查看以下test_table表中的数据:

[plain] view plain copy
 print?
  1. $ cd $HBASE_HOME/  
  2. $ bin/hbase shell  
  3. 2014-08-11 17:05:52,589 INFO  [main] Configuration.deprecation: hadoop.native.lib is deprecated. Instead, use io.native.lib.available  
  4. HBase Shell; enter 'help<RETURN>' for list of supported commands.  
  5. Type "exit<RETURN>" to leave the HBase Shell  
  6. Version 0.98.2-hadoop2, r1591526, Wed Apr 30 20:17:33 PDT 2014  
  7.   
  8. hbase(main):001:0> describe 'test_table'  
  9. DESCRIPTION                                                                                                                               ENABLED                                                                      
  10.  'test_table', {NAME => 'cf', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '1', COMPRESSION => 'NONE', VERSIONS => true                                                                        
  11.   '1', TTL => '2147483647', MIN_VERSIONS => '0', KEEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE =>                                                                              
  12.   'true'}                                                                                                                                                                                                              
  13. 1 row(s) in 1.3400 seconds  
  14.   
  15. hbase(main):002:0> scan 'test_table'  
  16. ROW                                                    COLUMN+CELL                                                                                                                                                     
  17.  r1                                                    column=cf:q1, timestamp=1406788229440, value=va1                                                                                                                
  18.  r2                                                    column=cf:q1, timestamp=1406788265646, value=va2                                                                                                                
  19.  r3                                                    column=cf:q1, timestamp=1406788474301, value=va3                                                                                                                
  20. 3 row(s) in 0.0560 seconds  
至此,HBase表数据导出结束。接下来开始导入工作。

2)将导出到hdfs中的数据导入到hbase创建好的表中。注意,该表可以和之前的表不同名,但模式一定要相同。我们领取一个名字,使用test_copy这个表名。创建表的命令如下:

[plain] view plain copy
 print?
  1. $ cd $HBASE_HOME/  
  2. $ bin/hbase shell  
  3. 2014-08-11 17:05:52,589 INFO  [main] Configuration.deprecation: hadoop.native.lib is deprecated. Instead, use io.native.lib.available  
  4. HBase Shell; enter 'help<RETURN>' for list of supported commands.  
  5. Type "exit<RETURN>" to leave the HBase Shell  
  6. Version 0.98.2-hadoop2, r1591526, Wed Apr 30 20:17:33 PDT 2014  
  7.   
  8. hbase(main):001:0> create 'test_copy', 'cf'  
  9. 0 row(s) in 1.1980 seconds  
  10.   
  11. => Hbase::Table - test_copy  
接下来,执行导入命令。具体的命令如下:

[plain] view plain copy
 print?
  1. $ cd $HBASE_HOME/  
  2. $ bin/hbase org.apache.hadoop.hbase.mapreduce.Import test_copy hdfs://l-master.data/data/test_table  
其中,test_copy为我们想要导入的表名。而hdfs://l-master.data/data/test_table为master集群的hdfs中,我们之前将test_table表导出hdfs的全路径。

导入命令执行的结果如下,因为结果很长,所以取最后一部分:

[plain] view plain copy
 print?
  1. 2014-08-11 17:13:08,706 INFO  [main] mapreduce.Job:  map 100% reduce 0%  
  2. 2014-08-11 17:13:08,710 INFO  [main] mapreduce.Job: Job job_1407728839061_0014 completed successfully  
  3. 2014-08-11 17:13:08,715 INFO  [main] mapreduce.Job: Counters: 27  
  4.     File System Counters  
  5.         FILE: Number of bytes read=0  
  6.         FILE: Number of bytes written=117256  
  7.         FILE: Number of read operations=0  
  8.         FILE: Number of large read operations=0  
  9.         FILE: Number of write operations=0  
  10.         HDFS: Number of bytes read=356  
  11.         HDFS: Number of bytes written=0  
  12.         HDFS: Number of read operations=3  
  13.         HDFS: Number of large read operations=0  
  14.         HDFS: Number of write operations=0  
  15.     Job Counters   
  16.         Launched map tasks=1  
  17.         Rack-local map tasks=1  
  18.         Total time spent by all maps in occupied slots (ms)=6510  
  19.         Total time spent by all reduces in occupied slots (ms)=0  
  20.     Map-Reduce Framework  
  21.         Map input records=3  
  22.         Map output records=3  
  23.         Input split bytes=113  
  24.         Spilled Records=0  
  25.         Failed Shuffles=0  
  26.         Merged Map outputs=0  
  27.         GC time elapsed (ms)=21  
  28.         CPU time spent (ms)=1110  
  29.         Physical memory (bytes) snapshot=379494400  
  30.         Virtual memory (bytes) snapshot=1855762432  
  31.         Total committed heap usage (bytes)=1029177344  
  32.     File Input Format Counters   
  33.         Bytes Read=243  
  34.     File Output Format Counters   
  35.         Bytes Written=0  
接下来,我们看看从集群test_copy表中的数据是否和主集群test_table表的数据一致,执行hbase shell命令:

[plain] view plain copy
 print?
  1. $ cd $HBASE_HOME/  
  2. $ bin/hbase shell  
  3. 2014-08-11 17:15:52,117 INFO  [main] Configuration.deprecation: hadoop.native.lib is deprecated. Instead, use io.native.lib.available  
  4. HBase Shell; enter 'help<RETURN>' for list of supported commands.  
  5. Type "exit<RETURN>" to leave the HBase Shell  
  6. Version 0.98.2-hadoop2, r1591526, Wed Apr 30 20:17:33 PDT 2014  
  7.   
  8. hbase(main):001:0> scan 'test_copy'  
  9. ROW                                                    COLUMN+CELL                                                                                                                                                     
  10.  r1                                                    column=cf:q1, timestamp=1406788229440, value=va1                                                                                                                
  11.  r2                                                    column=cf:q1, timestamp=1406788265646, value=va2                                                                                                                
  12.  r3                                                    column=cf:q1, timestamp=1406788474301, value=va3                                                                                                                
  13. 3 row(s) in 0.3640 seconds  
对照后,就可以发现,两个表的数据是完全一致的。
0 0
原创粉丝点击