设为首页 加入收藏

TOP

hbase rowkey的设计和预分区
2019-02-19 13:43:11 】 浏览:86
Tags:hbase rowkey 设计 分区

在项目中结合使用了hive和hbase,需要把hive中的表插入到hbase,hive表都经过了打标签处理,共包含9个字段,根据业务需求,此时需将hive表中的routermac字段(string类型)设计成hbase表中的rowkey,hive中是每天一张表,hbase中是每月一张表。首先是进行预分区,由于集群共有20个regionserver,则设置40个分区


(一)统计hive表中每个月去重的routermac

去重后的routermac每个月大约有400万到700多万

首先将每天的hive表中的routermac进行去重后插入一张表并再次去重生成mac_09表,得到9月份去重的所有routermac,然后进行排序生成新表,并添加一个排序序号字段rank,如下所示:

create table hbase.temp as select row_number() over (order byroutermac) rank,* from mac_09;

count一下发现temp表一共400万行,那就比较简单了,由于routermac已经经过了排序,则可以每隔10万个routermac设置一个分区,共40个分区,此时当把9月份的hive表都插入以mac为rowkey的hbase表时,大致可以保持region的负载均衡。


提取预分区所用rowkey如下:

hive -e "select routermac from hbase.temp where rank=100000 or .... or rank=3900000;" >> ~/var/lib/hadoop-hdfs/hbase/split_09.txt


(二)新建hbase表进行并根据split_09.txt文件进行预分区

create 'tags:router09', {NAME => 'f1',COMPRESSION => 'SNAPPY', VERSIONS => 1000,MIN_VERSIONS => 50},SPLITS_FILE => '/var/lib/hadoop-hdfs/hbase/split_09.txt'

此时则可以在hbase master节点上看到tags:router09表共有40个分区


(三)在hive的hbase数据库中创建外部表和hbase中的tags:router09表进行关联

CREATE EXTERNAL TABLE hbase.tags_router09(key string,httpdate string,httpcookie string,httpreferer string,httpuseragent string,
httphost string,httpurl string,tag string,times string)
STORED BY 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
WITH SERDEPROPERTIES ("hbase.columns.mapping" = ":key,f1:httpdate,f1:httpcookie,f1:httpreferer,f1:httpuseragent,
f1:httphost,f1:httpurl,f1:tag,f1:times")
TBLPROPERTIES("hbase.table.name" = "tags:router09", "hbase.mapred.output.outputtable" = "tags:router09");


(四)将hive中的数据插入tags_router09表

将数据插入tags_router09表时,即将数据插入了hbase中的tags:router09表,如下:

hive -e "insert into table hbase.tags_router09

selectroutermac,httpdate,httpcookie,httpreferer,httpuseragent,

httphost,httpurl,tag,times from tags.phitagorc20170923
where routermac<>'NULL';"


(五)如果速度比较慢的话可以对hive表建索引


其实hbase的插入速度还是非常快的,但是如果插入语句里边增加了where routernc<>'NULL',速度就会非常慢,一种情况是对routermc字段创建索引,另一种情况是对phitagorc20170923表进行清洗,去掉routermac='NULL'的字段后,把新表插入到hbase中。


创建索引语句如下:

create index mac_index on table phitagorc20170923(routermac) as
'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler'
with deferred rebuild in table index_phitagorc20170923;


加载索引数据:

alter index mac_index on phitagorc20170923 rebuild;

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇在Ubuntu 14.04上的zookeeper+HBa.. 下一篇源码解读--(1)hbase客户端源代码

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目