Want to access the config passed in the data in the API call
curl --location 'http://localhost:8080/api/v1/dags/postgres_operator_dag/dagRuns' \
--header 'Content-Type: application/json' \
--data '{
"conf": {
"seller_id": "test_seller"
}
}'
Want to access seller_id in the sqlquery in PostgresOperator
with DAG(dag_id="postgres_operator_dag", start_date=datetime(2020, 2, 2), schedule_interval=None) as dag:
# seller_id = context['dag_run'].conf['seller_id']. Cant access context here
connection_source_check = PostgresOperator(
task_id="fetch_connection_details",
postgres_conn_id="postgres_connection",
sql=f"""select c.name as connection_name, c.id as connection_id, a.name as source_name, a.id, a.actor_type as
source_id from "connection" c left join actor a on a.id=c.source_id where a.name=%(seller_id)s;""",
parameters={'seller_id': '{{ dag_run.conf["seller_id"] }}'},
dag=dag
)
The above task returns no data. But when I hardcode parameters as
parameters={'seller_id': 'test_seller'}
It works. Also Tried having sql file with params but with no effect.
This approach too doesnt work. It considers {{ dag_run.conf["seller_id"] }} as a string rather than evaluating the expression to test_seller and doesn't make the right query
with DAG(dag_id="postgres_operator_dag", start_date=datetime(2020, 2, 2), schedule_interval=None) as dag:
connection_source_check = PostgresOperator(
task_id="fetch_connection_details",
postgres_conn_id="postgres_hogwarts_connection",
sql=f"""select c.name as connection_name, c.id as connection_id, a.name as source_name, a.id, a.actor_type as
source_id from "connection" c left join actor a on a.id=c.source_id where a.name= '{{ dag_run.conf["seller_id"] }}' ;""",
dag=dag
)
This approach gives a syntax error -
psycopg2.errors.SyntaxError: syntax error at or near "{" LINE 2: ...ft join actor a on a.id=c.source_id where a.name={ dag_run....
with DAG(dag_id="postgres_operator_dag", start_date=datetime(2020, 2, 2), schedule_interval=None) as dag:
connection_source_check = PostgresOperator(
task_id="fetch_connection_details",
postgres_conn_id="postgres_hogwarts_connection",
sql=f"""select c.name as connection_name, c.id as connection_id, a.name as source_name, a.id, a.actor_type as
source_id from "connection" c left join actor a on a.id=c.source_id where a.name={{ dag_run.conf["seller_id"] }};""",
dag=dag
)
What is the right approach to access the seller_id value that was passed in the curl request data.