Hbase与RDBMS的区别在于:HBase的Cell(每条数据记录中的数据项)是具有版本描述的(versioned),行是有序的,列(qualifier)在所属列簇(Column families)存在的情况下,由客户端自由添加。以下的几个因素是Hbase Schema设计需要考虑的问题:
1、 Hbase中没有joins的概念
大表的结构可以使得不需要joins,而解决这一问题。
注意:HBase中没有join的概念,但是,大表的结构可以使其不需要Join操作就能解决Join操作所解决的问题。
2、Row keys 设计
主键,在Region里按字母顺序来排序(byte数组存储)。
写入要分散,如订单表: order_id做逆排序后做rowkey,以便分布式存储,避免数据只保存在个别节点上。
多条件查询时,设为组合row key
注:读取数据只能按row key(及其range)或scan全表扫描,确保查询高效
3、列族CF设计
尽量少,建议CF数量在1-2个。
设计Hbase schema的时候,要尽量只有一个column family。
flush和compaction触发的基本单位都是Region级别。当一个CF有大量的数据的时候会触发整个region里面的其他CF的memstore(其实这些memstore可能仅有少量的数据,还不需要flush的)也发生flush动作;
另外compaction触发的条件是当store file的个数(不是总的store file的大小)达到一定数量的时候会发生,而flush产生的大量store file通常会导致compaction,flush/compaction会发生很多IO相关的负载,这对Hbase的整体性能有很大影响,所以选择合适的column family个数很重要。
案例1、学生表和课程表,多对多
关系数据库中设计:
HBase中:
Student 表
| 
 Row Key  | 
 CF  | 
 CF  | 
| 
 
  | 
 info  | 
 course  | 
| 
 Student_id(reverse逆排序)  | 
 Info:name Info:age Info:sex  | 
 Course:c1 Course:c2 …  | 
Course表
| 
 Row Key  | 
 CF  | 
 CF  | 
| 
 
  | 
 info  | 
 Student  | 
| 
 Course_id(reverse逆排序)  | 
 Info:title Info:introduction Info:teacher_id  | 
 Student:t1=student_id Student:t2 …  | 
案例2、person 和 身份证card表
关系数据库:
| 
 Person表  | 
| 
 Pserson_id  | 
| 
 Name  | 
| 
 sex  | 
| 
 Card  | 
| 
 Card表  | 
| 
 ID  | 
| 
 City_id  | 
HBase中:
Person表
| 
 Row Key  | 
 CF  | 
| 
 
  | 
 info  | 
| 
 Person_id  | 
 Info:name Info:sex Info:age Info:card  | 
……
案例2、订单order 和 订单明细表order_item,一对多
关系数据库中:
Order表
| 
 Order_Id  | 
| 
 Order_mount  | 
| 
 User_id  | 
| 
 City_id  | 
| 
 ….  | 
Order_item 表
| 
 Item_Id  | 
| 
 Order_Id  | 
| 
 Product_id  | 
| 
 Unit_price  | 
| 
 Order_num  | 
| 
 ….. 
  | 
HBase 中:
方案一:
Order表
| 
 Row key  | 
 CF  | 
| 
 
  | 
 info  | 
| 
 Order_Id  | 
 Info: Order_mount Info:user_id Info:city_id …..  | 
Order_item 表
| 
 Row key  | 
 CF  | 
| 
 
  | 
 info  | 
| 
 Order_id-item_id  | 
 Info: Order_mount Info:user_id Info:city_id Info:order_id …..  | 
方案二:
Order表
| 
 Row key  | 
 CF  | 
 CF  | 
| 
 
  | 
 info  | 
 item  | 
| 
 Order_Id  | 
 Info: Order_mount Info:user_id Info:city_id Info:create_time …..  | 
 Item:i1<item_id> Item:i2 …..  | 
Order_item 表
| 
 Row key  | 
 CF  | 
| 
 
  | 
 info  | 
| 
 item_id  | 
 Info: Order_mount Info:user_id Info:city_id Info:order_id …..  | 
多条件查询时,需要增加一个index表
Row key为查询条件组合
| 
 Row key  | 
 CF  | 
| 
 
  | 
 info  | 
| 
 [create_time]_[order_id]_[city_id]  | 
 Info:order_id 
  | 
来源:https://www.cnblogs.com/zlslch/p/6140032.html