python通过impala连接hive总结

一.原理

脚本环境:windows
集群环境:centos6.8
通过在windows上使用python定时任务脚本执行大数据任务。采用了impala库查询,将查询到的结果存储到本地mysql数据库中。
在这里插入图片描述
重点:impala库安装

pip安装依赖:
thrift
thriftpy
thrift_sasl
pure_sasl
impyla
bitarray
开启hadoop集群 start-all.sh
开启hive server2并在后台运行
hive --service metastore &
hive --service hiveserver2 &

测试脚本:

# 导入连接工具
from impala.dbapi import connect
# 得到连接,
conn = connect(host='192.168.127.128', port=10000, auth_mechanism='PLAIN',user="root",password="root",database="default")
# 得到句柄	
cursor = conn.cursor()
# 执行查询
cursor.execute('show databases')
print(cursor.fetchall())
# 将结果放入dataframe中显示
# 关闭连接
cursor.close()
conn.close()

二.问题总结

1.时区出现问题
Note: System times on machines may be out of sync. Check system time and time zones.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
**
在这里插入图片描述
**
出现Note: System times on machines may be out of sync. Check system time and time zones.可能是虚拟机异常关闭导致虚拟机集群中的时间不一致。
centos6修改时间
date -s 20121019
date -s 23:40:00

2.connect() 失败:错误编号 = 10061
出现此问题:注意以下几点:
1》已安装正确的驱动程序。
2》已选择适当的端口。
3》已选择正确的 Hive 服务器(HiveServer 或 HiveServer2)。

3.TypeError: can’t concat str to bytes
定位到错误的最后一条,在init.py第94行(标黄的部分):

header = struct.pack(">BI", status, len(body))
#按照网上的提供的办法增加对BODY的处理
if (type(body) is str):
    body = body.encode()
self._trans.write(header + body)
self._trans.flush()

三.参考

使用impala连接hive踩坑过程:https://www.cnblogs.com/always-fight/p/11887808.html
win10 Python3.6连接hive:https://blog.csdn.net/jojosir/article/details/83472531

发布了76 篇原创文章 · 获赞 44 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/qq_38278799/article/details/104172337
今日推荐