天天看點

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 by routermac) 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

 select routermac,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;

繼續閱讀