71.Bulk loading
71.1. Overview
HBase有很多方法把数据加载到table中。最直接的方法就是使用MapReduce作业中的TableOutputFormat类,或使用client APIs。然而,这些都不是最有效地方法。
bulk load使用一个MapReduce job用HBase的内部数据格式来输出表的数据,然后直接加载所产生的StoreFiles到一个正在运行的集群上。使用bulk load将使用少量的CPU和带宽资源比使用HBase的API。
71.2. Bulk Load 局限性
As bulk loading bypasses the write path, the WAL doesn’t get written to as part of the process. Replication works by reading the WAL files so it won’t see the bulk loaded data – and the same goes for the edits that use Put.setDurability(SKIP_WAL). One way to handle that is to ship the raw files or the HFiles to the other cluster and do the other processing there.
71.3. Bulk Load 架构
HBase bulk load过程分个两个主要步骤。
71.3.1 通过MapReduce job准备数据
bulk load的第一步是使用HFileOutputFormat2用MapReduce job产生HBase的数据文件(StoreFiles)。这个输出格式所写的数据是使用HBase的内部存储格式,因此,随后在把数据加载到集群中时效率会非常高。
为了实现高效,HFileOutputFormat2必须配置好,以便于每个输出的HFile都在一个单一的region内。为了达到此目的,将要把输出数据bulk load到HBase的job将会使用Hadoop的TotalOrderPartitioner类来对map输出进行分区到不同的key空间,这个key空间与表中的region的key范围一致。
HFileOutputFormat2有一个很方便的函数,configureIncrementalLoad(),这个函数会基于表的当前region边界而自动地设置一个TotalOrderPartitioner。
71.3.2 完成数据加载
数据导入准备好之后(可以使用配置项"importtsv.bulk.output"来使用importtsv工具,或者使用HFileOutputFormat的一些其他的MapReduce job),completebulkload工具被用于导入数据到正在运行的集群中。通过对准备好的数据进行命令行工具迭代,以此来决定文件所属的region。然后,它会联系最合适的RegionServer来接收HFile,把文件移动到它的存储目录中,并使得数据可以被客户端获取。
如果在bulk load的准备阶段,或在准备与完成阶段之间,region的边界有变更,工具completebulkload将会自动地分割数据文件成与新边界一致的块。这个过程不是非常高效,因此,用户应该尽量缩小准备bulk load和导入数据到集群中的时间,尤其是有其他的客户端也正在以其他方式加载数据的时候。
$ hadoop jar hbase-server-VERSION.jar completebulkload [-c /path/to/hbase/config/hbase-site.xml] /user/todd/myoutput mytable
如果HBase没有配置CLASSPATH(另外,如果zookeeper没有被HBase管理,CLASSPATH必须包含zookeeper配置文件目录),配置项 -c config-file用于指定一个包含合适的hbase参数的文件(例如hbase-site.xml)。
*如果目标表不存在,这个工具会自动创建这个表。
71.4. See Also
For more information about the referenced utilities, see ImportTsv and CompleteBulkLoad.
See How-to: Use HBase Bulk Loading, and Why for a recent blog on current state of bulk loading.
71.5. 高级用法
Although the importtsv tool is useful in many cases, advanced users may want to generate data programmatically, or import data from other formats. To get started doing so, dig into ImportTsv.java and check the JavaDoc for HFileOutputFormat.
The import step of the bulk load can also be done programmatically. See the LoadIncrementalHFiles class for more information.