mac 版
首先
brew install thrift
然后安装python的thrift库
pip install thrift
再次进入 hbase安装目录
cd /usr/local/Cellar/hbase/1.2.2/libexec/bin
./hbase-daemon.sh start 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('localhost', 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)
transport.open()
#创建test表,列为cf
contents = ColumnDescriptor(name='cf:', maxVersions=1)
client.createTable('test', [contents])
print client.getTableNames()
#插入数据
row = 'row-key1'
mutations = [Mutation(column="cf:a", value="1")]
client.mutateRow('test', row, mutations)
#获取一行数据
tableName = 'test'
rowKey = 'row-key1'
result = client.getRow(tableName, rowKey)
print result
for r in result:
print 'the row is ' , r.row
print 'the values is ' , r.columns.get('cf:a').value