python操作hbase

1 python安装thrift拓展

(1) linux环境下安装

解压安装后,直接安装即可,注意安装包在https://pypi.python.org/pypi/thrift/0.11.0上下载,在apache上下载的,不适用本方法安装

tar -zxvf thrift-0.11.0.tar.gz
cd thrift-0.11.0/
python3 setup.py install
(2) windows环境下安装

安装包跟linux下的一样,安装前,先将安装包解压,安装在dos界面操作。在dos里进入解压后的目录执行下面命令

python setup.py install

2 安装thrift

在linux里安装即可。在Apache上下载安装包http://thrift.apache.org/download

tar -zxvf thrift-0.11.0.tar.gz
cd thrift-0.11.0/
./configure
make
make install
  • g++: command not found的解决
yum -y install gcc+ gcc-c++

3 编译hbase thrift

下载对应hbase的源码包,在hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift路径下执行

thrift -gen py Hbase.thrift

执行后,会有gen-py文件夹,将里面的hbase文件夹copy到python3的site-packages里,site-packages根据个人安装情况选择

cp -R hbase /usr/python3/lib/python3.6/site-packages

在linux执行后,将hbase包copy到windows的python环境

4 python查询hbase的demo

#!/usr/bin/python3
from hbase import Hbase
from thrift.protocol import TBinaryProtocol
from thrift.transport import TSocket, TTransport

#配置thriftServer主机名,端口
transport = TSocket.TSocket('A5-302-NF8460M3-157',9090)
#设置超时
transport.setTimeout(5000)
#设置传输方式(TFramedTransport或TBufferedTransport)
trans = TTransport.TBufferedTransport(transport)
#设置传输协议
protocol = TBinaryProtocol.TBinaryProtocol(trans)
#确定客户端
client = Hbase.Client(protocol)
#打开连接
transport.open()
#获取表名
client.getTableNames()

5 使用happyhbase操作hbase

(1) 安装happyhbase扩展

使用该扩展的之前,需要安装thrift

  • windows下安装
pip install happybase

安装完成后,修改site-packages\thriftpy\parser\parser.py,大约488行

if url_scheme == '':

修改为:

if url_scheme == '' or url_scheme == 'c':  
  • linux下安装
pip3 install happybase
(2) demo样例
#!/usr/bin/python3
import happybase

# 创建hbase链接
# 自动链接方式
connection = happybase.Connection('192.168.59.129')
print(connection.tables())


# 非自动链接方式
connection2 = happybase.Connection('192.168.59.129', autoconnect=False)
connection2.open()
print(connection2.tables())

猜你喜欢

转载自my.oschina.net/u/1188945/blog/1796461
今日推荐