I have some SQL to be triggered by airflow BigQueryOperator, one of the SQL is common for all tasks given below situation:
SQL to run - common.sql, abc.sql, xyz.sql
- Task1 -
common.sql + abc.sql - Task2 -
common.sql + xyz.sql
In order for one task run 2 SQL, I read 2 SQL files into a string, then run the merged-string to run task in one go.
Code looks like this:
with open ('common.sql', "r") as sqlfile:
common_array = sqlfile.readlines()
with open ('abc.sql', "r") as sqlfile:
abc_array = sqlfile.readlines()
# at this point, sql_script has all codes from common.sql and abc.sql
sql_script = ''.join(common_array) + '\n' + ''.join(abc_array)
BigQueryOperator(task_id='task1', sql=sql_script)
This serve my purpose, is there any other way that more elegant?