python操作Hbase
2024-10-19 17:33:49
本地操作
启动thrift服务:./bin/hbase-daemon.sh start thrift
hbase模块产生:
下载thrfit源码包:thrift-0.8.0.tar.gz
解压安装
./configure
make
make install
在thrift-0.8.0目录中,lib/py/build/lib.linux-x86_64-2.6/目录下存在thrift的python模块,拷贝出来即可
生成hbase模块
下载源码包:hbase-0.98.24-src.tar.gz
解压,进入下面目录:hbase-0.98.24/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift
thrift --gen py Hbase.thrift
创建表格
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('master' , 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol)
transport.open()
base_info_contents = ColumnDescriptor(name='meta-data',maxVersions=1)
other_info_contents = ColumnDescriptor(name='flags',maxVersions=1)
client.createTable('new_music_table', [base_info_contents, other_info_contents])
print client.getTableNames()写数据
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('master' , 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol)
transport.open()
tableName = 'new_music_table'
rowKey = '1100'
mutations = [Mutation(column="meta-data:name" , value="wangqingshui"),\
Mutation(column="meta-data:tag" , value="pop"),\
Mutation(column="meta-data:is_valid" , value="TRUE")]
client.mutateRow(tableName,rowKey,mutations,None)读数据
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('master' , 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol)
transport.open()
tableName = 'new_music_table'
rowKey = '1100'
result = client.getRow(tableName,rowKey,None)
for r in result:
print 'the row is ',r.row
print 'the name is ',r.columns.get('meta-data:name').value
print 'the name is ',r.columns.get('meta-data:is_valid').value读多条数据
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('master' , 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol)
transport.open()
tableName = 'new_music_table'
scan = TScan()
id = client.scannerOpenWithScan(tableName , scan , None)
result = client.scannerGetList(id,10)
for r in result:
print '==============='
print 'the row is ' , r.row
for k,v in r.columns.items():
print "\t".join([k,v.value])
# 执行结果
===============
the row is 1100
meta-data:name wangqingshui
meta-data:is_valid TRUE
meta-data:tag pop
===============
the row is 2200
meta-data:name langdechuanshuo
最新文章
- Codeforces 719E [斐波那契区间操作][矩阵快速幂][线段树区间更新]
- OA系统部门结构树
- bzoj1532
- boost::thread boost库线程
- canvas图形处理和进阶用法
- Web前端学习——HTML
- rs(0)与rs(";字段名";)的区别
- scrapy_redis实现爬虫
- SVM问题汇总
- E3Upload项目总结
- Tween animation
- 使用RemObjects Pascal Script
- dhtmlx uploader使用
- android view知识点 总结
- hibernate_boolean类型的处理
- C\C++与Java中的static关键字
- Spring Chapter4 WebSocket 胡乱翻译 (二)
- Codeforces Round #534 (Div. 2) D. Game with modulo(取余性质+二分)
- Java MVC 增删改查 实例
- c++函数进阶