Python API 使用
HBase是由Java写的,原生支持Java接口,对python人员不友好,那么针对这种情况,HBase提供了thrift接口服务器,可以采用其他语言编写HBase代码通过thrift接口连接操作HBase
启动HBase Thrift服务的方式是hbase-daemon.sh start thrift
python环境准备
前面在学习hive时,node03节点root用户,已经准备了python环境
编写Python HBase代码
脚本文件hbase_py_test.py
# coding=utf-8
from hbase.ttypes import ColumnDescriptor, Mutation
from thrift.transport import TSocket
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
transport = TSocket.TSocket('node01', 9090)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)
transport.open()
# 1.创建表,无返回值:createTable(tableName,columnFamilies)
cf1 = ColumnDescriptor(b"cf1")
cf2 = ColumnDescriptor(b"cf2")
client.createTable(b"py_students",[cf1,cf2])
# 2.获取所有表:getTablesName()
all_tables = client.getTableNames()
print(all_tables)
# 3.向表中插入数据:mutateRow(tableName,rowkey,mutations,attributes),tableName:表名称,mutations指的是多行数据
def put(cli,tbl,rowkey,columns):
func = lambda k_v: Mutation(column=k_v[0], value=k_v[1])
mutations = list(map(func, columns.items()))
cli.mutateRow(tbl,rowkey,mutations,None)
put(client,b"py_students",b"rowkey001",{b"cf1:name":b"zhangsan",b"cf1:age":b"18",b"cf2:gender":b"f",b"cf2:score":b"100"})
put(client,b"py_students",b"rowkey002",{b"cf1:name":b"lisi",b"cf1:age":b"20",b"cf2:gender":b"m",b"cf2:score":b"200"})
put(client,b"py_students",b"rowkey001",{b"cf1:name":b"wangwu",b"cf1:age":b"20",b"cf2:gender":b"m",b"cf2:score":b"200"})
# 4.获取rowkey记录:getRow(tableName,rowkey,attributes)
t_row_results = client.getRow(b"py_students",b"rowkey001",None)
for row in t_row_results:
print("rowkey:%s , columns = %s"%(row.row,row.columns))
# 5.删除记录:deleteAll(tableName,rowkey,deleteCol,attributes),tableName:表名称,deleteCol:要删除的“列族:列”
client.deleteAll(b"py_students",b"rowkey001",b"cf1:name",None)
# 6. 删除rowkey对应的记录:deleteAllRow(tableName,rowkey,attributes)
client.deleteAllRow(b"py_students",b"rowkey001",None)
# 7. 禁用表,无返回值:disableTable(tableName)
client.disableTable(b'py_students')
# 8. 启用表,无返回值:enabledTable(tableName)
client.enableTable(b'py_students')
# 9. 验证表是否被启用,返回一个bool值:isTableEnabled(tableName)
print(client.isTableEnabled(b'py_students'))
# 10. 删除表,无返回值deleteTable(tableName),需要先禁用表才能删除表
client.disableTable(b'py_students')
client.deleteTable(b'py_students')
# 使用完成transport对象后,可以关闭对象
transport.close()
# node01 启动 hbase thrift
hbase-daemon.sh start thrift
# node03 测试python连接hbase
# 进入python虚拟环境
source /data/virtual/venv/bin/activate
# 创建上面的hbase_py_test.py脚本文件
# 安装thrift和hbase-thrift
pip install thrift
pip install hbase-thrift
# 执行脚本,会报如下错误
python3 hbase_py_test.py
File "/data/virtual/venv/lib/python3.6/site-packages/hbase/Hbase.py", line 2066
except IOError,
# 由于这里使用python3版本,安装的hbase-thrift对于python3有一些不兼容,需要自己编译hbase的相关python依赖包
# 安装开发工具包,用于编译C/C++项目
yum groupinstall -y "Development tools"
# 安装并编译thrift,可以查看hbase源码找到对应的thrift的版本
cd ~
wget https://github.com/apache/thrift/archive/refs/tags/v0.14.1.tar.gz
tar xf thrift-0.14.1.tar.gz
cd thrift-0.14.1
./bootstrap.sh
./configure
make && make install
# 查看thrift是否安装成功
thrift -version
Thrift version 0.14.1
# 下载hbase源码,并通过thrift生成hbase相关的py文件
cd ~
wget https://dlcdn.apache.org/hbase/stable/hbase-2.5.8-src.tar.gz
tar xf hbase-2.5.8-src.tar.gz
# 生成hbase相关的py文件,并替换通过'pip install hbase-thrift'安装的py文件
thrift --gen py ~/hbase-2.5.8/hbase-thrift/src/main/resources/org/apache/hadoop/hbase/thrift/Hbase.thrift
cp ~/gen-py/hbase/*.py /data/virtual/venv/lib/python3.6/site-packages/hbase
# 执行脚本,这次执行成功了
python3 hbase_py_test.py
# 退出虚拟环境
deactivate