使用pyhive:连接hive(python3+)

1.安装相关依赖

sudo yum install cyrus-sasl-devel -y
sudo yum install gcc-c++  -y

pip3 install sasl
pip3 install thrift
pip3 install thrift-sasl
pip3 install PyHive

2.连接hive查询数据

# -*- coding: utf-8 -*-
from pyhive import hive

conn = hive.Connection(host='192.168.1.72', port=10000, username='root', database='testdalu')#host主机ip,port:端口号,username:用户名,database:使用的数据库名称
cursor=conn.cursor()
cursor.execute('select * from t_t1')#执行查询
for result in cursor.fetchall():
     print(result)                      #将查询结果打印出来
conn.close()

问题1:

thrift.transport.TTransport.TTransportException: Could not start SASL: Error in sasl_client_start (-4) SASL(-4): no mechanism available: No worthy mechs found

解决办法:

yum install cyrus-sasl-plain  cyrus-sasl-devel  cyrus-sasl-gssapi

问题2:

[root@node2 bin]# python3 hive-py.py
Traceback (most recent call last):
File “hive-py.py”, line 4, in
conn = hive.Connection(host=‘192.168.1.72’, port=10000, username=‘root’, database=‘testdalu’)#host主机ip,port:端口号,username:用户名,database:使用的数据库名称
File “/usr/local/lib/python3.6/site-packages/pyhive/hive.py”, line 199, in init
_check_status(response)
File “/usr/local/lib/python3.6/site-packages/pyhive/hive.py”, line 495, in _check_status
raise OperationalError(response)
pyhive.exc.OperationalError: TOpenSessionResp(status=TStatus(statusCode=3, infoMessages=[’*org.apache.hive.service.cli.HiveSQLException:Failed to open new session: java.lang.RuntimeException: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.authorize.AuthorizationException): User: root is not allowed to impersonate root:14:13’, ‘org.apache.hive.service.cli.session.SessionManager:createSession:SessionManager.java:419’, ‘org.apache.hive.service.cli.session.SessionManager:openSession:SessionManager.java:362’, ‘org.apache.hive.service.cli.CLIService:openSessionWithImpersonation:CLIService.java:193’, ‘org.apache.hive.service.cli.thrift.ThriftCLIService:getSessionHandle:ThriftCLIService.java:440’, ‘org.apache.hive.service.cli.thrift.ThriftCLIService:OpenSession:ThriftCLIService.java:322’, ‘org.apache.hive.service.rpc.thrift.TCLIService P r o c e s s o r Processor ProcessorOpenSession:getResult:TCLIService.java:1377’, 'org.apache.hive.service.rpc.thrift.TCLIService$P…

解决办法:

vim /data/hadoop/hadoop-2.7.7/etc/hadoop/core-site.xml
<!- 用户“root”可以代理所有主机上的所有用户 >
    <property>
       <name>hadoop.proxyuser.root.hosts</name>
       <value>*</value>
    </property>
    <property>
        <name>hadoop.proxyuser.root.groups</name> 
        <value>*</value>
   </property>

重启hadoop

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值