参考官网:
https://happybase.readthedocs.io/en/latest/index.html#
1、简介
happybase 模块是python提供操作hbase的第三方模块,此模块借助于thrift方式进行操作
thrift 是facebook开发并开源的一个二进制通讯中间件,通过thrift,我们可以用Python来操作Hbase
首先开启Hadoop平台的HadoopMaster的thrift服务,用Xshell连接HadoopMaster,用root用户登录,
如果想关闭终端之后,thrift服务继续运行,可以用daemon模式运行
对hbase的操作分为4类:
get,put,scan,delete
通过htable对象进行操作,用户可以完成向hbase存储和检索数据,以及删除无效数据之类的操作了;
创建htable实例的代价非常大,每个实例都要扫描.META.表,以及检测该表是否存在,是否可用,这些检测导致了实例的调用是非常耗时的;
因此,我们推荐用户只创建一次Htable实例,而且每个线程创建一个,如果用户需要使用多个Htable实例,应该考虑使用HTablePool类,他可以服用多个Htable实例
2、安装
pip install happybase
pip install thrift
注意:如果连接python连接hbase的时候报错:thrift.transport.TTransport.TTransportException: Could not connect to s86:9090
连接的时候,报上面的错误:
原因:
1、ip地址或者主机名写错了,需要是hbase的master节点ip地址或者主机名
2、hbase的thrift没有启动
# 查看9090端口是否启用
netstat -nl | grep 9090
# 启动hbase thrift server
hbase thrift -p 9090 start & 【后面加个&,是后台启动】【说明,这里一定要指定端口,要不操作的会卡着不动的】
3、API简介
3.1、连接hbase
连接(happybase.Connection)
happybase.Connection(host=’localhost’, port=9090, timeout=None, autoconnect=True, table_prefix=None, table_prefix_separator=b’_’, compat=’0.98’, transport=’buffered’, protocol=’binary’)
获取连接实例
host:主机名
port:端口
timeout:超时时间
autoconnect:连接是否直接打开
table_prefix:用于构造表名的前缀
table_prefix_separator:用于table_prefix的分隔符
compat:兼容模式
transport:运输模式
protocol:协议
【示例】
import happybase
connection = happybase.Connection(
host='192.168.1.182',
port=9090,
timeout=10,
autoconnect=True,
table_prefix='byd',
table_prefix_separator='_',
compat='0.98',
transport='buffered',
protocol='binary'
)
在Windows下面会报下面的异常:
解决方案:将488行的url_scheme == ''改为url_scheme in ('d', ''),即