您的位置:首页 > 编程语言 > Python开发

python通过thrift连接Hbase

2016-07-07 09:56 501 查看
第一步:安装python

通过本机下载Python源码 下载地址

https://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgz

将源码上传至19服务器,路径为:/usr/local/share/

运行解压缩:tar -xzf Python-2.7.5.tgz

进入解压文件夹:cd Python-2.7.5 依次运行 以下命令

./configure
make
make install


在打印日志中可以看到,Python的安装路径为:/usr/local/lib/python2.7

第二步:安装thrift

通过本机下载thrift源码 下载地址

https://mirrors.tuna.tsinghua.edu.cn/apache/thrift/0.9.3/thrift-0.9.3.tar.gz

将源码上传至19服务器,路径为:/usr/local/share/

运行解压缩:tar -xzf thrift-0.9.3.tar.gz

进入解压文件夹:cd thrift-0.9.3 依次运行 以下命令

./configure
make
make install


第三步:python添加thrift模块

cd /usr/local/share/thrift-0.9.3/lib/py
python setup.py install


运行成功后,可在python运行环境/usr/local/lib/python2.7/site-packages中找到thrift模块

第三步:生成接口文件

通过本机下载hbase源码 下载地址

https://mirrors.tuna.tsinghua.edu.cn/apache/hbase/0.98.20/hbase-0.98.20-src.tar.gz

将源码上传至19服务器,路径为:/usr/local/share/

运行解压缩:tar -xzf hbase-0.98.20-src.tar.gz

cd /usr/local/share/hbase-0.98.20/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift

thrift –gen py Hbase.thrift

第四步:python添加hbase模块

cp -r gen-py/hbase/ /usr/local/share/Python-2.7.5/Lib/site-packages

运行成功后,可在python运行环境/usr/local/lib/python2.7/site-packages中找到hbase模块

第四步:启动thrift服务

登录202服务器,启动hbase的thrift服务

hbase thrift -p 9090 start

运行成功

第五步:运行test

test.py

import sys
sys.path.append('/usr/local/lib/python2.7/site-packages/hbase')
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import *

transport = TSocket.TSocket('10.18.210.202', 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)
transport.open()
print(client.getTableNames())


运行成功后,可打印hbase中的表名

相关引用:

http://blog.sina.com.cn/s/blog_9f48885501018g03.html

http://shineforever.blog.51cto.com/1429204/1567640
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python hbase thrift