happybase 是使用python链接hbase的一个第三方库,目前基于thrift1 。在使用过程当中常常碰到报错html
TTransportException(type=4, message='TSocket read 0 bytes')python
即便使用thrift server首页上提供了链接Apache HBase Wiki on Thrift里的demo也同样报错。apache
import happybase def get_tables_name(host,port): conn = happybase.Connection(host=host,port=port) return conn.tables()
很简单,就是链接hbase,返回全部table名称。api
看一下官网wiki上创建链接的例子app
from thrift.transport.TSocket import TSocket
from thrift.transport.TTransport import TBufferedTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
transport = TBufferedTransport(TSocket(host, port))
transport.open()
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)oop
构建一个链接须要两个辅助类 TBufferedTransport,TBinaryProtocol。其中transport负责通信协议,protocol负责序列化协议。测试
happybase官网上对于connection的介绍connection的介绍code
总结有三个参数须要匹配server
在我将上述测试代码改成htm
import happybase def get_tables_name(host,port): conn = happybase.Connection(host=host,port=port,protocol='compact',transport='framed') return conn.tables()
就没问题了。