详解python调用hbase的实例

新来的一个工程师不懂hbase,java不熟,python还行,我建议他那可以考虑用hbase的thrift调用,完成目前的工作。首先,安装thrift下载thrift,这里,我用的是thrift-0.7.0-dev.tar.gz 这个版本

tar xzf thrift-0.7.0-dev.tar.gz
cd thrift-0.7.0-dev
sudo ./configure –with-cpp=no –with-ruby=no
sudo make
sudo make install

然后,到hbase的源码包里,找到src/main/resources/org/apache/hadoop/hbase/thrift/执行thrift –gen py hbase.thriftmv gen-py/hbase/ /usr/lib/python2.4/site-packages/ (根据python版本可能有不同)我这里写了些调用的脚本,供大家参考

from unittest import testcase, main
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 columndescriptor, mutation, batchmutation
class hbasetester:
def __init__(self, netloc, port, table=”staftesttable”):
self.tablename = table
self.transport = ttransport.tbufferedtransport(
tsocket.tsocket(netloc, port))
self.protocol = tbinaryprotocol.tbinaryprotocol(self.transport)
self.client = hbase.client(self.protocol)
self.transport.open()
tables = self.client.gettablenames()
if self.tablename not in tables:
self.__createtable()
def __del__(self):
self.transport.close()
def __createtable(self):
name = columndescriptor(name=’name’)
foo = columndescriptor(name=’foo’)
self.client.createtable(self.tablename,
[name,foo])
def put(self,key,name,foo):
name = mutation(column=”name:v”, value=name)
foo = mutation(column=”foo:v”,value=foo)
self.client.mutaterow(self.tablename,key,[name,foo])
def scanner(self,column):
scanner = client.scanneropen(self.tablename,””,[column])
r = client.scannerget(scanner)
result= []
while r:
print r[0]
result.append(r[0])
r = client.scannerget(scanner)
print “scanner finished”
return result
class testhbasetester(testcase):
def setup(self):
self.writer = hbasetester(“localhost”, 9090)
def teardown(self):
name = self.writer.tablename
client = self.writer.client
client.disabletable(name)
client.deletetable(name)
def testcreate(self):
tablename = self.writer.tablename
client = self.writer.client
self.asserttrue(self.writer.tablename in client.gettablenames())
columns =[‘name:’,’foo:’]
for i in client.getcolumndescriptors(tablename):
self.asserttrue(i in columns)
def testput(self):
self.writer.put(“r1″,”n1″,”f1”)
self.writer.put(“r2″,”n2″,”f2”)
self.writer.put(“r3″,”n3″,””)
self.writer.scanner(“name:”)
if __name__ == “__main__”:
main()

以上就是详解python调用hbase的实例 的详细内容,更多请关注 第一php社区 其它相关文章!

Posted in 未分类

发表评论