happybase 的filter使用(python 接口)
先上一个过滤代码:
def scansha256(self, name, prefix):
dic = {}
try:
with self.pool.connection() as conn:
table = conn.table(name)
# 子字符串查询
query = "RowFilter ( =, \'substring:{0}\')".format(prefix)
# 前缀查询
# query = "PrefixFilter ( \'{0}\')".format(prefix) # prefix = '958087641sample1' prefix=958087641
for key, data in table.scan(filter=query):
dic[key] = data
except Exception as e:
print(e)
return dic
return dic
再上实际中使用的测试数据,以下为了显示明显,用换行分割了每行数据
b'2554427471sample1' {b'info:one': b'one', b'info:three': b'three', b'info:two': b'two', b'meta:crc': b'sample1_crc', b'meta:name': b'sample1', b'meta:sha1': b'sample1_sha1', b'meta:sha256': b'sample1_sha256', b'meta:sha512': b'sample1_sha512', b'meta:size': b'122'}
b'958087634sample1' {b'info:one': b'one', b'info:three': b'three', b'info:two': b'two', b'meta:crc': b'sample1_crc', b'meta:name': b'sample1', b'meta:sha1': b'sample1_sha1', b'meta:sha256': b'sample1_sha256', b'meta:sha512': b'sample1_sha512', b'meta:size': b'122'}
b'958087641sample1' {b'info:one': b'one', b'info:three': b'three', b'info:two': b'two', b'meta:crc': b'sample1_crc', b'meta:name': b'sample1', b'meta:sha1': b'sample1_sha1', b'meta:sha256': b'sample1_sha256', b'meta:sha512': b'sample1_sha512', b'meta:size': b'122'}
代码中使用到了的两种方式,且都通过测试
第一种在rowkey中查询是否存在指定的子字符串
第二种在rowkey中查询是否存在指定的前缀字符串
实现的参考:
在以上网址的107.8. Individual Filter Syntax小节中,详细描述了HBASE支持使用哪些filter
以rowkey为例,原文描述如下:
RowFilter
This filter takes a compare operator and a comparator. It compares each row key with the comparator using the compare operator and if the comparison returns true, it returns all the key-values in that row.
在使用rowfilter中,必须使用compare operator 和 a comparator。
在以上网址的107. Filter Language小节中,详细描述了HBASE的filter如何使用。在107.4. Compare Operator 和 107.5. Comparator 小节中,描述了我们要使用的compare operator 和 a comparator。这里由于是要做目标的精确过滤,因此在使用时,选择了相等 = 和子字符串过滤。其余的不相等,》=,正则表达式方式,二进制方式等,可以参考需要使用。
参考107.6. Example PHP Client Program that uses the Filter Language以及其中的几个实例,很快就得到了要用到的模式。

浙公网安备 33010602011771号