
一、下载hive提供的依赖包
将hive安装包下的lib/py中的文件拷贝到python的sys.path中的site_packages目录下,否则引用对应的包会报错,这个是使用hive提供的Python接口来调用hive客户端。

相关推荐:《Python入门教程》
二、安装pyhs2
控制台执行命令:
如果安装不成功,安装上面提到的依赖包就可以了。

三、启动hive 的thrift
确保以下服务开启,默认端口是10000:
1 | hive --service hiveserver
|
四、代码演示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #!/usr/bin/python
# -*- coding: UTF-8 -*-
import pyhs2
with pyhs2.connect(host= '10.104.111.33' ,
port=10000,
authMechanism= "PLAIN" ,
user= 'bestfei' ,
password= 'password' ,
database= 'default' ) as conn:
with conn.cursor() as cur:
#Show databases
print "cur.getDatabases"
print cur.getDatabases()
print "-" *40
#Execute query
cur.execute( "show databases" )
#Return column info from query
print cur.getSchema()
print "-" *40
#Fetch table results
for i in cur.fetch():
print i
|