▌前言

     
python中用于连接HiveServer2的客户端有3个:pyhs2,pyhive,impyla。官网的示例采用的是pyhs2,但pyhs2的官网已声明不再提供支持,建议使用impyla和pyhive。我选择的python2.7和impala,运行环境Linux、Windows。

 

▌安装依赖(默认已安装好python2.7、pip)

   pip install sasl

   pip install thrift

   pip install thrift-sasl

   pip install impyla

* 安装sasl可能会报错,执行以下命令:
* 解决centos下缺少sasl.h的问题
           yum install cyrus-sasl-lib.x86_64

           yum install cyrus-sasl-devel.x86_64

           yum install libgsasl-devel.x86_64

           yum install saslwrapper-devel.x86_64

           一般拉说前面三句基本能解决问题,第四句留着实在没办法时用。

* 解决centos下缺少sasl.h的问题
           这里需要自己下载此文件,并清楚自己的python环境是32还是64.cd到此文件的路径下,然后执行如下命令:

           pip install sasl-0.2.1-cp27-cp27m-win32.whl或pip install
sasl-0.2.1-cp27-cp27m-win_amd64.whl

           可在cmd执行以下命令获取python环境的位数:

           1)、python

           2)、import platform 
           3)、platform.architecture() 

           

           到这里若无kerberos认证的hive可正常使用,但我们的测试环境带有认证,所以执行python脚本会报如下错:

     thrift.transport.TTransport.TTransportException: Could not start SASL:
Error in sasl_client_start (-4) SASL(-4): no mechanism available: Unable to
find a callback: 2

       
 问题出现的原因是:执行python脚本的机器无目标hive的kerberos认证信息。此问题暂未解决,而是Windows通过paramiko调用Linux服务器上连接hive的脚本,实现Windows操作hive。

 

▌python代码示例(linux)




#!/usr/bin/env python # -*- coding: utf-8 -*- # hive util with hive server2
from impala.dbapi import connect     class HiveClient:     def __init__(self,
db_host, port, authMechanism, user, password, database, kbservice):        
self.conn = connect(host=db_host,                             port=port,      
                      auth_mechanism=authMechanism,                            
user=user,                             password=password,                      
      database=database,                            
kerberos_service_name=kbservice                             )         def
query(self, sql):         with self.conn.cursor() as cursor:            
cursor.execute(sql)             return cursor.fetchall()         def
close(self):         self.conn.close()     if __name__ == '__main__':    
hive_client = HiveClient(db_host='IP', port=10000, authMechanism='GSSAPI',
user='test1', password='123456',                            
 database='database', kbservice='srv')     sql = "select * from
account_province_annual limit 5"     result = hive_client.query(sql)    
hive_client.close()     print result
▌结果



hive执行后的结果:



▌Windows

   
Windows直接操作服务器上的具有kerberos认证的hive,暂未解决。现阶段通过paramiko执行Linux服务器的hive操作脚本,见下一篇
https://blog.csdn.net/dlijuan/article/details/81204311
<https://blog.csdn.net/dlijuan/article/details/81204311>
。(因为需要使用RF写自动化,在Windows调试较方便。)

经过其他同事的努力,发现能通过MIT kerberos 认证用户,但出现了如下的错误,暂未解决,待时间充裕再来。希望已解决此问题的朋友能够告知,不胜感激。

MIT kerberos认证成功:



kinit命令报错如下:



MIT kerberos操作步骤见
https://mapr.com/docs/home/Impala/configuring_kerberos_authentication_for_windows.html

<https://mapr.com/docs/home/Impala/configuring_kerberos_authentication_for_windows.html>

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信