Hbase的Schema设计
Preface
下面将描述几个Hbase典型数据接入用户案例,Rowkey如何设计和构造的方法;
提示:这些只是提供可能的方法,非详尽的方法,这取决于你的业务需求。
Log Data / Timeseries Data
假设如下数据元素已经归集:
- 
Hostname
 - 
Timestamp
 - 
Log event
 - 
Value/message
 
我们可以在Hbase表里存储数据并命名LOG_DATA
Timestamp In The Rowkey Lead Position(时间戳优先)
这样,rowkey格式 [timestamp][hostname][log-event] 面临rowkey线性增长的问题。
另一个模式经常提到的“桶‘,对时间戳使用MOD操作,这个将更多注意在“桶”的个数上。
long bucket = timestamp % numBuckets 
rowkey构造:
[bucket][timestamp][hostname][log-event]
基于以上,为在特定时间范围查询数据,Scan将调优各个“桶”。
比如,100个“桶”,分布在更大的键值空间,需要100 个Scans获取单个时间戳数据,因此需要平衡。
Host In The Rowkey Lead Position(主机名优先)
rowkey [hostname][log-event][timestamp] 格式是备选,如果大量主机有读写的情况下。
Timestamp, or Reverse Timestamp
取决于特定场景
Variable Length or Fixed Length Rowkeys?
当hostname & event type 长度较短的时候,rowkey会非常小。
hostname & event type 长度较长时,则有二个方法:hased 和numeric。
Rowkey Hashes组合:
- 
[MD5 hash of hostname] = 16 bytes
 - 
[MD5 hash of event-type] = 16 bytes
 - 
[timestamp] = 8 bytes
 
Rowkey Numeric组合:ref Hbase counter
- 
[substituted long for hostname] = 8 bytes
 - 
[substituted long for event type] = 8 bytes
 - 
[timestamp] = 8 bytes
 
Log Data / Timeseries on Steroids
如果是这类数据接入:
[hostname][log-event][timestamp1] [hostname][log-event][timestamp2] [hostname][log-event][timestamp3]
可能用这类分开的rowkeys:
[hostname][log-event][timerange]
Customer/Order
假设HBase存储用户和订单信息,两个核心记录类型接入:用户记录类型、订单记录类型。
客户记录信息:
- 
Customer number
 - 
Customer name
 - 
Address (e.g., city, state, zip)
 - 
Phone numbers, etc.
 
订单记录信息:
- 
Customer number
 - 
Order number
 - 
Sales date
 - 
A series of nested objects for shipping locations and line-items
 
例如客户编号、销售编号这二个属性做为组合rowkey:
[customer number][order number]
可以用二种类型:
Rowkey Hashes 组合:
- 
[MD5 of customer number] = 16 bytes
 - 
[MD5 of order number] = 16 bytes
 
Rowkey Numeric/Hash 组合 :
- 
[substituted long for customer number] = 8 bytes
 - 
[MD5 of order number] = 16 bytes
 
单表或多表
传统设计方法对CUSTOMER 和 SALES分开表方式:
单表:
Customer Record Type Rowkey:
- 
[customer-id]
 - 
[type] = type indicating `1' for customer record type
 
Order Record Type Rowkey:
- 
[customer-id]
 - 
[type] = type indicating `2' for order record type
 - 
[order]
 
CUSTOMER++这类方法优点在于能组织多个客户ID的多种记录类型,缺点是不易扫描特定记录类型。
Tall/Wide/Middle Schema Design
List Data
ref :
hbase.apache.org/book.html#schema.casestudies
                    
                
                
            
        
浙公网安备 33010602011771号