Python3 impyla 连接 hiveserver2


简介:

接到一个任务,需要从 hive 中读取数据,生成报表。

于是找到了官方文档:https://cwiki.apache.org/confluence/display/Hive/Setting+Up+HiveServer2#SettingUpHiveServer2-PythonClientDriver

官方文档提供了一个使用 pyhs2 连接 hive 的例子,这本来很好的嘛。

结果去 Github:https://github.com/BradRuderman/pyhs2 瞅了一眼,很遗憾 pyhs2 项目已经不维护了。

不过,提供了两个很不错的替代项目:https://github.com/cloudera/impylahttps://github.com/dropbox/PyHive

终于绕到今天的主角了~

一、HiveServer2

shell > cd /usr/local/apache-hive-2.3.1-bin

shell > sh bin/hiveserver2 start > logs/beeline.log 2>&1 &

# 这就启动了,停止的话好像必须 kill pid。

二、impyla

# 安装依赖
shell > yum -y install gcc gcc-c++ cyrus-sasl-devel cyrus-sasl-plain

# 创建虚拟环境
shell > virtualenv --no-site-packages -p python3 venv

# 启用虚拟环境
shelll > source venv/bin/activate

(venv) shell > python -V
Python 3.6.3

# 安装 impyla 及所需依赖包
(venv) shell > pip install ipython six bit_array thriftpy thrift_sasl==0.2.1 sasl impyla

(venv) shell > ipython

In [1]: from impala.dbapi import connect

In [2]: conn = connect(host="192.168.10.45", port=10000, database="logsdb", auth_mechanism="PLAIN")

In [3]: cur = conn.cursor()

In [4]: cur.execute("select count(*) from log_bftv_api")

In [5]: cur.fetchone()
Out[5]: (1379094425,)

In [6]: conn.close()

# 程序查出了 hive table log_bftv_api 中总共有 1379094425 条数据。

# 其中,连接配置中 auth_mechanism 的值由 hive-site.xml 配置文件中 hive.server2.authentication 配置项指定。

# PLAIN 代表不启用认证,也就是 hive.server2.authentication 的默认值:NONE。
posted @ 2017-11-17 12:07  WangXiaoQiang  阅读(5819)  评论(0编辑  收藏  举报