python2访问CDH HBase Thrift2

简介

HBase Thrift Server是hbase基于Apache Thrift开发用来提供对多语言支持的服务。同时保留了thrift和thrift2两个版本,thrift2适应了新的java api,cdh默认采用thrift。

启动HBase Thrift2

执行命令,启动thrift2

/opt/cloudera/parcels/CDH/lib/hbase/bin/hbase-daemon.sh --config /opt/cloudera/parcels/CDH/lib/hbase/conf foreground_start thrift2 --infoport 9096 -p 9091

python connect thrift2

1、pip install thrift==0.9.0

2、在github中找到hbase源码hbase-examples中的hbase模块,如下图:
《python2访问CDH HBase Thrift2》 image.png 在DemoClient.py中也介绍了如何生成hbase模块。这里可以直接复制这个项目中的hbase模块,将hbase模块放到$PATHHOME\Lib\site-packages或者放到需要访问thrift2的项目中。如图:
《python2访问CDH HBase Thrift2》 image.png

from thrift.transport import TSocket

from hbase import THBaseService
from hbase.ttypes import *

print "Thrift2 Demo"
print "This demo assumes you have a table called \"example\" with a column family called \"family1\""

host = "200.200.200.65"
port = 9091
framed = False

socket = TSocket.TSocket(host, port)
if framed:
    transport = TTransport.TFramedTransport(socket)
else:
    transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = THBaseService.Client(protocol)

transport.open()

table = "bigdata:tb_sleep_feedback_data"
scan = TScan()
result = client.openScanner(table, scan)
s = client.getScannerRows(result, 1)
print "Result:", s

transport.close()
    原文作者:凡尔Issac
    原文地址: https://www.jianshu.com/p/c12295afb502
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞