内容纲要
概要描述
本文意在描述如何用Python3连接开了安全的 Hyperbase;
由于版本原因,需要修改部分库的源码来进行适配。
Python2连接开启安全的hyperbase参考:https://nj.transwarp.cn:8180/?p=585
详细说明
操作步骤
- 环境默认已安装python3环境
- 安装kinit客户端
#yum install krb5*
从开了 Hyperbase 安全的集群服务器上,将 /etc/hyperbase1/conf 目录下的 krb5.conf 文件文件拷贝到集群外部署python的服务器的 /etc 目录下
- 配置 /etc/hosts 文件,使其包含集群节点信息,例如:
# cat /etc/hosts
- 安装所需 Python 包
# pip install pure-sasl # pip install thrift # pip install hbase-thrift
- 安装Kerberos
- 下载Kerberos文件;
- 解压并将解压后的文件复制到python3安装目录下site-packages目录中,需要根据实际环境进行修改
# tar zxvf kerberos.gar.gz # tar zxvf kerberos-1.3.0.dist-info.tar.gz # cp -p * /usr/lib/python3.6.8/site-packages/
- 验证kerberos是否安装成功
# pip list | grep kerberos
- 替换site-packages/hbase目录下的Hbase.py和ttypes.py
Hbase.py–点此下载
ttypes.py–点此下载 - 修改site-packages/puresasl/mechanisms.py,将文件中的565行修改为:
kerberos.authGSSClientWrap(self.context, outgoing.decode('utf-8'), None, protect)
修改完成如下图:
Python3连接Hyperbase脚本参考如下:
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import ColumnDescriptor, Mutation, AlreadyExists, IOError
class HbaseClient:
def __init__(self, host='tdh700-mc001', port=19090, saslServiceName="hbase"):
sock = TSocket.TSocket(host, port)
self.transport = TTransport.TSaslClientTransport(sock, host, saslServiceName)
protocol = TBinaryProtocol.TBinaryProtocol(self.transport)
self.client = Hbase.Client(protocol)
self.transport.open()
def get_tables(self):
"""
获取所有表
"""
return self.client.getTableNames()
def create_table(self, table_name, column_list, max_versions=3):
"""
创建表
"""
column_families = list(map(lambda column: ColumnDescriptor(column, maxVersions=max_versions), column_list))
try:
self.client.createTable(table_name, column_families)
except AlreadyExists as e:
print('{}-->{}'.format(table_name, e))
def delete_table(self, table_name):
try:
self.client.deleteTable(table_name)
except IOError as e:
print(e)
# 插入数据
def put_data(self, table_name, column_data):
print('start put data to-->'.format(table_name))
for rowkey, value in column_data.items():
rowkey = rowkey
mutation = list(map(lambda value: Mutation(column=value['key'], value=value['value']), value))
self.client.mutateRow(table_name, rowkey, mutation)
def close_connection(self):
self.transport.close()
print('has closed connection')
if __name__ == "__main__":
client = HbaseClient()
# 建表
table_name = 'test0006'
column_list = ['cf1:', 'cf2:', 'cf3:']
client.create_table(table_name, column_list, max_versions=5)
# 获取所有已创建表
all_tables = client.get_tables()
print('all_tables:{}'.format(all_tables))
# 插入数据
column_data = {
'rowkey01': [
{'key': 'cf1:a', 'value': 'value-1-a'},
{'key': 'cf1:b', 'value': 'value-1-b'},
],
'rowkey02': [
{'key': 'cf2:a', 'value': 'value-2-a'},
{'key': 'cf2:b', 'value': 'value-2-b'},
]
}
client.put_data(table_name, column_data)
# 删除表
client.delete_table(table_name)
# 关闭连接
client.close_connection()