python使用thrift2协议connect hbase

搜索了一下,网上大多都是使用thrift1来链接hbase的,其实,现在线上大多使用的都是thrift2协议,那么到底python使用thrift2怎样连接hbase呢?

下面是具体步骤:

1,下载thrift2库:http://thrift.apache.org/ 下载最新的 0.9.2 src版本(最新的为0.10),如果要以前的版本,也可到 http://archive.apache.org/dist/thrift 去下载 ,

也可以 ,GIT Checkout,svn co 等等

2,编译安装:具体有多种方法,Maven,./configure   make   make install ...

3,生成py代码:thrift -o -gen py {对应版本的hbase源码地址}\src\main\resources\org\apache\hadoop\hbase\thrift2

 (或直接下载我生成好的code,见本文下方下载链接) 

4,将对应版本中python接口code复制到当前操作目录,{对应版本的thrift2源码地址}\lib\py\src...

5,参考sql脚本:

假设此表的创建结构如下:

create 'topic_test',{NAME => 'cf', VERSIONS => 5}, SPLITS=>['10','20','30','40']
6,写测试脚本:

import sys  

sys.path.append('./thrift/packages')
sys.path.append('./thrift')

from thrift import Thrift  
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.packages.hbase import THBaseService
from thrift.packages.hbase.ttypes import *

transport = TSocket.TSocket('10.255.1.35', 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = THBaseService.Client(protocol)

transport.open()
#do something ...
#get , put, delete and so on as below : 
transport.close()
#----------------下面的几部分放在transport open 和 close 的中间---------------------------------
tableName = 'topic_test'
rowKey = 'row_114392867856261'

##put------start-----------
coulumnValue1 = TColumnValue('cf','title','test')
coulumnValue2 = TColumnValue('cf','content','hello world')
coulumnValue3 = TColumnValue('cf','created_time',created)
coulumnValue4 = TColumnValue('cf','modified_time',modified)
coulumnValues = [coulumnValue1,coulumnValue2,coulumnValue3,coulumnValue4]
print 'coulumnValues is ', coulumnValues
tPut = TPut(rowKey,coulumnValues)
print 'tPut is ', tPut
##put-----end---------

##get------start-----------
get = TGet()
get.row = rowKey
result = client.get(tableName, get)
#print resultprint result.row
##get-----end---------

##delete-----start---------
coulumnValue1 = TColumnValue('cf','modified')
coulumnValues = [coulumnValue1]
tdelete = TDelete(rowKey,coulumnValues)
print 'tdelete is ',tdelete
result = client.deleteSingle(tableName,tdelete)
print result
##delete-----end---------


注意:鉴于有的网友发现在Python3下出现运行错误,特此声明,本代码在Python2.x下测试通过。

代码下载地址:http://download.csdn.net/detail/lesorb/9003987

你可能感兴趣的:(python)