Python连接presto开源提供了以下两个库可以使用
presto-python-client:https://github.com/prestodb/presto-python-client
pyhive:https://github.com/dropbox/PyHive
接下来就来分别实践一下,这两种客户端。
环境准备
- 创建一台Ubuntu的ECS,VPC选择和MRS集群一致
- 下载安装apt-get,pip3,python3
- 安装MRS客户端
参考https://support.huaweicloud.com/usermanual-mrs/mrs_01_0091.html
遇到的报错:Error: Network time protocol(NTP) not running. Please start NTP first.
解决方案:sudo /etc/init.d/ntp restart
4. 在MRS Manager页面中系统设置>用户管理中创建的用户,并下载认证凭据,解压后,将krb5.conf和user.keytab放到/root目录下
5. 获取SSL认证凭据
将集群中的/opt/Bigdata/MRS_1.8.10/install/FusionInsight-ldapclient-2.5.0/ldapclient/local/cert/cacert.pem文件拷贝到Ubuntu的ECS的/root目录下
连接方式一:presto-python-client
(1)安装依赖
- 安装pandas:
pip3 install pandas
- 安装requests-kerberos:
sudo apt-get install python3-dev
sudo apt install krb5-multidev
pip3 install requests_kerberos
- 安装presto-python-client
pip3 install presto-python-client
(2)编写代码
import prestodb import os from pandas import DataFrame os.system('kinit -kt {}/user.keytab {}'.format('/root','sxy')) conn=prestodb.dbapi.connect( host='192.168.0.194', #Presto Coordinator的ip port=7521, user='sxy', catalog='tpcds', schema='sf1', http_scheme='https', auth=prestodb.auth.KerberosAuthentication(config='/root/krb5.conf',service_name='presto',principal='sxy',mutual_authentication=False,ca_bundle='/root/cacert.pem') ) cur = conn.cursor() cur.execute('select c_first_name, c_last_name from customer limit 5') df = DataFrame(cur.fetchall()) df.columns = ['First_Name', 'Last_Name'] print(df)
(3)运行结果
连接方式二:PyHive
(1)安装依赖
- 安装requests-kerberos:
sudo apt-get install python3-dev
sudo apt install krb5-multidev
pip3 install requests_kerberos
- 安装pyhive
pip3 install pyhive2. 编写代码
(2)编写代码
from pyhive import presto presto_cli = presto.connect( host='192.168.0.194', port=7521, username='sxy', password='XXXX', # 创建用户时设置的密码 catalog='tpcds', schema='sf1', poll_interval=1, source='pyhive', session_props=None, protocol='https', requests_session=None, requests_kwargs={ 'verify':'/root/cacert.pem' }, KerberosRemoteServiceName='presto', KerberosPrincipal='sxy', KerberosConfigPath='/root/krb5.conf', KerberosKeytabPath='/root/user.keytab', KerberosCredentialCachePath=None, KerberosUseCanonicalHostname=None ) c = presto_cli.cursor() c.execute('show tables') for i in c.fetchall(): print(i)
- 运行结果
那些踩的坑
- 用EulerOS镜像装requests-kerberos遇到一些XXX.h文件无法找到的问题
没有找到EulerOS下替代sudo apt-get install python3-dev,修复该问题的方法。转而使用Ubuntu。
2. SSL认证失败
出现以下报错的原因pem文件配置不正确
3. 401 Unauthorized报错
原因:Kerberos的四个相关参数(user, principal,krb5,keytab)配置错误。
校验参数的方法:用presto_cli工具登录客户端后执行show tables,若成功则,参数正确。