我正在使用Python3并使用impala包连接到Impala DB,如下所示:
#!/usr/bin/python3
import pandas as pd
from impala.dbapi import connect
from impala.util import as_pandas
import sys
def pull_from_dw(dw_conn, qry,qryparams):
cur = dw_conn.cursor()
if (qryparams is None):
cur.execute(qry)
else:
cur.execute(qry,qryparams)
custdata=as_pandas(cur)
return custdata
x = sys.argv[1]
query_str="select * from <table_name> where <column_name> = '{}';"
print(query_str)
dw_conn = connect(host='10.xxx.xx.xx', port=21050, use_ssl=True,
user='<username>',
password='<password>',
auth_mechanism='LDAP')
df = pull_from_dw(dw_conn,query_str,x)
print(df)
我可以通过在sql查询中指定.format(x)直接替换。但是,我需要在调用函数df = pull_from_dw(dw_conn,query_str,x)中进行变量替换,并得到如下错误。请协助:
$ / usr / bin / python3 script1.py'abc'
impala.error.ProgrammingError:查询参数参数应为列表,元组或字典对象
您正在将字符串传递给qryparams,而它需要一个列表元组或字典。通过以下内容,它应该可以解决您的问题: