我要做的是非常基本的:使用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连接器连接到数据库。我的问题是:
谢谢!
解决:我使用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)
您的impala域名不得解析。你能在命令提示符下做nslookup impala
吗?如果您正在使用Docker,则需要将docker-compose中的docker服务名称设置为“impala”或具有“extra_hosts”选项。或者您可以随时将其作为impala 127.0.0.1
添加到/ etc / hosts(Windows / Drivers / etc / hosts)
同时尝试'NOSASL'而不是PLAIN,有时在关闭安全性时效果更好。