簡介:
接到一個任務,需要從 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/impyla、https://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。