我要做的很简单:使用Python连接到Impala数据库:
from impala.dbapi import connect
conn = connect(host='impala', port=21050, auth_mechanism='PLAIN')
我正在使用Impyla包来做到这一点。我得到了这个错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/thriftpy/transport/socket.py", line 96, in open
self.sock.connect(addr)
socket.gaierror: [Errno -3] Temporary failure in name resolution
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/alaaeddine/PycharmProjects/test/data_test.py", line 3, in <module>
conn = connect(host='impala', port=21050, auth_mechanism='PLAIN')
File "/usr/local/lib/python3.6/dist-packages/impala/dbapi.py", line 147, in connect
auth_mechanism=auth_mechanism)
File "/usr/local/lib/python3.6/dist-packages/impala/hiveserver2.py", line 758, in connect
transport.open()
File "/usr/local/lib/python3.6/dist-packages/thrift_sasl/__init__.py", line 61, in open
self._trans.open()
File "/usr/local/lib/python3.6/dist-packages/thriftpy/transport/socket.py", line 104, in open
message="Could not connect to %s" % str(addr))
thriftpy.transport.TTransportException: TTransportException(type=1, message="Could not connect to ('impala', 21050)")
还尝试了Ibis包,但失败了,并出现了与thriftpy相关的相同错误。
在使用Dbeaver的Windows中,我可以使用官方Cloudera JDBC连接器连接到数据库。我的问题是:
谢谢!
发布于 2019-02-02 01:32:58
解决方法:我使用pyhive包而不是Ibis/Impyla。下面是一个例子:
#import hive from pyhive
from pyhive import hive
#establish the connection to the db
conn = hive.Connection(host='host_IP_addr', port='conn_port', auth='auth_type', database='my_db')
#prepare the cursor for the queries
cursor = conn.cursor()
#execute a query
cursor.execute("SHOW TABLES")
#navigate and display the results
for table in cursor.fetchall():
print(table)
发布于 2019-03-18 02:21:27
您的impala域名不能被解析。你能在命令提示符下执行nslookup impala
吗?如果你正在使用Docker,你需要在docker中将docker服务名称组合为"impala“或"extra_hosts”选项。或者,您也可以始终将其作为impala 127.0.0.1
添加到/etc/hosts (Windows/Drivers/etc/hosts
也可以尝试“NOSASL”,而不是普通的,有时它在关闭安全性的情况下工作得更好。
发布于 2019-10-03 07:23:04
这是一种简单的方法,使用python通过impala shell连接impala。
import commands
import re
query1 = "select * from table_name limit 10"
impalad = str('hostname')
port = str('21000')
database = str('database_name')
result_string = 'impala-shell -i "'+ impalad+':'+port +'" -k -B --delimited -q "'+query1+'"'
status, output = commands.getstatusoutput(result_string)
print output
if status == 0:
print output
else:
print "Error encountered while executing HiveQL queries."
https://stackoverflow.com/questions/54482746
复制相似问题