使用HBase和Python过滤整数
我正在尝试从一个HBase表中筛选行(我使用的是HappyBase),具体来说,我想获取'id'小于1000的行:
for key, data in graph_table.scan(filter="SingleColumnValueFilter('cf', 'id', <, 'binary:1000')"):
print key, data
结果如下:
<http://ieee.rkbexplorer.com/id/publication-d2a6837e67d808b41ffe6092db50f7cc> {'cf:type': 'v', 'cf:id': '100', 'cf:label': '<http://www.aktors.org/ontology/portal#Proceedings-Paper-Reference>'}
<http://www.aktors.org/ontology/date#1976> {'cf:type': 'v', 'cf:id': '1', 'cf:label': '<http://www.aktors.org/ontology/support#Calendar-Date>'}
<http://www.aktors.org/ontology/date#1985> {'cf:type': 'v', 'cf:id': '10', 'cf:label': '<http://www.aktors.org/ontology/support#Calendar-Date>'}
在这个表中,'id'的值从1到1000都有。如果我用Java和HBase的Java库来写这个代码,它运行得很好,使用Byte.toBytes()函数来处理整数值。
谢谢。
1 个回答
4
好吧,问题是我把整数保存成了字符串,其实正确的做法是把它们保存为字节:
table.put(key, {'cf:id': struct.pack(">q", value)})
在查询数据库时,过滤器中的值也需要打包:
for key, data in graph_table.scan(filter="SingleColumnValueFilter('cf', 'id', <, 'binary:%s', true, false)" % struct.pack(">q", 1000)):
print key, data
最后,要把结果解包:
value = struct.unpack(">q", data['cf:id'])[0]
非常感谢。