gpt4 book ai didi

cloudera - cloudera impala 中的多个查询执行

转载 作者:行者123 更新时间:2023-12-04 00:42:23 26 4
gpt4 key购买 nike

是否可以在 impala 中同时执行多个查询?如果是,impala 是如何处理的?

最佳答案

我当然会自己做一些测试,但我无法执行多个查询:我正在使用 Impala 连接,并从 .sql 文件中读取查询。这适用于单个命令。

from impala.dbapi import connect
# actual server and port changed for this post for security
conn=connect(host='impala server', port=11111,auth_mechanism="GSSAPI")
cursor = conn.cursor()
cursor.execute((open("sandbox/z_temp.sql").read()))

这是我收到的错误。

 HiveServer2Error: AnalysisException: Syntax error in line 2:

这是 SQL 在 .sql 文件中的样子。

Select * FROM database1.table1;
Select * FROM database1.table2;

我能够在单独的 .sql 文件中使用 SQL 命令运行多个命令,迭代指定文件夹中的所有 .sql 文件。

#Create list of file names for recon .sql files this will be sorted
#Numbers at begining of filename are important to sort so that files will be executed in correct order

file_names = glob.glob('folder/.sql')

asc_names = sorted(file_names, reverse = False)
filename = ""
for file_name in asc_names:
str_filename = str(file_name)
print(filename)
query = (open(str_filename).read())

cursor = conn.cursor()

# creates an error log dataframe to print, or write to file at end of job.

try:
# Each SQL command must be executed seperately
cursor.execute(query)
df_id= pd.DataFrame([{'test_name': str_filename[-40:], 'test_status': 'PASS'}])
df_log = df_log.append(df_id, ignore_index=True)

except:
df_id= pd.DataFrame([{'test_name': str_filename[-40:], 'test_status': 'FAIL'}])
df_log = df_log.append(df_id, ignore_index=True)
continue

另一种方法是将所有 SQL 语句放在一个 .sql 文件中,用 ; 分隔。然后循环通过 .sql 文件拆分语句;一次运行一个。

from impala.dbapi import connect
from impala.util import as_pandas

conn=connect(host='impalaserver', port=11111, auth_mechanism='GSSAPI')
cursor = conn.cursor()

# split SQL statements from one file seperated by ';', Note: last command will not have semicolon at end.

sql_file = open("sandbox/temp.sql").read()
sql = sql_file.split(';')
for cmd in sql:
# This gets rid of the non printing characters you may have
cmd = cmd.replace('/r','')
cmd = cmd.replace('/n','')
# This runs your SQL commands one at a time.
cursor.execute(cmd)
print(cmd)

关于cloudera - cloudera impala 中的多个查询执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17519385/

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