Ubuntu Kylin16.04
Anaconda
环境下的Python3.9
HBase1.1.5
hbase-thrift-0.20.0.patch
Thrift0.16.0
Python3 通过
thrift
,rpc
接口操作HBase
,指定依赖库为:thrift
和hbase-thrift
。 然而我们 在 Python3 环境中发现 hbase-thrift-0.20.4无法被支持
, hbase-thrift 官方仅推荐用于 python2.x 。 所以需要使用下边的 patch 版本 和 patch 版本写法的客户端「第一种Python调用HBase的方法」。
「Ubuntu安装Anaconda及注意事项」
root@node01:~$ conda create -n test python=3.9
root@node01:~$ conda activate test
(test) root@node01:~$ pip3 list | grep hbase-thrift
(test) root@node01:~$ pip3 uninstall -y hbase-thrift
(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
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
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 IOError,AlreadyExists
print(client.getTableNames()) # 获取当前所有的表名
此处应该提前安装pymysql
(test) root@node01:/usr/local/$ pip3 install -y pymsql
需要提前开启thrift服务,否则会报错
Could not connect to any of [('127.0.0.1', 9090)]
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/anaconda/envs/pyhbase/lib/python3.9/site-packages/thrift-0.16.0-py3.9-linux-x86_64.egg/thrift/transport/TSocket.py", line 146, in open
raise TTransportException(type=TTransportException.NOT_OPEN, message=msg)
thrift.transport.TTransport.TTransportException: Could not connect to any of [('127.0.0.1', 9090)]
sudo netstat -lp | grep 9090
test@node01:/usr/local/hbase-1.1.5$ hbase-daemon.sh start thrift
starting thrift, logging to /usr/local/hbase-1.1.5/bin/../logs/hbase-test-thrift-node01.out
test@node01:/usr/local/hbase-1.1.5$ jps
3904 NodeManager
3268 NameNode
5381 Jps
4568 HMaster
4696 HRegionServer
5305 ThriftServer
3420 DataNode
3774 ResourceManager
3614 SecondaryNameNode
4463 HQuorumPeer
可以看到已经有ThriftServer
>>> 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 IOError,AlreadyExists
>>> print(client.getTableNames()) # 获取当前所有的表名
['SC', 'Student', 'job_tmp', 'user_action']
使用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依赖的库,如 Automake、LibTool、Bison、Boost等,请参考「Thrift官网安装手册」
https://thrift.apache.org/docs/install/
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.2
、boost 1.53.0
、编译可能需要的运行时库lex
、yacc
– 从源代码构建的要求「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安装目录
(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
(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客户端,和第一种方法一样,不再赘述
https://www.qb5200.com/article/359274.html
NoSQL数据库原理与应用[人民邮电出版社]
https://blog.51cto.com/u_15187242/3729044