python 通过thrift操作hbase实例

升级所有软件包

yum -y update

tar -zxvf thrift-0.13.0.tar.gz

然后进入到thrift目录,

在编译之前要先安装:

yum install libtool flex bison pkgconfig boost-devel libevent-devel zlib-devel python-devel ruby-devel openssl-devel ant

然后进入到thrift目录,执行./bootstrap.sh

然后执行./configure,这是默认的,也可以支持选择,如下命令:

./configure --with-cpp --with-boost=/usr/local/ --with-python --without-csharp --with-java --without-erlang --without-perl --without-php --without-php_extension --without-ruby --without-haskell --without-go

然后执行make,如出现错误,执行autoreconf -ivf,再次执行make。

make install

thrift --version来查看版本号

打开终端输入:

pip install thrift

pip install hbase-thrift

替换文件:/usr/local/lib/python3.7/site-packages/hbase/Hbase.pyttypes.py

https://github.com/626626cdllp/infrastructure/tree/master/hbase

系统中文件替换位置:

/root/anaconda3/lib/python3.8/site-packages/hbase

启动终端,打开thrift:hbase thrift start

程序1

from thrift.transport import TSocket

from thrift.protocol import TBinaryProtocol

from hbase import Hbase

transport = TSocket.TSocket('192.168.126.140',9090)

protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)

transport.open()

result=client.getTableNames()

print (result)

程序2

from thrift.transport import TSocket

from thrift.protocol import TBinaryProtocol

from thrift.transport import TTransport

from hbase import Hbase

from hbase.ttypes import *

transport = TSocket.TSocket('172.16.3.68',9090)

transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)

transport.open()

contents = ColumnDescriptor(name='cf:', maxVersions=1)

client.createTable('test2', [contents])

result=client.getTableNames()

print (result)

程序3

from thrift.transport import TSocket,TTransport

from thrift.protocol import TBinaryProtocol

from hbase import Hbase

# thrift默认端口是9090

socket = TSocket.TSocket('172.16.3.68',9090)

socket.setTimeout(5000)

transport = TTransport.TBufferedTransport(socket)

protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)

socket.open()

print client.getTableNames()

print client.get('Student1','001','StuInfo:Name')

程序4:插入数据:

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 *

transport = TSocket.TSocket('172.16.3.68', 9090)

transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)

transport.open()

row = 'row-key1'

mutations = [Mutation(column='stuinfo:a', value='1')]

client.mutateRow('Student2',row,mutations)

程序5:获取一行数据:

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 *

transport = TSocket.TSocket('172.16.3.68', 9090)

transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)

transport.open()

tableName='Student1'

rowKey='001'

result = client.getRow(tableName, rowKey)

print (result)

for r in result:

    print ('the row is ' , r.row)

    print ('the values is ' , r.columns.get('StuInfo:Name').value)

程序6

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 *

transport = TSocket.TSocket('172.16.3.68', 9090)

transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)

transport.open()

scannerId = client.scannerOpen('Student1','001',["StuInfo:Name","Grades:Math"])

result=client.scannerGet(scannerId)

print (result)

程序7:返回多行:

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 *

transport = TSocket.TSocket('172.16.3.68', 9090)

transport = TTransport.TBufferedTransport(transport)

protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)

transport.open()

scannerId = client.scannerOpen('Student1','001',["StuInfo:Name","Grades:Math"])

result=client.scannerGetList(scannerId,10) #拿10行数据

print (result)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值