• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

python通过thrift操作hbase实例

python 搞代码 4年前 (2022-01-09) 29次浏览 已收录 0个评论

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表:

from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocol  from hbase import Hbasefrom 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()

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

插入数据:

from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom 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)

获取一行数据:

from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocol  from hbase import Hbasefrom 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 resultfor r in<strong style="color:transparent">来2源gaodaima#com搞(代@码&网</strong> result:    print 'the row is ' , r.row    print 'the values is ' , r.columns.get('cf:a').value

返回多行则需要使用scan:

from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocol  from hbase import Hbasefrom 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

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

from thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocol  from hbase import Hbasefrom 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)result = client.scannerGet(id)while result:    print result    result = client.scannerGet(id)

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python通过thrift操作hbase实例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址