Python 使用Thrift 接口 操作hbase
thrift是一个跨语言服务的软件开发框架(Thrift is a software framework for scalable cross-language services development.)。
下载 thrift
官网 : http://incubator.apache.org/thrift/
安装:
tar zxf thrift-0.9.2.tar.gz
cd thrift-0.9.2/
./configure && make && make install
安装python-thrift 插件
pip install thrift
生成hbase的client代码
cd $HBASE_HOME/src/main/resources/org/apache/hadoop/hbase/thrift/
thrift --gen py Hbase.thrift
参考:
然后将生成的gen-py文件夹下的hbase文件夹拷贝到 /usr/lib/python2.7/dist-packages/
准备hbase
首先确认hbase正常工作,然后启动hbase的thrift服务:
$HBASE_HOME//bin/hbase-deamon.sh start thrift
验证:
root@hrsjw1-izp:/usr/local/hbase/bin# netstat -tanp | grep 9090
tcp 0 0 127.0.0.1:47605 127.0.0.1:9090 TIME_WAIT -
tcp6 0 0 :::9090 :::* LISTEN 21313/java
OK,准备工作到此为止,我们开始编写python客户程序。
vim hb_test.py
#!/usr/bin/env python
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 *
thrift_server = '127.0.0.1'
Port = 9090
transport = TTransport.TBufferedTransport(
TSocket.TSocket(thrift_server, Port))
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)
transport.open()
contents = ColumnDescriptor(name='cf:', maxVersions=1)
client.createTable('test', [contents])
print client.getTableNames()
hrsjw1@hrsjw1-izp:~/hadoop_jobs/hbase_job$ python hb_test.py
['member', 'test']
hbase shell :
hbase(main):009:0> list
TABLE
member
test
2 row(s) in 0.0370 seconds
阅读(1986) | 评论(0) | 转发(0) |