内容纲要
概要描述
本文意在描述如何用 Python 连接开了安全的 Hyperbase。
截止到目前 TDH 最新版本 6.2,Python连接 Hyperbase,暂时 只支持python2,不支持python3.
案例环境说明
IP地址 | hostname | 说明 |
---|---|---|
172.22.16.88 | tdh-001 | 集群外节点 |
172.22.33.1 | mll01 | 集群节点1 |
172.22.33.2 | mll02 | 集群节点2 |
172.22.33.3 | mll03 | 集群节点3 |
详细说明
-
安装Python2.7
# yum -y install gcc # wget https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz # gunzip Python-2.7.13.tgz # tar xvf Python-2.7.13.tar # cd Python-2.7.13 # ./configure --prefix=/usr/local/python2.7 --with-threads --enable-shared # make && make install
-
安装epel源,并安装python-pip2 & python-pip
# rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm # yum install python-pip*
-
安装kinit客户端
#yum install krb5*
从开了 Hyperbase 安全的集群服务器上,将 /etc/hyperbase1/conf 目录下的 krb5.conf 文件和相应的 keytab 文件拷贝到集群外部署python的服务器的 /etc 目录下
-
配置 /etc/hosts 文件,使其包含集群节点信息,例如:
# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.22.22.1 tdh-001 172.22.16.88 tdh1-600 172.22.33.1 mll01 172.22.33.2 mll02 172.22.33.3 mll03
-
查看Keytab认证信息(查看keytab的principal),例如:
# klist -kt /etc/yangliu.keytab
# kinit -kt /etc/yangliu.keytab mll@TDH
-
安装所需 Python 包
# pip install pure-sasl # pip install thrift # pip install hbase-thrift
-
安装Kerberos
- 下载Kerberos文件;
- 解压并将解压后的文件复制到 /usr/lib/python2.7/site-packages/ 目录下
# tar zxvf kerberos.gar.gz # tar zxvf kerberos-1.3.0.dist-info.tar.gz # cp -p * /usr/lib/python2.7/site-packages/
- 验证kerberos是否安装成功
# pip list | grep kerberos
Python 连接Hyperbase脚本参考如下:
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from hbase import Hbase
from hbase.ttypes import *
thriftServer = "172.22.33.1"
thriftPort = 9090
saslServiceName = "hbase"
sock = TSocket.TSocket(thriftServer, thriftPort)
transport = TTransport.TSaslClientTransport(sock, thriftServer, saslServiceName)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Hbase.Client(protocol)
transport.open()
client.getTableNames()