一、前期工作
在Linux(我用的Ubuntu18.04)已经安装好Apache Hadoop2和Anaconda3,并已经安装了HBase(可参考4.6 HBase基本使用实践)
二、新建Python环境
先新建一个Python环境(如果使用之前建立好的则可以忽略此步骤)。
在shell中输入以下命令
$ conda create -n hadoop python=3.7
这样就创建了一个名为hadoop的python3.7环境。
然后激活此环境
$ conda activate hadoop
三、安装thrift和happybase
thrift 是facebook开发并提交给Apache的开源二进制通讯中间件通过thrift,我们可以用Python来操作Hbase。
happybase是Python通过Thrift访问HBase的库。
在上述Python环境下输入如下命令来安装thrift和happybase
pip install thrift
pip install happybase
注意:安装happybase可能会遇到错误
unable to execute 'gcc': No such file or directory
error: command 'gcc' failed with exit status 1
这是因为没有安装gcc(GNU Compiler Collection)。执行
sudo apt-get install gcc
安装好gcc后,再重新执行happybase安装命令即可。
四、启动HBase for Python
这里使用伪分布模式的HBase(配置方法参考https://www.jianshu.com/p/11f15025d6b2)
假设现在刚启动计算机(没有进入任何环境),首先启动HDFS——因为我已经把HDFS的目录添加在.bashr的PATH了,所以可以不加目录
$ start-dfs.sh #或输入完全路径/usr/local/hadoop/sbin/start-dfs.sh
接着启动HBase
$ start-hbase.sh #或输入完全路径/usr/local/hbase/bin/start-hbase.sh
以上是HBase核心启动过程(通常使用HBase都要有的步骤),下面是Python使用HBase的必要步骤。
启动thrift
$ hbase-daemon.sh start thrift
这时可以输入jps查看已启动的Java进程
4512 HQuorumPeer
3121 DataNode
5298 Jps
2914 NameNode
3379 SecondaryNameNode
5172 ThriftServer
4777 HRegionServer
4634 HMaster
Jps是Java的主进程,NameNode、SecondaryNameNode和DataNode是HDFS的进程,HMaster、HRegionServer和HQuorumPeer是HBase的进程,ThriftServer是Thrift的进程。八个进程都出现说明启动成功。
如果使用完毕要退出,则依次输入以下命令:
hbase-daemon.sh stop thrift
stop-hbase.sh
stop-dfs.sh
然后我们可以进入刚才建立的conda环境
$ conda activate hadoop
我使用Spyder来写Python脚本。如果没有安装,可输入
$ conda install spyder
安装好之后,输入
$ spyder
即进入Python IDE环境。
注意如果启动了SSH则可能无法使用Spyder(报错“QXcbConnection: Could not connect to display”)。如果不想使用IDE,也可以直接输入“python”进入Python命令行来操作。
五、Python下操作HBase
1. 创建HBase连接
在Python环境下,引入happybase库
import happybase
创建HBase连接
conn=happybase.Connection() #等效于conn=happybase.Connection()
其默认设置是
happybase.Connection(host=‘localhost’, port=9090, timeout=None, autoconnect=True, table_prefix=None, table_prefix_separator=b’_’, compat=‘0.98’, transport=‘buffered’, protocol=‘binary’)
连接建立好之后查看可以使用的table
print(conn.tables())
用其他方式(如shell)在HBase建立的table也会显示出来。如果还没有建立任何表则返回
[]
2. 新建表
创建表一个名为student2的表,包含五个属性(即列族)Sname、Ssex、Sage、Sdept和course
conn.create_table(
'student2',
{
'Sname': dict(max_versions=10),
'Ssex': dict(),
'Sage': dict(),
'Sdept': dict(),
'course': dict()
}
)
max_versions是指定最多保留的版本数,可缺省。
这时再输入
print(conn.tables())
则返回
[b'student2']
3. 添加数据
要操作已经创建的表,则先获取其table实例
ts=conn.table('student2')
添加主键(默认,可视为学号)为95001,名字(Sname列族)为YangXing的一行数据——注意所有输入都必须是字符串
ts.put(row='95001',data={'Sname:':'YangXing','course:math':'90'})
注意即使没有列限定符冒号也不能省(如上面的“Sname:”),这点和shell操作HBase不同。
put方法每输入一行都要和HBase通信一次,如果想多行一次性批量写入HBase可以使用batch
bat = ts.batch()
bat.put(row='95001',data={'course:english':'82'})
bat.put(row='93020',data={'Sname:':'WeiLiu','course:math':'87'})
bat.send()
为方便可利用上下文管理器with/as实现
with ts.batch() as bat:
bat.put(row='95001',data={'course:english':'82'})
bat.put(row='93020',data={'Sname:':'WeiLiu','course:math':'87'})
4. 查看数据
这里介绍三种方法:row、rows和scan。功能不同,酌情使用。
(1) row
如果想获取一行的数据,如行号95001
ts.row('95001')
输出
{b'Sname:': b'YangXing', b'course:english': b'82', b'course:math': b'90'}
如果想获取某行的某列族,如行号95001的course列族
ts.row(row='95001',columns=['course'])
注意course后不能加冒号,输出
{b'course:english': b'82', b'course:math': b'90'}
即输出此列族的所有列限定符内容。
如果想获取某行的某列族的某列限定符内容,如行号95001的course列族的english列
ts.row(row='95001',columns=['course:english'])
输出
{b'course:english': b'82'}
另外,可以通过设定来显示时间戳
ts.row(row='95001',columns=['course:english'],include_timestamp=True)
输出
{b'course:english': (b'82', 1587562954464)}
注意columns只能传入list或tuple,如[course]或(course,)。
(2) rows
可使用rows来获取多行
ts.rows(rows=['95001','93020'])
输出
[(b'95001', {b'Sname:': b'YangXing', b'course:english': b'82', b'course:math': b'90'}),
(b'93020', {b'Sname:': b'WeiLiu', b'course:math': b'87'})]
也可指定列族
ts.rows(rows=['95001','93020'],columns=['course'])
输出
[(b'95001', {b'course:english': b'82', b'course:math': b'90'}),
(b'93020', {b'course:math': b'87'})]
甚至列限定符
ts.rows(rows=['95001','93020'],columns=['course:math'])
输出
[(b'95001', {b'course:math': b'90'}), (b'93020', {b'course:math': b'87'})]
同row一样,rows也可以设置include_timestamp=True来显示时间戳。
(3) scan
如果想遍历整个表的所有内容
for key,value in ts.scan():
print(key,value)
输出
b'93020' {b'Sname:': b'WeiLiu', b'course:math': b'87'}
b'95001' {b'Sname:': b'YangXing', b'Sname:a': b'ss', b'course:english': b'82', b'course:math': b'90'}
也可设置起始行键(行键的顺序由系统自动排列)
for key,value in ts.scan(row_start='95001'):
print(key,value)
输出
b'95001' {b'Sname:': b'YangXing', b'Sname:a': b'ss', b'course:english': b'82', b'course:math': b'90'}
或终止行键
for key,value in ts.scan(row_stop='95001'):
print(key,value)
输出
b'93020' {b'Sname:': b'WeiLiu', b'course:math': b'87'}
[b'student', b'student2', b'zhy']
也可以row_start和row_stop同时设定,注意是前闭后开区间。
同row一样,scan也可以设置include_timestamp=True来显示时间戳。
5. 删除数据
使用delete命令删除某行的若干列族/列数据。注意delete命令会删除所有历史数据(类似于shell的deleteall)。
删除一整行,如93020行
ts.delete(row='93020')
执行后93020这行的所有数据(包括任何列的所有历史版本)都被删除
删除某行某列族,如95001行的course列族
ts.delete(row='95001',columns=['course'])
注意course后面没有冒号
删除某行某列族某列,如95001行的course列族的english列
ts.delete(row='95001',columns=['course:english'])
Reference:
https://blog.csdn.net/qq_21153619/article/details/82619925
https://www.jianshu.com/p/62f687ba0c11
https://blog.csdn.net/weixin_33860722/article/details/92385409
https://blog.csdn.net/ytusdc/article/details/78679100
https://www.cnblogs.com/tashanzhishi/p/10917956.html