使用python连接到impala数据库(thriftpy错误)

xlpyo6sf  于 2021-06-26  发布在  Impala
关注(0)|答案(3)|浏览(572)

我尝试做的是非常基本的:使用python连接到impala db:

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中,我可以使用官方的clouderajdbc连接器连接到数据库。我的问题是:
应该在连接代码中作为参数传递jdbc连接器吗?我找了些东西找不到指向这个方向的东西。
我应该尝试其他的东西比朱鹭和 Impala 包?在使用它们时,我经历了许多与版本相关的问题和依赖性。如果是,你会推荐什么替代方案?
谢谢!

v6ylcynt

v6ylcynt1#

这是一个简单的方法,使用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."
uajslkp6

uajslkp62#

你的 Impala 域名不能解析。你能做到吗 nslookup impala 在命令提示符下?如果您使用的是docker,则需要在docker compose中将docker服务名称设置为“impala”或“extra\u hosts”选项。或者您也可以随时将其添加到/etc/hosts(windows/drivers/etc/hosts)中 impala 127.0.0.1 也可以尝试使用“nosasl”而不是“plain”,这样在关闭安全性的情况下效果会更好。

rpppsulh

rpppsulh3#

解决方法:我使用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)

相关问题