thrift 是facebook开发并开源的一个二进制通讯中间件,通过thrift,我们可以充分利用各个语言的优势,编写高效的代码。
关于thrift的论文:http://pan.baidu.com/share/link?sharelocalhost’, 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 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)
获取一行数据:
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
返回多行则需要使用scan:
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
scannerget则是每次只取一行数据:
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)
result = client.scannerget(id)
while result:
print result
result = client.scannerget(id)