使用Python连接到Impala数据库(过时的错误)

时间:2019-02-01 15:44:48

标签: python python-3.x impala impyla ibis

我想要做的事情非常基本:使用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软件包,但失败了,并且存在相同的节俭相关错误。

在使用Dbeaver的Windows中,我可以使用官方Cloudera JDBC连接器连接到数据库。我的问题是:

  • 是否应在连接代码中将JDBC连接器作为参数传递?我进行了搜索,找不到指向该方向的东西。
  • 我应该尝试Ibis和Impyla软件包以外的其他方法吗?使用它们时,我遇到了许多与版本有关的问题和依赖项。如果是,您会推荐什么替代方案?

谢谢!

3 个答案:

答案 0 :(得分:0)

已解决: 我用的是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)

答案 1 :(得分:0)

您的黑斑羚域名不能解析。您可以在命令提示符下执行nslookup impala吗?如果使用的是Docker,则需要在docker-compose中将docker服务名称设置为“ impala”,或使用“ extra_hosts”选项。或者,您始终可以将其作为impala 127.0.0.1

添加到/ etc / hosts(Windows / Drivers / etc / hosts)中

有时,在关闭安全性的情况下,也可以尝试使用“ NOSASL”而不是PLAIN。

答案 2 :(得分:0)

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