你的分享就是我们的动力 ---﹥

happybase访问Hbase

时间:2015-01-26 17:25来源:www.chengxuyuans.com 点击:
Java以为的语言访问Hbase都是通过thrift进行访问的,因此需要在Hbase上开启thrift服务
如果用
hbase-deamon.sh start

启动服务,默认用的是thrift2,在用happybase的时候会出现版本不兼容的问题,因此用thrift来启动
hbase thrift -p 9090 start

happybase使用很简单,与hbase shell 用法类似
主要的用法都很简单,在其官方网站happybase上都有。
这里主要记录一个错误。
  File "test.py", line 21, in <module>
    for k, d in g:
  File "/home/jack_boy/product/lib/python2.6/site-packages/happybase/table.py", line 374, in scan
    self.name, scan, {})
  File "/home/jack_boy/product/lib/python2.6/site-packages/happybase/hbase/Hbase.py", line 1919, in scannerOpenWithScan
    return self.recv_scannerOpenWithScan()
  File "/home/jack_boy/product/lib/python2.6/site-packages/happybase/hbase/Hbase.py", line 1937, in recv_scannerOpenWithScan
    raise x
thrift.Thrift.TApplicationException: Internal error processing scannerOpenWithScan


源代码:
import happybase
import sys

#reload(sys)
#sys.setdefaultencoding('utf-8')

connection = happybase.Connection('192.168.1.110')

#connection.open()
#print connection.tables() 
table = connection.table('testTable')


g = table.scan(filter="SingleColumnValueFilter('f', 'id', =, '852223')", limit=10)
for k, d in g:
        print k, d


改为:
g = table.scan(filter="SingleColumnValueFilter('f', 'id', =, 'substring:852223')", limit=10)
程序正确运行,输出结果

引用The general syntax of a comparator is: ComparatorType:ComparatorValue

The ComparatorType for the various comparators is as follows:

BinaryComparator - binary

BinaryPrefixComparator - binaryprefix

RegexStringComparator - regexstring

SubStringComparator - substring

The ComparatorValue can be any value.

Example 12.3. Example 1

>, 'binary:abc' will match everything that is lexicographically greater than "abc"


Example 12.4. Example 2

=, 'binaryprefix:abc' will match everything whose first 3 characters are lexicographically equal to "abc"


Example 12.5. Example 3

!=, 'regexstring:ab*yz' will match everything that doesn't begin with "ab" and ends with "yz"


Example 12.6. Example 4

=, 'substring:abc123' will match everything that begins with the substring "abc123"



参考:
http://hbase.apache.org/book/thrift.html#example-filter-strings
https://github.com/wbolster/happybase/blob/master/tests/test_api.py

转载注明地址:http://www.chengxuyuans.com/Python/89418.html

推荐文章