使用 Python 连接到 Impala 数据库(thriftpy 错误)
Using Python to connect to Impala database (thriftpy error)
我想做的是非常基础的:使用 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 相关错误而失败。
在 Windows 使用 Dbeaver 时,我可以使用官方 Cloudera JDBC 连接器连接到数据库。我的问题是:
- 是否应该在我的连接代码中将我的 JDBC 连接器作为参数传递?我做了一些搜索,我找不到指向这个方向的东西。
- 除了 Ibis 和 Impyla 套餐,我还应该尝试其他套餐吗?我在使用它们时遇到了很多与版本相关的问题和依赖性。如果是,您会推荐什么替代方案?
谢谢!
已解决:
我使用 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 中包含 docker 服务名称 - 组成 "impala" 或具有 "extra_hosts" 选项。或者您可以随时将其添加到 /etc/hosts (Windows/Drivers/etc/hosts) 作为 impala 127.0.0.1
有时也可以尝试 'NOSASL' 而不是 PLAIN,这在关闭安全性的情况下效果更好。
这是简单的方法,使用python连接impala到impalashell。
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."
我想做的是非常基础的:使用 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 相关错误而失败。
在 Windows 使用 Dbeaver 时,我可以使用官方 Cloudera JDBC 连接器连接到数据库。我的问题是:
- 是否应该在我的连接代码中将我的 JDBC 连接器作为参数传递?我做了一些搜索,我找不到指向这个方向的东西。
- 除了 Ibis 和 Impyla 套餐,我还应该尝试其他套餐吗?我在使用它们时遇到了很多与版本相关的问题和依赖性。如果是,您会推荐什么替代方案?
谢谢!
已解决: 我使用 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 中包含 docker 服务名称 - 组成 "impala" 或具有 "extra_hosts" 选项。或者您可以随时将其添加到 /etc/hosts (Windows/Drivers/etc/hosts) 作为 impala 127.0.0.1
有时也可以尝试 'NOSASL' 而不是 PLAIN,这在关闭安全性的情况下效果更好。
这是简单的方法,使用python连接impala到impalashell。
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."