首页 > 代码库 > Hbase Python接口

Hbase Python接口

http://wiki.apache.org/hadoop/Hbase/ThriftApi

首先根据Hbase.thrift文件生成Python接口代码,Hbase.thrift在Hbase的源码中有,路径src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift

thrift --gen py Hbase.thrift 

生成的接口文件在gen-py目录下,咱直接引用即可。

from thrift.transport.TSocket import TSocket
from thrift.transport.TTransport import TBufferedTransport
from thrift.protocol import TBinaryProtocol
import sys

sys.path.append(gen-py)
from hbase import Hbase

host = 192.168.6.1
port = 8080
transport = TBufferedTransport(TSocket(host, port))
transport.open()
protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)

#取数据
results = client.getRow(Item, ‘rowkey, {})
if len(results) > 0:
    result = results[0]
    for k,v in result.columns.iteritems():
        print k, v.value

# 关闭
transport.close()