2013-07-08 2 views

답변

1

임팔라는 메모리 캡을 맞추지 않는 한 동시에 여러 쿼리를 실행할 수 있습니다.

1

당신은 파일이 여러 쿼리를 세미콜론으로 구분 된 각 전체 쿼리를 가지고 impala-shell -f <<file_name>>, 같은 명령을 실행할 수 있습니다 (;) 당신은 파이썬 괴짜 경우

1

, 당신도 여러 개 만들 수 impyla 패키지를 시도 할 수 있습니다 연결하고 모든 쿼리를 한 번에 실행하십시오.

pip install impyla

1

나는 확실히 당신의 자신에 대한 몇 가지 테스트를 할 것입니다,하지만 난 여러 쿼리를 실행 얻을 수 없습니다 : 을 나는 임팔라 연결을 사용하고 .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)