加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python 通过thrift操作hbase的完整代码

发布时间:2020-12-17 17:49:25 所属栏目:Python 来源:网络整理
导读:对python这个高级语言感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧! thrift 是facebook开发并开源的一个二进制通讯中间件,通过thrift,我们可以充分利用各个语言的优势,编写高效的代码。 关于thrift的论文:http://pan.baidu.com/sh
对python这个高级语言感兴趣的小伙伴,下面一起跟随编程之家 52php.cn的小编两巴掌来看看吧!

thrift 是facebook开发并开源的一个二进制通讯中间件,通过thrift,我们可以充分利用各个语言的优势,编写高效的代码。

关于thrift的论文:http://pan.baidu.com/share/link?shareid=234128&uk=3238841275

安装thrift:http://thrift.apache.org/docs/install/ubuntu/

安装完成后到hbase的目录下,找到Hbase.thrift,该文件在 hbase-0.94.4/src/main/resources/org/apache/hadoop/hbase/thrift下可以找到thrift --gen python hbase.thrift 会生成gen-py文件夹,将其修改成hbase

 

安装python的thrift库

sudo pip install thrift

启动hbase的thrift服务:bin/hbase-daemon.sh start thrift 默认端口是9090

创建hbase表:
 

# @param python通过thrift操作hbase实例
# @author 编程之家 52php.cn|52php.cn 

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('localhost',9090);
 
transport = TTransport.TBufferedTransport(transport)
 
protocol = TBinaryProtocol.TBinaryProtocol(transport);
 
client = Hbase.Client(protocol)
transport.open()
 
 
contents = ColumnDescriptor(name='cf:',maxVersions=1)
client.createTable('test',[contents])
 
print client.getTableNames()

# End www.52php.cn

执行代码,成功后,进入hbase的shell,用命令list可以看到刚刚的test表已经创建成功。

插入数据:

# @param python通过thrift操作hbase实例
# @author 编程之家 52php.cn|52php.cn 

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('localhost',9090)
 
transport = TTransport.TBufferedTransport(transport)
 
protocol = TBinaryProtocol.TBinaryProtocol(transport)
 
client = Hbase.Client(protocol)
 
transport.open()
 
row = 'row-key1'
 
mutations = [Mutation(column="cf:a",value="1")]
client.mutateRow('test',row,mutations,None)

# End www.52php.cn

获取一行数据:
 

# @param python通过thrift操作hbase实例
# @author 编程之家 52php.cn|52php.cn 

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('localhost',9090)
transport = TTransport.TBufferedTransport(transport)
 
protocol = TBinaryProtocol.TBinaryProtocol(transport)
 
client = Hbase.Client(protocol)
 
transport.open()
 
tableName = 'test'
rowKey = 'row-key1'
 
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('cf:a').value

# End www.52php.cn

返回多行则需要使用scan:
 

# @param python通过thrift操作hbase实例
# @author 编程之家 52php.cn|52php.cn 

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('localhost',9090)
transport = TTransport.TBufferedTransport(transport)
 
protocol = TBinaryProtocol.TBinaryProtocol(transport)
 
client = Hbase.Client(protocol)
transport.open()
 
scan = TScan()
tableName = 'test'
id = client.scannerOpenWithScan(tableName,scan,None)
 
result2 = client.scannerGetList(id,10)
 
print result2

# End www.52php.cn

scannerGet则是每次只取一行数据:

# @param python通过thrift操作hbase实例
# @author 编程之家 52php.cn|52php.cn 

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('localhost',None)
result = client.scannerGet(id)
while result:
    print result
    result = client.scannerGet(id)

# End www.52php.cn

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读