HBase 数据导入 批量导入 BulkLoad ImportTsv HFile格式

网址:https://www.jianshu.com/p/2b4390310345

1.BulkLoad介绍

  BulkLoad是HBase将海量数据写入到集群的一种方式。运行一个Mapreduce作业,使用HFileOutputFormat输出HBase数据文件,使得输出的HFile文件可以适应单个region,使用TotalOrderPartitioner类将map输出结果分区到各个不同的key区间中,每个key区间都对应着HBase表的region。之后使用completebulkload工具将第一步的结果文件依次交给负责文件对应region的RegionServer,并将文件移动到region在HDFS上的存储目录中。

  概括:HBase按照HFile格式存储在HDFS的原理,使用MapReduce直接生成HFile格式的数据文件,再通过RegionServer将HFile数据文件移动到相应的Region上去。

测试:使用脚本生成1T数据,预分区500个region进行测试。

2.ImportTsv介绍

  使用HBase API进行数据导入时,如果一次性导入大批量数据,会占用大量RegionServer的资源,影响存储在RegionServer上其他的操作,或者性能下降。

  可以将TSV格式(默认制表符分隔\t,或者自定义分隔符的其他格式数据文件也可)的数据加载到HBase中。和CompleteBulkLoad一起使用

  有两种加载方式:

  • 直接导入:使用使用TableOutputFormat在map中PUT数据到HBase中(不属于BulkLoad方式)

  • BulkLoad导入:通过MR生成HFile格式的文件,再执行CompleteBulkLoad命令将文件移动到HBase的存储目录

3.ImportTsv命令

(1)命令参数

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.



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 
org.apache.hadoop.hbase.mapreduce.TsvImporterMapper

(2)直接导入

# 前提需要进入HBase_Home
bin/hbase org.apache.hadoop.hbase.mapreduce.ImportTsv 
-Dimporttsv.columns=a,b,c <tablename> <hdfs-inputdir>


# 命令例子
bin/hbase org.apache.hadoop.hbase.mapreduce.ImportTsv 
-Dimporttsv.columns=cf:col1,cf:col2 test /hbase_tsv_input

弊端:

使用方法简单,但是在导入大批量数据的时候有可能会存在问题,尤其是column比较多的宽表导入的时候,会出现RegionTooBusyException,导致数据丢失,因此建议在数据量不是特别大并且column不是特别多的情况下使用。

(3)BulkLoad方式导入

bin/hbase org.apache.hadoop.hbase.mapreduce.ImportTsv 
-Dimporttsv.columns=a,b,c 
-Dimporttsv.bulk.output=hdfs://storefile-outputdir 
<tablename> 
<hdfs-data-inputdir>


# 命令例子
bin/hbase org.apache.hadoop.hbase.mapreduce.ImportTsv -
Dimporttsv.columns=HBASE_ROW_KEY,f1:H_NAME,f1:ADDRESS \
-Dimporttsv.separator="," -Dimporttsv.skip.bad.lines=true -
Dmapreduce.split.minsize=1073741824 -Dimporttsv.bulk.output=/tmp/hbase/hfile ImportTable 
/tmp/hbase/datadirImport

4.CompleteBulkLoad

completebulkload 实用工具可以将产生的存储文件移动到HBase表

两种方式调用工具

  • 显式类名

  • jar包驱动

(1)显式类名

# 命令格式
bin/hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles <hdfs://storefileoutput> <tablename>

# 例子
hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles 
/user/pingz/hbase/20181105 hbase_table
hbase org.apache.hadoop.hbase.mapreduce.LoadIncrementalHFiles 
/tmp/hbase/hfile ImportTable

(2)jar包驱动

# 命令格式
HADOOP_CLASSPATH=`${HBASE_HOME}/bin/hbase classpath` ${HADOOP_HOME}/bin/hadoop jar 
${HBASE_HOME}/hbase-VERSION.jar completebulkload <hdfs://storefileoutput> <tablename>

# 例子
hadoop jar hbase-VERSION.jar completebulkload [-c /path/to/hbase/config/hbase-site.xml] 
/user/todd/myoutput mytable

猜你喜欢

转载自blog.csdn.net/qq_35260875/article/details/108691948