python利用thrift服务读取hbase数据的方法

yipeiwu_com6年前Python基础

因工作需要用python通过hbase的thrift服务读取Hbase表数据,发现公司的测试环境还不支持,于是自己动手准备环境,在此我将在安装步骤尽可能描述清楚,旨在给第一次动手安装的朋友,此过程亲测成功!

安装过程如下:

1、首先确保hbase安装测试成功,再者确认下hbase的thrift服务是否启动,注意目前的Hbase(本文基于版本0.98.17)有两套thrift接口thrift和thrift2,本文使用thrift,启动命令:hbase thrift -p 9090 start,确保该端口没有被占用,使用lsof -i:9090查看),本公司测试环境该端口被占用,如果被占用换一个没有被占用的端口即可;

2、安装thrift,去官网下载thrift:http://thrift.apache.org/download,本人使用 thrift-0.10.0.tar.gz ,下载好后编译安装,解压后进入安装目录/home/hadoop/thrift-0.10.0,分别执行./configure make,make,sudo make install,注意这边可能因各环境不同可能遇到问题,具体安装所需环境请参考官网:http://thrift.apache.org/docs/install/centos,安装之后可以用thrift -version命令测试是否安装成功,安装成功后会显示安装的版本;

3、确保1和2没有问题,接下来需要生成python脚本需要导入的hbase相关模块,首先去官网下载hbase源码,注意虽然本公司用的hbase版本是0.98.17但是只要版本相差不大都可以使用,本人使用的是 hbase-0.98.24-src.tar.gz,下载解压后找到thrift目录:hbase-0.98.24/hbase-thrift/src/main/resources/org/apache/hadoop/hbase,该目录下有两个thrift服务,进入thrift后执行thrift --gen pyHbase.thrift,不出现问题会在该thrift目录下生成目录 gen-py,里面具体是hbase模块,将该目录名称改为hbase,并拷贝进python模块包:cp -r hbase /usr/lib/python2.7/site-packages/,至此python需要使用的hbase模块已经准备好;

4、写python脚本测试

#! /usr/bin/python
import sys
sys.path.append('/usr/lib/python2.7/site-packages/hbase') # 引入正确的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('101.71.51.221', 9099)
transport = TTransport.TBufferedTransport(transport)
 
protocol = TBinaryProtocol.TBinaryProtocol(transport)
 
client = Hbase.Client(protocol)
 
transport.open()
 
tableName = 'hb_vender'
rowKey = '17_bcc5f02a876b010cbcd3fb2f82ab5b8e_43_111_57_437b9e2a-257c-4115-9570-bcd61741b3dc'
 
result = client.getRow(tableName, rowKey, None)
print result
for r in result:
	print 'the row is ' , r.row
	print 'the values is ' , r.columns.get('a:venderName').value

注意这边可能出现:ImportError: No module named six,因为需要安装six,如果已经安装pip,使用pip install six,如果没有安装用root执行安装:easy_install six,安装成功后执行脚本测试成功!

以上这篇python利用thrift服务读取hbase数据的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python使用pyqt写带界面工具的示例代码

python使用pyqt写带界面工具的示例代码

上篇介绍的使用python自带tkinter包,来写带界面的工具。 此篇介绍使用pyqt来开发测试工具。 tkinter的好处是python官方自带,上手容易(但手写控件复杂),布局和摆...

Python转换时间的图文方法

Python转换时间的图文方法

time模块常用的中时间的转换。 python中的时间戳:通俗讲就是某个时刻的时间,单位是秒; 获取当前时间的时间戳: time.time() 1)没有参数, 2)返回从1970年1月1...

python 接口返回的json字符串实例

如下所示: JSON 函数 使用 JSON 函数需要导入 json 库:import json。 函数 描述 json.dumps 将 Python 对象编码成 JSON 字符串...

使用 Python 合并多个格式一致的 Excel 文件(推荐)

使用 Python 合并多个格式一致的 Excel 文件(推荐)

一 问题描述 最近朋友在工作中遇到这样一个问题,她每天都要处理如下一批 Excel 表格:每个表格的都只有一个 sheet,表格的前两行为表格标题及表头,表格的最后一行是相关人员签字。最...

python文档字符串(函数使用说明)使用详解

python文档字符串(函数使用说明)使用详解

1.效果图: 2.代码: # 文档字符串( doc str) 是 函数使用说明 # 用法: 在函数第一行写一个字符串 def fn(*nums): ''' 函数的作用: 计...