1.首先安装anaconda3.6.5
略
2.配置好bin下环境变量
3.来到anaconda官网,搜索需要的包,pyhive(不是anaconda前缀的也行,例如biaze/pyhive)
https://anaconda.org/anaconda/
如上截图,选择 anaconda 对应的pyhive,点击进去
点击FIles,选择linux-64/pyhive-0.6.0-py36_0.tar.bz2
注:_0,_1是小版本,下那个都行;py36对应python3.6
4.安装pyhive
conda install pyhive-0.6.0-py36_0.tar.bz2
5.启动python(anaconda3.6.5/bin/python)
from pyhive import hive
如果报错,就去安装对应缺少的包,直接在anaconda3官网搜索
例如:
这里缺少的past,你去搜索会发现只有paste,安装后还是会报这个错,这时直接百度python paste
点击进去安装past包,发现past是含在future包中的,在anaconda3官网搜索future安装对应版本,解决上面的问题,pyhive至此安装完毕
6.pyhive测试
(1)首先启动hiveserver2服务(后台启动,命令打全)
nohup hive --service hiveserver2 --hiveconf hive.server2.thrift.port=端口 &
注:别与其他用户端口冲突,可以先这样启动:hive --service hiveserver2 --hiveconf hive.server2.thrift.port=端口,然后查看是否有端口冲突报错
(2)进入python进行测试连接:(端口为启动hiveserver2的端口)
from pyhive import hive
conn = hive.Connection(host='xxxx', port=xxxx, username='xxx', database='xxxx')
cursor= conn.cursor()
cursor.execute('select * from xxx limit 10')
for result in cursor.fetchall():
(三个及以上空格)print result (只能输出select 的结果,insert无法输出)
(3)关于pyhive insert等走mr流程的操作无法得到资源运行的问题,请设置如下:
cursor.execute("set mapreduce.job.queuename=队列名")(测试可用,放到执行语句前面)
(4)关闭hive锁
set hive.support.concurrency=false;
问题:
pyhive先执行select,在执行insert操作,出现hive锁
from pyhive import hive
conn = hive.Connection(host='xxxx', port=xxxx, username='xxx', database='xxxx')
cursor= conn.cursor()
cursor.execute('select * from xxx limit 10')
正常这一步
cursor.execute(‘insert into test values (1)')
这一步出现报错
解决:
执行如下:
cursor.execute("set hive.support.concurrency=false")
再次运行,成功执行