python连接hive的几种方式
·基于pyhive连接hive。
·基于impyla连接hive。
方法一:使用PyHive库
安装依赖包:其中sasl安装可能会报错,可以去https://www.lfd.uci.edu/~gohlke/pythonlibs/#sasl下载对应版本安装。
pipinstallsasl pipinstallthrift pipinstallthrift-sasl pipinstallPyHive
相关推荐:《Python基础教程》
Python脚本代码操作:
frompyhiveimporthive#orimporthive
conn=hive.Connection(host='****',port=****,username='****',database='****')
cursor.execute(''SELECT*FROMmy_awesome_dataLIMIT10'')
foriinrange(****):
sql="INSERTINTO****VALUES({},'username{}')".format(value,str(username))
cursor.execute(sql)
#下面是官网代码:
frompyhiveimportpresto#orimporthive
cursor=presto.connect('localhost').cursor()
cursor.execute('SELECT*FROMmy_awesome_dataLIMIT10')
print(cursor.fetchone())
print(cursor.fetchall())
方法二:使用impyla库
impyla依赖包:
pipinstallsix pipinstallbit-array pipinstallthriftpy
为了支持Hive还需要以下两个包:
pipinstallsasl pipinstallthrift-sasl
可在Python PyPI中下载impyla及其依赖包的源码
Python脚本代码:
fromimpala.dbapiimportconnect
conn=connect(host='****',port=****)
cursor=conn.cursor()
cursor.execute('SELECT*FROMmytableLIMIT100')
printcursor.description#打印结果集的schema
results=cursor.fetchall()
下一篇