Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass and use parameters to a MySqlOperator

Tags:

airflow

Given a MySqlOperator and an sql query how do i pass a parameter and use it? Here is my code which doesn't work. I am trying to pass the table name as a parameter.

UPDATE_SQL = "UPDATE {table} SET version=version+1 WHERE id='x'"

update_row = MySqlOperator(
                dag=dag,
                task_id='update_row',
                mysql_conn_id=CONNECTION_ID,
                parameters={'table': 'hello_world'},
                database=MY_DATABASE,
                sql=UPDATE_SQL,
                )

I could not find any examples on how to use the parameters in the Operator.

like image 800
Sutirtha Das Avatar asked Oct 15 '22 23:10

Sutirtha Das


1 Answers

You can do it as:

UPDATE_SQL = "UPDATE {{ params.table }} SET version=version+1 WHERE id='x'"

update_row = MySqlOperator(
                dag=dag,
                task_id='update_row',
                mysql_conn_id=CONNECTION_ID,
                params={"table": "hello_world"},
                database=MY_DATABASE,
                sql=UPDATE_SQL,
                )

enter image description here

like image 75
Elad Kalif Avatar answered Oct 21 '22 04:10

Elad Kalif