python 调用HBase的简单实例

时间:2021-05-22

新来的一个工程师不懂HBase,java不熟,python还行,我建议他那可以考虑用HBase的thrift调用,完成目前的工作。

首先,安装thrift

下载thrift,这里,我用的是thrift-0.7.0-dev.tar.gz 这个版本

tar xzf thrift-0.7.0-dev.tar.gzcd thrift-0.7.0-devsudo ./configure --with-cpp=no --with-ruby=nosudo makesudo make install

然后,到HBase的源码包里,找到

src/main/resources/org/apache/hadoop/hbase/thrift/

执行

thrift --gen py Hbase.thrift
mv gen-py/hbase/ /usr/lib/python2.4/site-packages/ (根据python版本可能有不同)

我这里写了些调用的脚本,供大家参考

from unittest import TestCase, mainfrom thrift import Thriftfrom thrift.transport import TSocketfrom thrift.transport import TTransportfrom thrift.protocol import TBinaryProtocolfrom hbase import Hbasefrom hbase.ttypes import ColumnDescriptor, Mutation, BatchMutationclass 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的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章