zl程序教程

您现在的位置是:首页 >  后端

当前栏目

​使用Python3操作HBase的两种方法

Python3HBase方法 使用 操作 两种
2023-06-13 09:16:47 时间

使用Python3操作HBase的两种方法

  • 0. 写在前面
  • 1. 安装conda
  • 2. 安装hbase-thrift-0.20.0.patch
    • 新建一个Python3.9的anaconda环境
    • 激活新建的anaconda环境test
    • 检查是否已经存在hbase-thrift环境
    • 下载hbase-thrift-0.20.0.patch
    • 3. python连接hbase测试
  • 4. 第二种方法
    • 下载并安装
    • 安装Thrift依赖的库
    • 编译安装Thrift
  • 5. 参考


0. 写在前面

  • Linux: Ubuntu Kylin16.04
  • Python: Anaconda环境下的Python3.9
  • HBase: HBase1.1.5
  • hbase-thrift: hbase-thrift-0.20.0.patch
  • Thrift:Thrift0.16.0

❝Python3 通过 thriftrpc 接口操作 HBase ,指定依赖库为:thrifthbase-thrift 。然而我们 在 Python3 环境中发现 hbase-thrift-0.20.4 无法被支持, hbase-thrift 官方仅推荐用于 python2.x 。所以需要使用下边的 patch 版本 和 patch 版本写法的客户端「第一种Python调用HBase的方法」。 ❞

1. 安装conda

「Ubuntu安装Anaconda及注意事项」

2. 安装hbase-thrift-0.20.0.patch

新建一个Python3.9的anaconda环境

root@node01:~$ conda create -n test python=3.9

激活新建的anaconda环境test

root@node01:~$ conda activate test

查是否已经存在hbase-thrift环境

(test) root@node01:~$ pip3 list | grep hbase-thrift
  • 若存在hbase-thrift环境,直接删除「不存在也建议执行下面命令语句」
(test) root@node01:~$ pip3 uninstall -y hbase-thrift

下载hbase-thrift-0.20.0.patch

  • 下载
(test) root@node01:/usr/local$ wget http://dl.cpp.la/Archive/hbase-thrift-0.20.4.patch.tgz

❝报错 ❞

--2022-09-17 20:06:13--  http://dl.cpp.la/Archive/hbase-thrift-0.20.4.patch.tgz
正在解析主机 dl.cpp.la (dl.cpp.la)... 3.0.3.0
正在连接 dl.cpp.la (dl.cpp.la)|3.0.3.0|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 502 Bad Gateway
2022-09-17 20:06:14 错误 502:Bad Gateway。

❝错误 502:Bad Gateway。 ❞

尝试以下方法下载

(test) root@node01:/usr/local$ wget --no-cookies http://dl.cpp.la/Archive/hbase-thrift-0.20.4.patch.tgz

❝该方法不可行 ❞

❝建议在Windows下载后上传到Linux上 ❞

  • 正式安装
(test) root@node01:/usr/local/$ cd hbase-thrift-0.20.4.patch
(test) root@node01:/usr/local/hbase-thrift-0.20.4.patch$ python3 setup.py install

❝报错 ❞

Traceback (most recent call last):
  File "setup.py", line 2, in <module>
    from setuptools import setup, find_packages
ImportError: No module named 'setuptools'

❝按照提示,下载setuptools

(test) root@node01:/usr/local$ apt-get install python3-setuptools
  • 再次执行安装命令
(test) root@node01:/usr/local/hbase-thrift-0.20.4.patch$ sudo python3 setup.py install
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa2 in position 37: invalid start byte in /usr/local/hbase-thrift-0.20.4.patch/._hbase_thrift.egg-info

❝各种报错接连不断,人都麻了 ❞

❝删除并卸载hbase-thrift相关内容 ❞

  • 重新下载并安装

看到这些字样就代表成功了

Installed /usr/local/anaconda/envs/test/lib/python3.9/site-packages/six-1.16.0-py3.9.egg
Finished processing dependencies for hbase-thrift===0.20.4.patch
  • 再次确认一下
(test) root@node01:/usr/local/hbase-thrift-patch/hbase-thrift-0.20.4.patch$ pip3 list | grep hbase-thrift
hbase-thrift 0.20.4.patch

3. python连接hbase测试

from thrift import Thrift
from thrift.transport import TSocket,TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase

# thrift默认端口是9090
socket = TSocket.TSocket('127.0.0.1',9090)
socket.setTimeout(5000)

transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)

client = Hbase.Client(protocol)
socket.open()

from hbase.ttypes import ColumnDescriptor,Mutation,BatchMutation,TRegionInfo
from hbase.ttypes import IOErroe,AlreadyExists

print(client.getTableNames())  # 获取当前所有的表名

4. 第二种方法

❝使用Python调用HBase需要启动Thrift服务,但由于Linux本身没有内置该安装包,需要手动下载并安装 ❞

下载并安装

# 下载
(test) root@node01:/usr/local/$ wget https://mirrors.aliyun.com/apache/thrift/0.16.0/thrift-0.16.0.tar.gz?spm=a2c6h.25603864.0.0.654e1a4a7lakI8
# 重命名
(test) root@node01:/usr/local/$ mv thrift-0.16.0.tar.gz?spm=a2c6h.25603864.0.0.654e1a4a7lakI8 thrift-0.16.0.tar.gz
# 解压
(test) root@node01:/usr/local/$ tar -zxvf thrift-0.16.0.tar.gz -C ./

安装Thrift依赖的库

❝在编译安装Thrift之前,需要提前安装Thrift依赖的库,如 Automake、LibTool、Bison、Boost等,请参考「Thrift官网安装手册」 ❞

❝https://thrift.apache.org/docs/install/ ❞

  • 基本要求「Basic requirements」
A relatively POSIX-compliant *NIX system
Cygwin or MinGW can be used on Windows (but there are better options, see below)
g++ 4.2 (4.8 or later required for thrift compiler plug-in support)
boost 1.53.0
Runtime libraries for lex and yacc might be needed for the compiler.

g++ 4.2boost 1.53.0、编译可能需要的运行时库 lexyacc

  • 从源代码构建的要求「Requirements for building from source」
GNU build tools:
autoconf 2.65
automake 1.13
libtool 1.5.24
pkg-config autoconf macros (pkg.m4)
lex and yacc (developed primarily with flex and bison)
libssl-dev
  • 依赖的库
apt-get install automake bison flex g++ git libboost1.55 libevent-dev libssl-dev libtool make pkg-config

编译安装Thrift

  • 编译并安装Thrift
## 进入Thrift安装目录
(test) root@node01:/usr/local/thrift-0.16.0$ ./configure
(test) root@node01:/usr/local/thrift-0.16.0$ make
(test) root@node01:/usr/local/thrift-0.16.0$ make install
  • 调用thrift命令验证安装是否成功
(test) root@node01:/usr/local/thrift-0.16.0$ thrift --version
Thrift version 0.16.0

❝HBase的源代码中,hbase.thrift文件描述了HBase服务API和有关对象的IDl文件,需要使用thrift命令对此文件进行编译,生成Python链接HBase的库包。Hbase.thrift 文件在HBase安装目录中的 hbase-thrift目录下 ❞

  • 编译操作如下:
# 创建目录
root@node01:/usr/local/thrift-0.16.0$ mkdir pythonhbase
# 进入pythonhbase目录下
root@node01:/usr/local/thrift-0.16.0$ cd pythonhbase
# 执行thrift命令生成Hbase的Python库
root@node01:/usr/local/thrift-0.16.0$ thrift --gen py ../hbase-1.15/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift

注意:py 指的是Python语言,thrift可以指定多种语言编译

❝另外,此处的HBase安装目录下是没有hbase-thrift的,需要下载并放置Hbase安装目录下 ❞

  • 复制生成的 gen-py 目录下的 hbase 子目录 到工程目录 python3.6/site-packages/hbase 下直接使用

❝如果没有上一步骤中的编译操作,那么Python3操作hbase会报错,可以按以下方法解决 ❞

首先要下载Python3的Hbase文件,替换Hbase文件/usr/local/lib/python3.6/dist-packages/hbase/Hbase.py和ttypes.py

  • 下载地址:

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

❝最后就是测试是否可以使用Python成功访问HBase客户端,和第一种方法一样,不再赘述 ❞

5. 参考

  • https://www.qb5200.com/article/359274.html
  • NoSQL数据库原理与应用[人民邮电出版社]
  • https://blog.51cto.com/u_15187242/3729044