使用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

    
       hadoop.proxyuser.root.hosts
       *
    
    
        hadoop.proxyuser.root.groups 
        *
   

重启hadoop

你可能感兴趣的:(bigdata)