设为首页 收藏本站
查看: 3569|回复: 0

[经验分享] python 通过thrift 简单操作hbase

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2015-4-20 09:29:27 | 显示全部楼层 |阅读模式
  
  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 py hbase.thrift 会生成gen-py文件夹,将其修改成hbase
  安装python的thrift库
  sudo pip install thrift
  启动hbase的thrift服务:bin/hbase-daemon.sh start thrift 默认端口是9090
  创建hbase表:



1 from thrift import Thrift
2 from thrift.transport import TSocket
3 from thrift.transport import TTransport
4 from thrift.protocol import TBinaryProtocol
5
6 from hbase import Hbase
7 from hbase.ttypes import *
8
9 transport = TSocket.TSocket('localhost', 9090);
10
11 transport = TTransport.TBufferedTransport(transport)
12
13 protocol = TBinaryProtocol.TBinaryProtocol(transport);
14
15 client = Hbase.Client(protocol)
16 transport.open()
17
18
19 contents = ColumnDescriptor(name='cf:', maxVersions=1)
20 client.createTable('test', [contents])
21
22 print client.getTableNames()
  执行代码,成功后,进入hbase的shell,用命令list可以看到刚刚的test表已经创建成功。
DSC0000.png
  插入数据:



1 from thrift import Thrift
2 from thrift.transport import TSocket
3 from thrift.transport import TTransport
4 from thrift.protocol import TBinaryProtocol
5
6 from hbase import Hbase
7
8 from hbase.ttypes import *
9
10 transport = TSocket.TSocket('localhost', 9090)
11
12 transport = TTransport.TBufferedTransport(transport)
13
14 protocol = TBinaryProtocol.TBinaryProtocol(transport)
15
16 client = Hbase.Client(protocol)
17
18 transport.open()
19
20 row = 'row-key1'
21
22 mutations = [Mutation(column="cf:a", value="1")]
23 client.mutateRow('test', row, mutations, None)
  插入成功,通过scan命令查看插入结果:
DSC0001.png
  获取一行数据:
  



1 from thrift import Thrift
2 from thrift.transport import TSocket
3 from thrift.transport import TTransport
4 from thrift.protocol import TBinaryProtocol
5
6 from hbase import Hbase
7 from hbase.ttypes import *
8
9 transport = TSocket.TSocket('localhost', 9090)
10 transport = TTransport.TBufferedTransport(transport)
11
12 protocol = TBinaryProtocol.TBinaryProtocol(transport)
13
14 client = Hbase.Client(protocol)
15
16 transport.open()
17
18 tableName = 'test'
19 rowKey = 'row-key1'
20
21 result = client.getRow(tableName, rowKey, None)
22 print result
23 for r in result:
24     print 'the row is ' , r.row
25     print 'the values is ' , r.columns.get('cf:a').value
  
  getRow返回的是TResult列表,结果如下:
DSC0002.png
  返回多行则需要使用scan:



1 from thrift import Thrift
2 from thrift.transport import TSocket
3 from thrift.transport import TTransport
4 from thrift.protocol import TBinaryProtocol
5
6 from hbase import Hbase
7 from hbase.ttypes import *
8
9 transport = TSocket.TSocket('localhost', 9090)
10 transport = TTransport.TBufferedTransport(transport)
11
12 protocol = TBinaryProtocol.TBinaryProtocol(transport)
13
14 client = Hbase.Client(protocol)
15 transport.open()
16
17 scan = TScan()
18 tableName = 'test'
19 id = client.scannerOpenWithScan(tableName, scan, None)
20
21 result2 = client.scannerGetList(id, 10)
22
23 print result2
  scannerGetList会取10条数据,然后输出结果
DSC0003.png
  scannerGet则是每次只取一行数据:



1 from thrift import Thrift
2 from thrift.transport import TSocket
3 from thrift.transport import TTransport
4 from thrift.protocol import TBinaryProtocol
5
6 from hbase import Hbase
7 from hbase.ttypes import *
8
9 transport = TSocket.TSocket('localhost', 9090)
10 transport = TTransport.TBufferedTransport(transport)
11
12 protocol = TBinaryProtocol.TBinaryProtocol(transport)
13
14 client = Hbase.Client(protocol)
15 transport.open()
16
17 scan = TScan()
18 tableName = 'test'
19 id = client.scannerOpenWithScan(tableName, scan, None)
20 result = client.scannerGet(id)
21 while result:
22     print result
23     result = client.scannerGet(id)
  输出结果:
DSC0004.png

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-58665-1-1.html 上篇帖子: Python特殊语法:filter、map、reduce、lambda [转] 下篇帖子: 你可能不知道的python
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表