HBase的bulkload使用方法

1.简介

  将数据插入HBase表中的方法很多,我们可以通过TableOutputFormat以Mapreduce on HBase的方式将数据插入,也可以单纯的使用客户端API将数据插入。但是以上方法效率并不高。

而使用BulkLoad特性能够利用MR计算框架将源数据直接生成内部的hfile格式,然后可以在不重启HBase集群的场景下数据load到对应表中。

  BulkLoad方法能够将数据快速的load到HBase中,打一个“生动”的比方:

使用API就好比将饭一口一口喂给HBase,而使用BulkLoad就相当于切开HBase的肚子直接将食物放到胃中。(重口味的比方)

 

2.限制

  1. Bulkload方式由于并不是通过API来插入数据而是直接生成HFile文件所以并不会记录WAL日志。如果集群直接是通过Replication机制来备份的话(Replication机制是通过读取WAL日志来备份数据的),那么另外一个集群上就不会有Bulkload的数据。

  

3.前准备

  1. 在HBase客户端建立一张目标表:

hbase(main):003:0> create 'bulkload_text','f'
0 row(s) in 0.5230 seconds

  2. 准备数据源,在hdfs上建立一个文本文件,假设取名为bulkload_reouse_file.txt内容如下:

rowKey1|a_1|b_1
rowKey2|a_2|b_2
rowKey3|a_3|b_3
rowKey4|a_4|b_4
rowKey5|a_5|b_5

  并将其保存在hdfs上,作为bulkload的数据源:

复制代码
[hadoop@xufeng-3 bulkload]$ hadoop fs -ls /testdata/bulkload
16/07/30 16:33:55 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 1 items
-rw-r--r--   1 hadoop supergroup         80 2016-07-30 16:33 /testdata/bulkload/bulkload_reouse_file.txt 
复制代码

 

4. 实现步骤

  使用Bulkload需要经过两个大步骤。

  1.通过MR计算框架进行HFile文件的生成。

  2.加载HFile文件到集群(表)

 

1.通过MR计算框架进行HFile文件的生成

命令格式:

HADOOP_CLASSPATH=`$HBASE_HOME/bin/hbase classpath` hadoop jar $HBASE_HOME/lib/hbase-server-version.jar importtsv -Dimporttsv.bulk.output=<输出文件夹路径> -Dimporttsv.separator=<分割符> -Dimporttsv.columns=<key和列映射> <目标表> <数据源路径>

如本例中结合自身接群可以写成:

HADOOP_CLASSPATH=`/opt/hadoop/hbase/bin/hbase classpath` hadoop jar /opt/hadoop/hbase/lib/hbase-server-1.0.0-cdh5.4.2.jar importtsv -Dimporttsv.bulk.output=hdfs://ns1/testdata/bulkload/result -Dimporttsv.separator='|' -Dimporttsv.columns=HBASE_ROW_KEY,f:a,f:b bulkload_text hdfs://ns1/testdata/bulkload/bulkload_reouse_file.txt

  其中  -Dimporttsv.columns=HBASE_ROW_KEY,f:a,f:b的意思是通过'|'分隔符号分割的第一个元素作为rowkey,第二个元素作为f:a列值,第三个元素作为f:b值。

结果:

  在目标路径文件夹下生成了f列的hife文件:

复制代码
[hadoop@xufeng-3 bulkload]$ hadoop fs -ls /testdata/bulkload/result
16/07/30 16:56:00 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 2 items
-rw-r--r--   1 hadoop supergroup          0 2016-07-30 16:46 /testdata/bulkload/result/_SUCCESS
drwxr-xr-x   - hadoop supergroup          0 2016-07-30 16:53 /testdata/bulkload/result/f
复制代码

 

2.加载HFile文件到集群(表)

  生成的HFile必须尽快的去load到表中,在第一个步骤中HFile生成的规则是一个region一个文件,如果不尽快加载一旦线上的region发生分裂就会造成加载的性能下降。

命令格式:

HADOOP_CLASSPATH=`$HBASE_HOME/bin/hbase classpath` hadoop jar $HBASE_HOME/lib/hbase-server-version.jar completebulkload <生成的HFile路径> <目标表名称> 

 

如本例中结合自身接群可以写成:

HADOOP_CLASSPATH=`/opt/hadoop/hbase/bin/hbase classpath` hadoop jar /opt/hadoop/hbase/lib/hbase-server-1.0.0-cdh5.4.2.jar completebulkload hdfs://ns1/testdata/bulkload/result bulkload_text

 

结果: 

  文件中信息按照key和列的映射关系load到了表中

复制代码
hbase(main):002:0> scan 'bulkload_text'
ROW                                            COLUMN+CELL                                                                                                                           
 rowKey1                                       column=f:a, timestamp=1469911544908, value=a_1                                                                                        
 rowKey1                                       column=f:b, timestamp=1469911544908, value=b_1                                                                                        
 rowKey2                                       column=f:a, timestamp=1469911544908, value=a_2                                                                                        
 rowKey2                                       column=f:b, timestamp=1469911544908, value=b_2                                                                                        
 rowKey3                                       column=f:a, timestamp=1469911544908, value=a_3                                                                                        
 rowKey3                                       column=f:b, timestamp=1469911544908, value=b_3                                                                                        
 rowKey4                                       column=f:a, timestamp=1469911544908, value=a_4                                                                                        
 rowKey4                                       column=f:b, timestamp=1469911544908, value=b_4                                                                                        
 rowKey5                                       column=f:a, timestamp=1469911544908, value=a_5                                                                                        
 rowKey5                                       column=f:b, timestamp=1469911544908, value=b_5                                                                                        
5 row(s) in 0.3520 seconds
复制代码

 

 

5. 源码中bulkload注释信息:

复制代码
Usage: importtsv -Dimporttsv.columns=a,b,c <tablename> <inputdir>

Imports the given input directory of TSV data into the specified table.

The column names of the TSV data must be specified using the -Dimporttsv.columns
option. This option takes the form of comma-separated column names, where each
column name is either a simple column family, or a columnfamily:qualifier. The special
column name HBASE_ROW_KEY is used to designate that this column should be used
as the row key for each imported record. You must specify exactly one column
to be the row key, and you must specify a column name for every column that exists in the
input data. Another special column HBASE_TS_KEY designates that this column should be
used as timestamp for each record. Unlike HBASE_ROW_KEY, HBASE_TS_KEY is optional.
You must specify atmost one column as timestamp key for each imported record.
Record with invalid timestamps (blank, non-numeric) will be treated as bad record.
Note: if you use this option, then 'importtsv.timestamp' option will be ignored.

By default importtsv will load data directly into HBase. To instead generate
HFiles of data to prepare for a bulk data load, pass the option:
  -Dimporttsv.bulk.output=/path/for/output
  Note: if you do not use this option, then the target table must already exist in HBase

Other options that may be specified with -D include:
  -Dimporttsv.skip.bad.lines=false - fail if encountering an invalid line
  '-Dimporttsv.separator=|' - eg separate on pipes instead of tabs
  -Dimporttsv.timestamp=currentTimeAsLong - use the specified timestamp for the import
  -Dimporttsv.mapper.class=my.Mapper - A user-defined Mapper to use instead of TsvImporterMapper
For performance consider the following options:
  -Dmapred.map.tasks.speculative.execution=false
  -Dmapred.reduce.tasks.speculative.execution=false
复制代码

 

6. 总结

  bulkload提供一种快速的数据加载方法,使得外部数据以资源最小化的方式加载的HBase中。

  当你在加载数据的时候遇到如下情况,那么使用bulkload或许是一个不错的选择:

  • You needed to tweak your MemStores to use most of the memory.
  • You needed to either use bigger WALs or bypass them .
  • Your compaction and flush queues are in the hundreds.
  • Your GC is out of control because your inserts range in the MBs.
  • Your latency goes out of your SLA when you import data.

7. 参考

  http://blog.cloudera.com/blog/2013/09/how-to-use-hbase-bulk-loading-and-why/

  https://hbase.apache.org/book.html#arch.bulk.load

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值