python 通过thrift服务连接Hbase

一直在找python连接Hbase的方法,方法之一是thrift 服务,Hbase还提供thrift2服务,这次我们使用thrift服务连接Hbase。

需要安装两个包:       

pip install  thrift
pip install hbase-thrift

HBase 开启thrift服务:  hbase-daemon.sh start thrift

连接Hbase:

from thrift.transport import TSocket, TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
ip='master'
port=9090
transport = TTransport.TBufferedTransport(TSocket.TSocket(ip, port))
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)
transport.open()
# transport.close()

插入操作:

from hbase.ttypes import ColumnDescriptor,Mutation, BatchMutation
data = {
        "col:addr": "2",
         "col:name": "cu",
    
 }
columns = map(lambda (k, v): Mutation(column=k,value=v), data.items())
client.mutateRow("job", "0011", columns)

扫描job表:

so = client.scannerOpen("job", "", None)
r= True
while r:
    r= client.scannerGet(so)
    # r =client.scannerGetList(so, 4)
    print r
client.scannerClose(so)

函数说明:

 mutateRow(table, row, mutaions)

功能: 向Hbase写入数据.

 1. table 是表名
 2. mutaions是一个 Mutation的 list [Mutation(column="col:name",value="wujq")]
 3. 如果muations里面有一个抛出异常,那边整个数据都无法写入
 4. 如果写入成功,那边对于么一列的timestamp是一样的

mutateRowTs(tableName, row, mutations, timestamp)

timestamp 是时间戳

mutateRows(tableName, rowBatches)

批量写入

rowBatches 是 BatchMutation list   eg:[BatchMutation("0007",[Mutation(column="col:name",value="wujq")]),
                                        BatchMutation("0008",[Mutation(column="col:name",value="wujq")])]

deleteAllRow(tableName, row)

删除一行

deleteAll(tableName, row, column)

删除某一列

get(tableName, row, column)

获取值

getRow(tableName, row)

获取row记录值

disableTable(tableName)

禁用表

deleteTable(tableName)

删除表

createTable(tableName, columnFamilies)

创建表

tableName : 表名

columnFamilies: 列簇

猜你喜欢

转载自blog.csdn.net/weixin_39594447/article/details/86697370