掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
python連接Hive的幾種方式

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站制作、成都網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的貴港網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
·基于pyhive連接hive。
·基于impyla連接hive。
方法一:使用PyHive庫
安裝依賴包:其中sasl安裝可能會報錯,可以去https://www.lfd.uci.edu/~gohlke/pythonlibs/#sasl下載對應(yīng)版本安裝。
pip install sasl pip install thrift pip install thrift-sasl pip install PyHive
相關(guān)推薦:《Python基礎(chǔ)教程》
Python腳本代碼操作:
from pyhive import hive # or import hive
conn = hive.Connection(host='****', port=****, username='****', database='****')
cursor.execute(''SELECT * FROM my_awesome_data LIMIT 10'')
for i in range(****):
sql = "INSERT INTO **** VALUES ({},'username{}')".format(value, str(username))
cursor.execute(sql)
# 下面是官網(wǎng)代碼:
from pyhive import presto # or import hive
cursor = presto.connect('localhost').cursor()
cursor.execute('SELECT * FROM my_awesome_data LIMIT 10')
print(cursor.fetchone())
print(cursor.fetchall())方法二:使用impyla庫
impyla依賴包:
pip install six pip install bit-array pip install thriftpy
為了支持Hive還需要以下兩個包:
pip install sasl pip install thrift-sasl
可在Python PyPI中下載impyla及其依賴包的源碼
Python腳本代碼:
from impala.dbapi import connect
conn = connect(host ='****',port = ****)
cursor = conn.cursor()
cursor.execute('SELECT * FROM mytable LIMIT 100')
print cursor.description # 打印結(jié)果集的schema
results = cursor.fetchall() 
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流