需要在window10下,使用python去连hive,获取在hive中的数据,然后训练模型,将模型训练好后,相关模型结果写回到hive进行持久化。目的是不让数据在本地建模环境有存储!!!
环境:
操作系统 | window 10 |
python | python 3.6.5 |
hive | 1.2.1 |
python所需要的第三方依赖
包名 | 版本 | 安装命令 |
bitarray | 0.8.1 | pip install bitarray==0.8.1 -i https://pypi.tuna.tsinghua.edu.cn/simple |
impyla | 0.16.0 | pip install impyla==0.16.0 -i https://pypi.tuna.tsinghua.edu.cn/simple |
pure_sasl | 0.5.1 | pip install pure_sasl==0.5.1 -i https://pypi.tuna.tsinghua.edu.cn/simple |
thrift | 0.9.3 | pip install thrift==0.9.3 -i https://pypi.tuna.tsinghua.edu.cn/simple |
thrift_sasl | 0.2.1 | pip install thrift_sasl==0.2.1 -i https://pypi.tuna.tsinghua.edu.cn/simple |
thriftpy | 0.3.9 | pip install thriftpy==0.3.9 -i https://pypi.tuna.tsinghua.edu.cn/simple |
thriftpy2 | 0.4.7 | pip insta thriftpy2==0.4.7 -i https://pypi.tuna.tsinghua.edu.cn/simple |
ps 暂时在实践过程中发现的包冲突问题如下:
1、impyla 对 thrift 库的要求是<=0.9.3,而pyhive 0.6.1不兼容thrift 0.9.3
2、thrift_sasl保证在0.2.1及以下,要不然会报错误。待会后面会介绍报的错误
3、有了thrift_sasl后,需要将另一个第三方包卸载。sasl需要卸载。这个包在使用pyhive的时候会有所使用到。
连接hive的代码
from impala.dbapi import connect
from impala.util import as_pandas
conn = connect(host='*.*.*.*', port=10000, auth_mechanism='PLAIN', database='src')
cursor = conn.cursor()
cursor.execute('select * from table_name limit 10')
print(cursor.description) #打印字段名
#print(as_pandas(cursor)) #打印结果
#转化为dataframe
df = as_pandas(cursor)
print(df)
cursor.close()
conn.close()
运行结果如下:
在如上过程中。遇到如下一些坑。记录下,以防后续备忘
问题一:
解决方案:
conn = connect(host='*.*.*.*', port=10000, database='src')
#在这句话中,添加上auth_mechanism='NONE'
conn = connect(host='*.*.*.*', port=10000, auth_mechanism='NONE', database='src')
问题二:
解决方案:
conn = connect(host='*.*.*.*', port=10000, auth_mechanism='NONE', database='src')
#将如上这句代码中的auth_mechanism='NONE'修改为auth_mechanism='PLAIN'
conn = connect(host='*.*.*.*', port=10000, auth_mechanism='PLAIN', database='src')
我之所以将auth_mechanism='NONE'的原因是由于hive-site.xml这个文件中的如下参数是NONE
代码中应该修改为auth_mechanism='PLAIN'
问题三:
解决方案:
#将sasl该第三方包卸载。
pip uninstall sasl
问题四:
解决方案:
将thrift_sasl该包中的__init__.py第94行代码修改下下
修改后如下:
问题五:
解决方案
#将thrift_sasl 0.3.0卸载,安装0.2.1
pip install thrift_sasl==0.2.1
至此。问题都解决。看到希望!!!