關(guān)于happybase中 row_prefix 參數(shù)
來(lái)源:程序員人生 發(fā)布時(shí)間:2014-12-08 09:00:08 閱讀次數(shù):3960次
起因: 使用happybase 訪問(wèn)hbase 時(shí)
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 函數(shù)中有1個(gè)row_prefix 參數(shù),而這個(gè)參數(shù)在java client 對(duì)應(yīng)函數(shù)并沒(méi)有出現(xiàn),它到底有甚么作用呢
查看源碼,我們能看到
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
看完代碼大家應(yīng)當(dāng)很明白了,row_prefix 被轉(zhuǎn)換成了row_start 和row_stop。
當(dāng)有以下場(chǎng)景
微博表
用戶ID_微博ID
假定我們想獲得此用戶的所有微博,在scan時(shí)就沒(méi)有必要設(shè)定scan范圍 '用戶ID_0' ~ '用戶ID_a'
而可以直接使用row_prefix = '用戶ID'
PS:回頭我會(huì)提供str_increment 的java 實(shí)現(xiàn)
生活不易,碼農(nóng)辛苦
如果您覺(jué)得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)