Python calls HBase simple instances, and python calls hbase

Source: Internet
Author: User

Python calls HBase simple instances, and python calls hbase

A new engineer is not familiar with HBase, java is not familiar, and python is okay. I suggest that he consider using thrift call of HBase to complete the current work.

First, install thrift

Download thrift. here, we use 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

Then, go to the HBase source code package and find

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

Run

Thrift -- gen py Hbase. thrift
Mv gen-py/hbase // usr/lib/python2.4/site-packages/(may vary with python versions)

I have written some calling scripts for your reference.

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()

The simple example of calling HBase in python above is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support the help house.

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.