happybase(TSocket read 0 bytes)

关于报错

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

报错可能缘由

  • hbase 未开启thrift服务
  • Connection参数与thrift服务不匹配

Connection参数

看一下官网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

构建一个链接须要两个辅助类 TBufferedTransportTBinaryProtocol。其中transport负责通信协议,protocol负责序列化协议。测试

happybase connection

happybase官网上对于connection的介绍connection的介绍code

总结有三个参数须要匹配server

  • protocol: binary (the default) and compact
  • compat :0.90, 0.92, 0.94, or 0.96 (the default)
  • transport :buffered (the default) and framed

在我将上述测试代码改成htm

import happybase
def get_tables_name(host,port):
    conn = happybase.Connection(host=host,port=port,protocol='compact',transport='framed')
    return conn.tables()

就没问题了。

相关文章
相关标签/搜索