關于happybase中 row_prefix 參數
來源:程序員人生 發布時間:2014-12-08 09:00:08 閱讀次數:3954次
起因: 使用happybase 訪問hbase 時
def scan(self, row_start=None, row_stop=None, row_prefix=None,
columns=None, filter=None, timestamp=None,
include_timestamp=False, batch_size=1000, scan_batching=None,
limit=None, sorted_columns=False):
scan 函數中有1個row_prefix 參數,而這個參數在java client 對應函數并沒有出現,它到底有甚么作用呢
查看源碼,我們能看到
if row_prefix is not None:
if row_start is not None or row_stop is not None:
raise TypeError(
"'row_prefix' cannot be combined with 'row_start' "
"or 'row_stop'")
row_start = row_prefix
row_stop = str_increment(row_prefix)
str_increment 的具體代碼
def str_increment(s):
"""Increment and truncate a byte string (for sorting purposes)
This functions returns the shortest string that sorts after the given
string when compared using regular string comparison semantics.
This function increments the last byte that is smaller than ``0xFF``, and
drops everything after it. If the string only contains ``0xFF`` bytes,
`None` is returned.
"""
for i in xrange(len(s) - 1, ⑴, ⑴):
if s[i] != 'xff':
return s[:i] + chr(ord(s[i]) + 1)
return None
看完代碼大家應當很明白了,row_prefix 被轉換成了row_start 和row_stop。
當有以下場景
微博表
用戶ID_微博ID
假定我們想獲得此用戶的所有微博,在scan時就沒有必要設定scan范圍 '用戶ID_0' ~ '用戶ID_a'
而可以直接使用row_prefix = '用戶ID'
PS:回頭我會提供str_increment 的java 實現
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈