python3.7代码示例_python3.7通过thrift操作hbase的示例代码

本文介绍了如何在Python3.7环境中使用thrift接口操作HBase,包括创建、删除表,增删记录以及扫描数据。由于hbase-thrift-0.20.4不支持Python3.x,文章提供了patch版本的安装和使用方法。
摘要由CSDN通过智能技术生成

HBase是一个分布式的、面向列的开源数据库,其是Apache的Hadoop项目的子项目。HBase不同于一般的关系数据库,它是一个适合于非结构化数据存储的数据库。另一个不同的是HBase基于列的而不是基于行的模式。其数据结构类似与Redis的key-value模式。

f5699174783ee04c6a5f1afb8c713539.png

python3.7 通过 thrift , rpc 接口操作 hbase ,指定依赖库为: thrift 和 hbase-thrift 。 然而我们 在 python3.7 环境中发现 hbase-thrift-0.20.4 无法被支持, hbase-thrift 官方仅推荐用于 python2.x 。 于是有了下边的 patch 版本 和 patch 版本写法的客户端。

patch 版本下载,适用于 python 3.x : http://dl.cpp.la/Archive/hbase-thrift-0.20.4.patch.tgz

卸载 hbase-thrift-0.20.4 版本

# pip3 list | grep hbase-thrift

>> hbase-thrift 0.20.4

# pip3 uninstall hbase-thrift -y

>> Successfully uninstalled hbase-thrift-0.20.4

安装 hbase-thrift-0.20.4.patch 版本(支持 python3.x )

wget http://dl.cpp.la/Archive/hbase-thrift-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Python中使用Thrift连接HBase,您需要按照以下步骤进行设置: 1. 安装所需的依赖项: 您需要安装`thrift`和`happybase`这两个Python库。可以使用以下命令进行安装: ```bash pip install thrift happybase ``` 2. 生成HBaseThrift代码: 使用Thrift工具生成HBaseThrift代码。您可以使用以下命令: ```bash thrift -r --gen py hbase.thrift ``` 这将生成Python的Thrift代码文件。 3. 创建HBase连接: 在Python脚本中,您需要首先创建一个HBase连接。示例代码如下: ```python import happybase connection = happybase.Connection(host='localhost', port=9090) ``` 4. 执行HBase操作: 在创建了HBase连接之后,您可以使用`connection`对象执行各种HBase操作,例如创建表、插入数据、获取数据等。以下是一些示例代码: - 创建表: ```python connection.create_table( 'mytable', { 'cf': dict(max_versions=10), } ) ``` - 插入数据: ```python table = connection.table('mytable') table.put( b'row1', { b'cf:col1': b'value1', b'cf:col2': b'value2', } ) ``` - 获取数据: ```python table = connection.table('mytable') row = table.row(b'row1') print(row) ``` - 删除数据: ```python table = connection.table('mytable') table.delete(b'row1') ``` 这只是一些示例代码,您可以根据需要使用其他HappyBase方法来执行更多操作。 5. 关闭连接: 当您完成HBase操作后,记得关闭连接以释放资源: ```python connection.close() ``` 请注意,为了成功执行这些操作,您需要确保HBase正在运行并且在指定的主机和端口上进行监听。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值