首頁 >後端開發 >Python教學 >詳解python呼叫HBase的實例

詳解python呼叫HBase的實例

高洛峰
高洛峰原創
2017-03-23 17:37:321545瀏覽

新來的一個工程師不懂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.thrift

mv 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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn