To pass SQL as a file when leveraging the Postgres Operator you just have to provide a file name with .sql
at the end and make sure itβs in your image.
You could consider:
- Creating a sub folder in the
dags
folder namedsql
and putting all of your.sql
files in there - Using jinja templates and macros in your .sql files too (if you wanted to pass in execution_date to filter your sql)
Example
DAG Code
from airflow import DAG
from datetime import datetime, timedelta
from airflow.operators.postgres_operator import PostgresOperator
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'start_date': datetime(2019, 8, 25),
'email_on_failure': False,
'email_on_retry': False,
'retries': 1,
'retry_delay': timedelta(minutes=5),
}
with DAG('example_dag',
max_active_runs=3,
schedule_interval='@daily',
default_args=default_args) as dag:
t1 = PostgresOperator(
task_id='my_task',
sql='sql/my_query.sql'
)
Astro-Airflow Directory
βββ dags
β βββ example-dag.py
β βββ sql
β βββ my_query.sql
βββ include
βββ packages.txt
βββ plugins
β βββ example-plugin.py
βββ requirements.txt