Python连接hive库

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

《Python连接hive库》 001.png

二、安装pyhs2
控制台执行命令:pip install pyhs2
如果安装不成功,安装上面提到的依赖包就可以了。

《Python连接hive库》 pyhs2.png

三、启动hive 的thrift
确保以下服务开启,默认端口是10000:
hive –service hiveserver

四、代码演示

#!/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
    原文作者:BestFei
    原文地址: https://www.jianshu.com/p/65781aaaa6c7
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞