python写数据到hive_Python数据篇之Pyhive
Python提供了操作hive數據倉庫的API接口,一下記錄Windows和Linux系統環境的環境配置和語法。目前筆者在Windows上不能成功運行程序,依然寫下Windows平臺配置。
1,安裝三方庫:
Windows:
sasl/thrift/thriftsasl/pyhive,其中thrift/thrift_sasl/pyhive可直接通過pip或者conda直接安裝;sasl需要到如下網址下載whl文件安裝:https://www.lfd.uci.edu/~gohlke/pythonlibs/,搜索下載對應版本的xxx.whl文件到本地,通過pip install xxx.whl來安裝。
Linux:
sasl/thrift/thriftsasl/pyhive,直接通過pip即可安裝。
2,代碼實例:
from pyhive import hive
import sasl
import thrift
import thrift_sasl
import pysnooper
@pysnooper.snoop()
def read_hive(host, port, username, password, auth):
conn = hive.Connection(host=host, port=port, username=username, password=password, auth=auth)
cursor = conn.cursor()
query_sql = 'select * from user_table limit 10'
cursor.execute(query_sql)
result = cursor.fetchall()
cursor.close()
conn.close()
for res in result:
print(res)
return result
實例化Connection傳參,auth為‘CUSTOM’,如果運行結果報錯:thrift.transport.TTransport.TTransportException: TSocket read 0 bytes,則在hive集群機器修改配置文件:
打開hive-site.cml,增加如下內容:
hive.server2.authentication
NOSASL
3,以上步驟執行下來,Linux系統代碼運行OK,Windows系統代碼運行依然報錯。
總結
以上是生活随笔為你收集整理的python写数据到hive_Python数据篇之Pyhive的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么要玩FLTK(Fast Light
- 下一篇: Blender全新毛发工具演示露出,预计