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)::> create 'bulkload_text','f'
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

最新文章

  1. Jenkins 2.16.3默认没有Launch agent via Java Web Start,如何配置使用
  2. Knockout.js随手记(6)
  3. Python私有函数和公开函数
  4. javax.el.PropertyNotFoundException: Property &#39;name&#39; not found on type java.lang.String
  5. 用户环境配置文件/etc/profile
  6. vtiger 支持 物业收费功能 微信收费
  7. nginx 编译选项
  8. C#复制数据库,将数据库数据转到还有一个数据库
  9. Delphi一共封装(超类化)了8种Windows基础控件和17种复杂控件
  10. 代码创建xml文档并写入指定节点
  11. Scala编程入门---面向对象编程之对象
  12. [转]Linux 基本操作(RM 删除)
  13. malloc()
  14. spring mvc后端校验validator
  15. postman(二):使用postman发送get or post请求
  16. 连接SQLsever数据库在C#中不能操作的问题
  17. Convert java.lang.String to oracle.jbo.domain.Date
  18. 转载 Servlet3 的 @WebServlet http://www.cnblogs.com/luxh/archive/2012/06/06/2537458.html
  19. [转]11个在线编码大赛,与全球程序员PK
  20. mysql触发器的实战经验-不错的文章

热门文章

  1. BZOJ3714 PA2014Kuglarz(最小生成树)
  2. css基于文件格式使用不同的样式
  3. 已知UIScrollView放大后的Frame和放大之前的Frame计算放大的瞄点坐标
  4. LINUX内核设计与实现第三周读书笔记
  5. (转)IO复用,AIO,BIO,NIO,同步,异步,阻塞和非阻塞 区别
  6. 《用Apache HttpClient实现URL重定向》
  7. poj 3636
  8. 拓扑排序 最大字典序+优先队列 BZOJ 4010
  9. Windows下搭建网络代理
  10. 遍历hashmap