Airflow Postgress中的气流形态参数出现错误
脚本从以下位置获取输入参数{“flag”:“NA”,“metric_name”:“RED”}。我试图在sql的where子句中使用one param值。有人能检查一下我通过where caluse的方式是否有问题吗 错误:-在“{”处或附近出现语法错误Airflow Postgress中的气流形态参数出现错误,airflow,airflow-operator,Airflow,Airflow Operator,脚本从以下位置获取输入参数{“flag”:“NA”,“metric_name”:“RED”}。我试图在sql的where子句中使用one param值。有人能检查一下我通过where caluse的方式是否有问题吗 错误:-在“{”处或附近出现语法错误 您不能像这样在python可调用内部使用Jinja。 调用运算符时,将对Jinja进行求值,因此在您的情况下,Jinja引擎不会应用于sql语句 您需要做的只是使用一个简单的作业: sql=f"select count(1) as cn
您不能像这样在python可调用内部使用Jinja。 调用运算符时,将对Jinja进行求值,因此在您的情况下,Jinja引擎不会应用于sql语句 您需要做的只是使用一个简单的作业:
sql=f"select count(1) as cnt FROM odw.metrics where flag = {params.param1} "
我不太清楚你为什么要做这么多作业
我相信
sql=f“从odw.metrics中选择count(1)作为cnt,其中flag={varteam_flag}”
应该提供相同的sql语句。如何在UI中传递命令行参数/conf参数并在PostgresShake/PostgresOperator中使用它[简单python变量在这里使用f或.format()] 如果有其他的写作方式,请告诉我
sql=f"select count(1) as cnt FROM odw.metrics where flag = {params.param1} "
1. If you are using PostgresHook and calling PythonOperator for the module
def get_metrics(**kwargs):
varteam_flag=(kwargs['dag_run'].conf['flag'])
conn_id = kwargs.get('conn_id')
pg_hook = PostgresHook(conn_id)
connection = pg_hook.get_conn()
sql=f"select count(1) as cnt FROM odw.metrics where flag = {varteam_flag} "
print(sql)
cursor = connection.cursor()
cursor.execute(sql)
rows = cursor.fetchall()
cols = list(map(lambda x: x[0], cursor.description))
2. if you are using PostgresOperator [you can use jinja template]
gerics=PostgresOperator(task_id='gerics',
postgres_conn_id='veritas',
params={'param1': 10 },
sql=['truncate table test.dept1','insert into test.dept1 (select * from test.dept where rating={{ params.param1 }} )'],