---
title: "happybase访问Hbase"
author: "jack_boy"
date: "2015-01-08T14:40:12Z"
tags: []
categories: ["数据库"]
slug: "happybase-hbase"
draft: false
lastmod: "2015-01-08T14:40:12Z"
id: 2173946
url: "https://jack-boy.iteye.com/blog/2173946"
---

Java以为的语言访问Hbase都是通过thrift进行访问的，因此需要在Hbase上开启thrift服务
如果用

```shell
hbase-deamon.sh start
```

启动服务，默认用的是thrift2，在用happybase的时候会出现版本不兼容的问题，因此用thrift来启动

```shell
hbase thrift -p 9090 start
```

happybase使用很简单，与hbase shell 用法类似
主要的用法都很简单，在其官方网站[happybase](http://happybase.readthedocs.org/en/latest/user.html)上都有。
这里主要记录一个错误。

```python
  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
```


源代码：

```python
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
```


改为：
<span style="color:blue">g = table.scan(filter="SingleColumnValueFilter('f', 'id', =, 'substring:852223')", limit=10)</span>
程序正确运行，输出结果


> 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