我们知道,hbase表可以设置一个至多个列簇(column families),但是为什么说越少的列簇越好呢?
官网原文:
HBase currently does not do well with anything above two or three column families so keep the number of column families in your schema low. Currently, flushing and compactions are done on a per Region basis so if one column family is carrying the bulk of the data bringing on flushes, the adjacent families will also be flushed even though the amount of data they carry is small. When many column families exist the flushing and compaction interaction can make for a bunch of needless i/o (To be addressed by changing flushing and compaction to work on a per column family basis).
回顾下hbase表,每张表会切分为多个region,每个region也就是表的一部分子集数据,region会分散到hbase 集群regionserver上;
region中每个columnFamily的数据组成一个Store。每个Store由一个Memstore和多个HFile组成(一个列簇对应一个memstore和N个HFile);
在达到flush条件时候,每个memstore都会flush生成一个HFile文件;另外随着HFile文件的生成,后台minorCompact线程会触发合并HFile文件;
重点来了!flush和compact都是在region的基础上进行的!!!
比如在flush时候,如果有多个memstore(多个列簇),只要有一个memstore达到flush条件,其他的memstore即使数据很小也要跟着执行flush,这也就导致了很多不必要的I/O开销。触发flush的条件如下:
- Memstore级别限制:当Region中任意一个MemStore的大小达到了上限(hbase.hregion.memstore.flush.size,默认128MB),会触发Memstore刷新。
- Region级别限制:当Region中所有Memstore的大小总和达到了上限(hbase.hregion.memstore.block.multiplier * hbase.hregion.memstore.flush.size,默认 2* 128M = 256M),会触发memstore刷新。
- Region Server级别限制:当一个Region Server中所有Memstore的大小总和达到了上限(hbase.regionserver.global.memstore.upperLimit * hbase_heapsize,默认 40%的JVM内存使用量),会触发部分Memstore刷新。Flush顺序是按照Memstore由大到小执行,先Flush Memstore最大的Region,再执行次大的,直至总体Memstore内存使用量低于阈值(hbase.regionserver.global.memstore.lowerLimit * hbase_heapsize,默认 38%的JVM内存使用量)。
- 当一个Region Server中HLog数量达到上限(可通过参数hbase.regionserver.maxlogs配置)时,系统会选取最早的一个 HLog对应的一个或多个Region进行flush
- HBase定期刷新Memstore:默认周期为1小时,确保Memstore不会长时间没有持久化。为避免所有的MemStore在同一时间都进行flush导致的问题,定期的flush操作有20000左右的随机延时。
同样在compact时候,由于是建立在region的基础上,同样会产生不必要的I/O开销,触发compcat(minor_compact)条件:
hbase.hstore.compactionThreshold Description If more than this number of HStoreFiles in any one HStore (one HStoreFile is written per flush of memstore) then a compaction is run to rewrite all HStoreFiles files as one. Larger numbers put off compaction but when it runs, it takes longer to complete. default 3
Where multiple ColumnFamilies exist in a single table, be aware of the cardinality (i.e., number of rows). If ColumnFamilyA has 1 million rows and ColumnFamilyB has 1 billion rows, ColumnFamilyA’s data will likely be spread across many, many regions (and RegionServers). This makes mass scans for ColumnFamilyA less efficient.
另外,如果一个表中存在多个列族,请注意数据量(即,行数)。如果ColumnFamilyA有100万行,而ColumnFamilyB有10亿行,ColumnFamilyA的数据很可能分布在许多许多regions(和regionservers)。这使得ColumnFamilyA的大规模scan效率降低。(我们知道hbase split是由参数hbase.hregion.max.filesize值来控制的,但是,触发region split不是说该region下所有的HFile文件大小达到这个值就会触发split,而是region下某个HFile文件达到了这个值才会执行split,也就是说这里ColumnFamilyB在做split时候,ColumnFamilyA的数据量还很小很小,但是也会被带着执行split,这也就会导致更多的HDFS小文件,并且分散到更多的region和regionservers上)