安裝Thrift的具體操做,請點擊連接java
pip install thrift pip install hbase-thrift
from thrift.transport import TSocket,TTransport from thrift.protocol import TBinaryProtocol from hbase import Hbase # thrift默認端口是9090 socket = TSocket.TSocket('192.168.0.156',9090) socket.setTimeout(5000) transport = TTransport.TBufferedTransport(socket) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = Hbase.Client(protocol) socket.open() print client.getTableNames() print client.get('test','row1','cf:a')
from hbase.ttypes import ColumnDescriptor # 定義列族 column = ColumnDescriptor(name='cf') # 建立表 client.createTable('test4',[column])
# 啓用表,若表以前未被禁用將會引起IOError錯誤 client.enabledTable('test')
# 禁用表,若表以前未被啓用將會引起IOError錯誤 client.disableTable('test')
client.isTableEnabled('test')
client.getTableNames()
client.getColumnDescriptors('test')
TRegionInfo
對象列表
client.getTableRegions('test')
# 表不存在將會引起IOError(message='java.io.IOException: table does not exist...)錯誤 # 表未被禁用將會引起IOError(message='org.apache.hadoop.hbase.TableNotDisabledException:...)錯誤 client.deleteTable('test5')
hbase.ttypes.TCell
對象列表
result = client.get('test','row1','cf:a') # 爲一個列表,其中只有一個hbase.ttypes.TCell對象的數據 print result[0].timestamp print result[0].value
hbase.ttypes.TCell
對象列表
result = client.get('test','row1','cf:a',2) # 爲一個列表,其中只有一個hbase.ttypes.TCell對象的數據 print result[0].timestamp print result[0].value
hbase.ttypes.TCell
對象列表
result = client.get('test','row1','cf:a',2) # 爲一個列表,其中只有一個hbase.ttypes.TCell對象的數據 print result[0].timestamp print result[0].value
hbase.ttypes.TRowResult
對象列表,若是行號不存在返回一個空列表
# 行 row = 'row1' # 列 column = 'cf:a' # 查詢結果 result = client.getRow('test',row) # result爲一個列表 for item in result: # item爲hbase.ttypes.TRowResult對象 print item.row print item.columns.get('cf:a').value # 獲取值。item.columns.get('cf:a')爲一個hbase.ttypes.TCell對象 print item.columns.get('cf:a').timestamp # 獲取時間戳。item.columns.get('cf:a')爲一個hbase.ttypes.TCell對象
hbase.ttypes.TRowResult
對象列表,若是行號不存在返回一個空列表
result = client.getRowWithColumns('test','row1',['cf:a','df:a']) for item in result: print item.row print item.columns.get('cf:a').value print item.columns.get('cf:a').timestamp print item.columns.get('df:a').value print item.columns.get('df:a').timestamp
hbase.ttypes.TRowResult
對象列表,若是行號不存在返回一個空列表
result = client.getRowTs('test','row1',1513069831512)
hbase.ttypes.TRowResult
對象列表,若是行號不存在返回一個空列表
result = client.getRowWithColumnsTs('test','row1',['cf:a','cf:b','df:a'],1513069831512)
from hbase.ttypes import Mutation mutation = Mutation(name='cf:a',value='1') # 插入數據。若是在test表中row行cf:a列存在,將覆蓋 client.mutateRow('test','row1',[mutation])
from hbase.ttypes import Mutation # value必須爲字符串格式,不然將報錯 mutation = Mutation(column='cf:a',value='2') client.mutateRowTs('test','row1',[mutation],1513070735669)
from hbase.ttypes import Mutation,BatchMutation mutation = Mutation(column='cf:a',value='2') batchMutation = BatchMutation('row1',[mutation]) client.mutateRows('test',[batchMutation])
mutation = Mutation(column='cf:a',value='2') batchMutation = BatchMutation('row1',[mutation]) client.mutateRowsTs('cx',[batchMutation],timestamp=1513135651874)
result = client.atomicIncrement('cx','row1','cf:b',1) print result # 若是以前的值爲2,此時值爲3
client.deleteAll('cx','row1','cf:a')
client.deleteAllTs('cx','row1','cf:a',timestamp=1513569725685)
client.deleteAllRow('cx','row1')
client.deleteAllRowTs('cx','row1',timestamp=1513568619326)
scannerId = client.scannerOpen('cx','row2',["cf:b","cf:c"])
scannerId = client.scannerOpenTs('cx','row1',["cf:a","cf:b","cf:c"],timestamp=1513579065365)
scannerId = client.scannerOpenWithStop('cx','row1','row2',["cf:b","cf:c"])
scannerId = client.scannerOpenWithStopTs('cx','row1','row2',["cf:a","cf:b","cf:c"],timestamp=1513579065365)
scannerId = client.scannerOpenWithPrefix('cx','row',["cf:b","cf:c"])
hbase.ttypes.TRowResult
對象列表
scannerId = client.scannerOpen('cx','row1',["cf:b","cf:c"]) while True: result = client.scannerGet(scannerId) if not result: break print result
hbase.ttypes.TRowResult
對象列表
scannerId = client.scannerOpen('cx','row1',["cf:b","cf:c"]) result = client.scannerGetList(scannerId,2)
client.scannerClose(scannerId)