安装Thrift的具体操作,请点击链接
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)