python通过thrift操作hbase

本文介绍如何在Mac上使用Homebrew安装Thrift并配置Python的Thrift库,进而连接到HBase服务器。通过具体步骤展示了如何启动Thrift服务、创建表以及进行基本的数据读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mac 版

首先

brew install thrift

然后安装python的thrift库

pip install thrift

再次进入 hbase安装目录

cd /usr/local/Cellar/hbase/1.2.2/libexec/bin


启动thrift服务(默认端口9090)

./hbase-daemon.sh start thrift

操作

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('localhost', 9090)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)
transport.open()

#创建test表,列为cf
contents = ColumnDescriptor(name='cf:', maxVersions=1)
client.createTable('test', [contents])
print client.getTableNames()

#插入数据
row = 'row-key1'
mutations = [Mutation(column="cf:a", value="1")]
client.mutateRow('test', row, mutations)

#获取一行数据
tableName = 'test'
rowKey = 'row-key1'
result = client.getRow(tableName, rowKey)
print result
for r in result:
    print 'the row is ' , r.row
    print 'the values is ' , r.columns.get('cf:a').value






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值