博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 通过thrift 简单操作hbase
阅读量:5020 次
发布时间:2019-06-12

本文共 3806 字,大约阅读时间需要 12 分钟。

 

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表已经创建成功。

插入数据:

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命令查看插入结果:

获取一行数据:

 

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 result23 for r in result:24     print 'the row is ' , r.row25     print 'the values is ' , r.columns.get('cf:a').value

 

getRow返回的是TResult列表,结果如下:

返回多行则需要使用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条数据,然后输出结果

 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 result23     result = client.scannerGet(id)

输出结果:

posted on
2013-01-21 21:35 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/hitandrew/archive/2013/01/21/2870419.html

你可能感兴趣的文章
2017.12.1T19_B2_5zuoye
查看>>
在编译器中调试spark程序处理
查看>>
css3 iphone开关 移动端开关、按钮、input
查看>>
信息论随笔
查看>>
VC2008下配置boost库使用正则表达式[转]
查看>>
.net使用Easyui+MVC进行图片上传的实现
查看>>
web_day2_重要标签
查看>>
Linux学习历程——Centos 7重置root密码
查看>>
ConfigurationManager.AppSettings Property
查看>>
vscode 插件 配置
查看>>
结对第一次—原型设计(文献摘要热词统计)
查看>>
iptables防火墙设置
查看>>
合并两个排序的链表
查看>>
[错误]集合已修改;可能无法执行枚举操作
查看>>
环境配置
查看>>
关于dom事件
查看>>
Qt 对文件的操作
查看>>
opengl中对glOrtho()函数的理解
查看>>
金山词霸 每日一句
查看>>
冒泡排序
查看>>