A detailed example of Python invoking HBase

Source: Internet
Author: User

A new engineer does not know Hbase,java, Python is OK, I suggest that he can consider using HBase thrift call to complete the current work.

First, install Thrift

Download thrift, here, I'm using thrift-0.7.0-dev.tar.gz this version

Tar xzf thrift-0.7.0-dev.tar.gzcd thrift-0.7.0-devsudo./configure--with-cpp=no--with-ruby=nosudo Makesudo make Install


Then, in the source bundle of HBase, find

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

Perform

Thrift--gen PY Hbase.thrift

MV gen-py/hbase//usr/lib/python2.4/site-packages/(may differ depending on Python version)

I've written some of the scripts that we call here for your reference.

From unittest import TestCase, mainfrom Thrift import thriftfrom thrift.transport import tsocketfrom thrift.transport impo RT 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 = TBinaryPro Tocol. Tbinaryprotocol (self.transport) self.client = hbase.client (self.protocol) self.transport.open () tables = Self.cli Ent.gettablenames () If Self.tablename not in Tables:self.__createtable () def __del__ (self): self.transport.cl OSE () def __createtable (self): name = Columndescriptor (name= ' name ') foo = columndescriptor (name= ' foo ') Self.clie Nt.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,col     UMN): 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 te Ardown (self): name = Self.writer.tableName client = self.writer.client client.disabletable (name) client.delete Table (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 (tabl ENAME): 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 () 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.