HBase系列(三):利用SQL BulkLoad快速导入海量数据

版权声明:如需转载,请注明出处 https://blog.csdn.net/whdxjbw/article/details/81145672

HBase BulkLoad概述

直接用put命令将海量数据导入至HBase会耗费大量时间(HFile合并、Region分割等),而且用原生的put命令导入海量数据,会占用HRegionServer很多资源,HBase集群会变得压力山大,集群基本对外丧失写的能力。

其实HBase中数据以HFile文件的形式存储于HDFS,所以我们可以绕过HBase put API,直接将数据加工成HFile文件,再将其加载到Hyperbase中,从而完成大规模数据的快速入库。

BulkLoad实现路线(技术选型)

原生BulkLoad需要编写mapreduce代码,对数据文件进行处理,通过HBase提供的api转为HFile。但这里我们选用HyperBase+Inceptor的方式实现BulkLoad,它底层其实是将SQL命令转为mapreduce任务。整体流程:

对数据集预分Region,获取SplitKey  - > 根据SplitKey创建HyperDrive表,预分Region -> 将数据转为HFile文件并加载至HBase -> 为Index数据划分Region,Rebuild Index。

rebuild索引的操作大家参考此篇博文:

HBase二级索引实践

废话不多说,开搞:

准备数据

将数据文件上传至HDFS。

hadoop fs -mkdir -p /tmp/jbw/bulkLoadData
hadoop fs -put data.csv /tmp/jbw/bulkLoadData

根据SplitKey预分Region 

观察数据,发现id编号从10000000到19999999,一共10000000条数据,故在预分Region环节,建表DDL如下。

此过程即预先定义最终HBase表实际分布式存储的结构(划分多少个Region)

drop table if exists hyper_table;

-- 此表在后续会从外表中查询并导入数据
create table hyper_table
(rowkey string
  , num int
  , country int
  , rd string)
stored as hyperdrive
tblproperties('hyperdrive.table.splitkey' = '"110000000",
              "120000000",
              "130000000",
              "140000000",
              "150000000",
              "160000000",
              "170000000",
              "180000000",
              "190000000"'
);

导入HDFS上数据至外表

创建外表,从HDFS中导入数据至外表。

drop table if exists hyper_external_table;
create external table hyper_external_table
(rowkey string
  , num int
  , country int
  , rd string)
row format delimited fields terminated by ','
location '/tmp/jbw/bulkLoadData';

导入过程挺快的,千万条数据,秒级导入。 

 

使用BulkLoad方式从数据外表中导入至最终HBase表 

-- 用bulkload方式导入预先分好region的hyper_table表中
insert overwrite table hyper_table
select
    /*+ USE_BULKLOAD */
    rowkey
     , num
     , country
     , rd
from
    hyper_external_table
order by
     rowkey;

利用SQL BulkLoad方式,我们仅用了32秒即完成了千万条数据的导入。

查询可见,数据均以成功导入。

好了,可以发现用BulkLoad黑科技导入数据至HBase,既方便又快捷,生产环境海量数据入库,基本都是使用此方式。

猜你喜欢

转载自blog.csdn.net/whdxjbw/article/details/81145672
今日推荐