I am trying to read sql file that contains query with jinja templates in the custom operator in Airflow. I have already achieved it using PythonOperator that calls function where I used
def execute_query(**kwargs)
sql_query = open('my_sql_query.sql').read() #(SELECT * FROM my_table WHERE date > {})
sql_query.format(kwargs['ds'])
but I would prefer use this syntax {{ ds }} directly in the query like
SELECT * FROM my_table WHERE date > {{ ds }}
What I have done:
- I created CustomOperator with template_fields and template_ext
class SQLOperator(BaseOperator):
template_fields = ['sql']
template_ext = ('.sql',)
@apply_defaults
def __init__(
self,
name = None,
sql = None,
*args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.name = name
self.sql = sql
def execute(self, context):
print("Name", name) # <- works
print("Query", sql) # <- doesn't work and I don't know how to get the sql file content
- Dag
default_args = {...}
dag = DAG(
'sql_operator_test',
schedule_interval='0 0 * * *',
template_searchpath=['/Users/username/airflow/dags/sql/test/'],
default_args=default_args)
sql_task = SQLOperator(
task_id='sql_process',
name="Aaa",
sql="/Users/username/airflow/dags/sql/test.sql",
dag=dag)
- SQL query
SELECT * FROM my_table WHERE date > {{ ds }}
I am running out of ideas. Is there any option to pass the file to the operator or get its rendered content?

