首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Python连接到Impala数据库(节俭错误)

使用Python连接到Impala数据库(节俭错误)
EN

Stack Overflow用户
提问于 2019-02-01 23:44:48
回答 3查看 5.8K关注 0票数 1

我要做的很简单:使用Python连接到Impala数据库:

代码语言:javascript
复制
from impala.dbapi import connect

conn = connect(host='impala', port=21050, auth_mechanism='PLAIN')

我正在使用Impyla包来做到这一点。我得到了这个错误:

代码语言:javascript
复制
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连接器连接到数据库。我的问题是:

  • 应该将我的JDBC连接器作为连接代码中的参数传递吗?我已经做了一些搜索,我不能找到指向这个direction.
  • Should的东西,我尝试了其他的东西,而不是Ibis和Impyla包?在使用它们时,我遇到了很多与版本相关的问题和依赖。如果是,你会推荐什么作为替代方案?

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-02-02 01:32:58

解决方法:我使用pyhive包而不是Ibis/Impyla。下面是一个例子:

代码语言:javascript
复制
#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)
票数 0
EN

Stack Overflow用户

发布于 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”,而不是普通的,有时它在关闭安全性的情况下工作得更好。

票数 0
EN

Stack Overflow用户

发布于 2019-10-03 07:23:04

这是一种简单的方法,使用python通过impala shell连接impala。

代码语言:javascript
复制
    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."
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54482746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档