python操作hbase_通过Python操作hbase api

#coding=utf-8#Author: ruin

"""discrible:"""

from thrift.transport importTSocketfrom thrift.protocol importTBinaryProtocolfrom thrift.transport importTTransportfrom hbase importHbaseimportstruct#Method for encoding ints with Thrift's string encoding

defencode(n):return struct.pack("i", n)#Method for decoding ints with Thrift's string encoding

defdecode(s):return int(s) if s.isdigit() else struct.unpack('i', s)[0]classHBaseApi(object):def __init__(self,table='fr_test_hbase:test_api',host='10.2.46.240',port=9090):

self.table= table.encode('utf-8')

self.host=host

self.port=port#Connect to HBase Thrift server

self.transport =TTransport.TBufferedTransport(TSocket.TSocket(host, port))

self.protocol=TBinaryProtocol.TBinaryProtocolAccelerated(self.transport)#Create and open the client connection

self.client =Hbase.Client(self.protocol)

self.transport.open()#set type and field of column families

self.set_column_families([bytes],['info'])

self._build_column_families()def set_column_families(self,type_list,col_list=['info']):

self.columnFamiliesType=type_list

self.columnFamilies=col_listdef_build_column_families(self):"""give all column families name list,create a table

:return:"""tables=self.client.getTableNames()if self.table not intables:

self.__create_table(self.table)def __create_table(self,table):"""create table in hbase with column families

:param table: fr_test_hbase:fr_test

:return:"""columnFamilies=[]for columnFamily inself.columnFamilies:

name= Hbase.ColumnDescriptor(name =columnFamily)

columnFamilies.append(name)

table= table.encode('utf-8')print(type(table),type(columnFamilies))

self.client.createTable(table,columnFamilies)def __del__(self):

self.transport.close()def __del_table(self,table):"""delete a table,first need to disable it"""self.client.disableTable(table)

self.client.deleteTable(table)defgetColumnDescriptors(self):returnself.client.getColumnDescriptors(self.table)defput(self, rowKey, qualifier, value):"""put one row

column is column name,value is column value

:param rowKey: rowKey

:param column: column name

:param value: column value

:description: HbaseApi(table).put('rowKey','column','value')"""rowKey= rowKey.encode('utf-8')

mutations=[]#for j, column in enumerate(column):

ifisinstance(value, str):

value= value.encode('utf-8')

m_name= Hbase.Mutation(column=(self.columnFamilies[0]+':'+qualifier).encode('utf-8'), value=value)elifisinstance(value, int):

m_name= Hbase.Mutation(column=(self.columnFamilies[0]+':'+qualifier).encode('utf-8'), value=encode(value))

mutations.append(m_name)

self.client.mutateRow(self.table, rowKey, mutations, {})defputs(self,rowKeys,qualifier,values):"""put sevel rows, `qualifier` is autoincrement

:param rowKeys: a single rowKey

:param values: values is a 2-dimension list, one piece element is [name, sex, age]

:param qualifier: column family qualifier

Usage::

>>> HBaseTest('table').puts(rowKeys=[1,2,3],qualifier="name",values=[1,2,3])"""mutationsBatch=[]if notisinstance(rowKeys,list):

rowKeys= [rowKeys] *len(values)for i, value inenumerate(values):

mutations=[]#for j, column in enumerate(value):

ifisinstance(value, str):

value= value.encode('utf-8')

m_name= Hbase.Mutation(column=(self.columnFamilies[0]+':'+qualifier).encode('utf-8'), value=value)elifisinstance(value, int):

m_name= Hbase.Mutation(column=(self.columnFamilies[0]+':'+qualifier).encode('utf-8'), value=encode(value))

mutations.append(m_name)

mutationsBatch.append(Hbase.BatchMutation(row= rowKeys[i].encode('utf-8'),mutations=mutations))

self.client.mutateRows(self.table, mutationsBatch, {})def getRow(self,row, qualifier='name'):"""get one row from hbase table

:param row:

:param qualifier:

:return:"""

#res = []

row = self.client.getRow(self.table, row.encode('utf-8'),{})for r inrow:

rd={}

row= r.row.decode('utf-8')

value= (r.columns[b'info:name'].value).decode('utf-8')

rd[row]=value#res.append(rd)

#print ('the row is ',r.row.decode('utf-8'))

#print ('the value is ',(r.columns[b'info:name'].value).decode('utf-8'))

returnrddef getRows(self, rows, qualifier='name'):"""get rows from hbase,all the row sqecify the same 'qualifier'

:param rows: a list of row key

:param qualifier: column

:return: None"""

#grow = True if len(rows) == 1 else False

res =[]for r inrows:

res.append(self.getRow(r,qualifier))returnresdef scanner(self, numRows=100, startRow=None, stopRow=None):""":param numRows:

:param startRow:

:param stopRow:

:return:"""scan=Hbase.TScan(startRow, stopRow)

scannerId=self.client.scannerOpenWithScan(self.table,scan, {})

ret=[]

rowList=self.client.scannerGetList(scannerId, numRows)for r inrowList:

rd={}

row= r.row.decode('utf-8')

value= (r.columns[b'info:name'].value).decode('utf-8')

rd[row]=value#print ('the row is ',r.row.decode('utf-8'))

#print ('the value is ',(r.columns[b'info:name'].value).decode('utf-8'))

ret.append(rd)returnretdefdemo():

ha= HBaseApi('fr_test_hbase:test_log1')#ha.put('0002','age','23')

rowKeys = [str(key) for key in range(10001,10010)]

values= ['fr'+str(val) for val in range(10001,10010)]

ha.puts(rowKeys,'name',values)print(ha.scanner())#print(ha.getRow('0001'))

#print(ha.getRows(rowKeys))

if __name__ == "__main__":

demo()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值