簡介:python
接到一個任務,須要從 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
終於繞到今天的主角了~c++
1、HiveServer2git
shell > cd /usr/local/apache-hive-2.3.1-bin shell > sh bin/hiveserver2 start > logs/beeline.log 2>&1 & # 這就啓動了,中止的話好像必須 kill pid。
2、impylagithub
# 安裝依賴 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。