gpt4 book ai didi

python - 通过 Python 从 Impala 访问表

转载 作者:太空宇宙 更新时间:2023-11-04 04:06:51 25 4
gpt4 key购买 nike

我需要在同一个 cloudera 服务器上使用 python 通过 CLI 从 Impala 访问表

我已尝试使用以下代码建立连接:

def query_impala(sql):
cursor = query_impala_cursor(sql)
result = cursor.fetchall()
field_names = [f[0] for f in cursor.description]
return result, field_names


def query_impala_cursor(sql, params=None):
conn = connect(host='xx.xx.xx.xx', port=21050, database='am_playbook',user='xxxxxxxx', password='xxxxxxxx')
cursor = conn.cursor()
cursor.execute(sql.encode('utf-8'), params)
return cursor

但是因为我在同一个cloudera服务器上,所以我不需要提供主机名。您能否提供正确的代码以通过 python 访问存在于同一服务器上的 Impala/hive 表。

最佳答案

您可以使用 pyhive 连接到 hive 并访问您的 hive 表。

from pyhive import hive
import pandas as pd
import datetime

conn = hive.Connection(host="hostname", port=10000, username="XXXX")
hive.connect('hostname', configuration={'hive.execution.engine':'tez'})
query="select col1,col2,col3,col4 from db.yourhiveTable"

start_time= datetime.datetime.now()

data=pd.read_sql(query,conn)
print(data)

end_time=datetime.datetime.now()
print 'Finished reading from Hive table', (start_time-end_time).seconds/60.0,' minutes'

关于python - 通过 Python 从 Impala 访问表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57157942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com