使用Python3操作HBase

简介: 使用Python3操作HBase

使用Python3操作HBase


d93b307106c147e29b4eb4e0f8eae2a0.jpeg



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 ,指定依赖库为: thrift  和 hbase-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测试


fromthriftimportThriftfromthrift.transportimportTSocket,TTransportfromthrift.protocolimportTBinaryProtocolfromhbaseimportHbase# thrift默认端口是9090socket=TSocket.TSocket('127.0.0.1',9090)
socket.setTimeout(5000)
transport=TTransport.TBufferedTransport(socket)
protocol=TBinaryProtocol.TBinaryProtocol(transport)
client=Hbase.Client(protocol)
socket.open()
fromhbase.ttypesimportColumnDescriptor,Mutation,BatchMutation,TRegionInfofromhbase.ttypesimportIOError,AlreadyExistsprint(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 "<stdin>", line 1, in <module>
  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)]


  • 查看9090端口是否开放
sudo netstat -lp | grep9090


  • 开启thrift服务
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$ jps3904 NodeManager
3268 NameNode
5381 Jps
4568 HMaster
4696 HRegionServer
5305 ThriftServer
3420 DataNode
3774 ResourceManager
3614 SecondaryNameNode
4463 HQuorumPeer


可以看到已经有ThriftServer


  • 重新执行上述Python代码
>>> 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']


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/)


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--versionThrift 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. 参考


相关实践学习
云数据库HBase版使用教程
&nbsp; 相关的阿里云产品:云数据库 HBase 版 面向大数据领域的一站式NoSQL服务,100%兼容开源HBase并深度扩展,支持海量数据下的实时存储、高并发吞吐、轻SQL分析、全文检索、时序时空查询等能力,是风控、推荐、广告、物联网、车联网、Feeds流、数据大屏等场景首选数据库,是为淘宝、支付宝、菜鸟等众多阿里核心业务提供关键支撑的数据库。 了解产品详情:&nbsp;https://cn.aliyun.com/product/hbase &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
4月前
|
Java Shell 分布式数据库
【大数据技术Hadoop+Spark】HBase数据模型、Shell操作、Java API示例程序讲解(附源码 超详细)
【大数据技术Hadoop+Spark】HBase数据模型、Shell操作、Java API示例程序讲解(附源码 超详细)
84 0
|
7月前
|
Shell 分布式数据库 Apache
Hbase常用shell操作
Hbase常用shell操作
322 1
|
7月前
|
Java Shell 分布式数据库
HBase高级操作
HBase高级操作
202 0
|
4月前
|
分布式计算 Hadoop Shell
|
4月前
|
存储 分布式计算 分布式数据库
对给定的数据利用MapReduce编程实现数据的清洗和预处理,编程实现数据存储到HBase数据库,实现数据的增删改查操作接口
对给定的数据利用MapReduce编程实现数据的清洗和预处理,编程实现数据存储到HBase数据库,实现数据的增删改查操作接口
27 0
|
4月前
|
SQL 存储 NoSQL
分布式NoSQL列存储数据库Hbase操作(二)
分布式NoSQL列存储数据库Hbase操作(二)
114 0
|
5月前
|
分布式计算 分布式数据库 Hbase
99 MapReduce操作Hbase
99 MapReduce操作Hbase
43 0
|
7月前
|
Shell 分布式数据库 Hbase
Hbase shell管理操作
Hbase shell管理操作
53 0
|
8月前
|
Shell 分布式数据库 数据库
Hbase Shell操作
Hbase Shell操作
133 0
|
9月前
|
存储 Java Shell
分布式数据库HBase的常用操作的基本Shell命令的表操作
HBase是一个分布式数据库系统,支持高性能、可伸缩、实时的数据存储和处理。在HBase中,我们可以使用Shell命令来进行常用的操作,如创建表、插入数据、查询数据、更新数据等。本文将介绍关于分布式数据库HBase的常用操作基本Shell命令的表操作。
136 0