Last active
December 10, 2021 09:39
-
-
Save kaxil/61f41dd87a69230d1a637dc3a1d2fa2c to your computer and use it in GitHub Desktop.
Using Airflow Json Variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from airflow.models import Variable | |
# Common (Not-so-nice way) | |
# 3 DB connections when the file is parsed | |
var1 = Variable.get("var1") | |
var2 = Variable.get("var2") | |
var3 = Variable.get("var3") | |
# Recommended Way | |
# Just 1 Database call | |
dag_config = Variable.get("dag1_config", deserialize_json=True) | |
dag_config["var1"] | |
dag_config["var2"] | |
dag_config["var3"] | |
# You can directly use it Templated arguments {{ var.json.my_var.path }} | |
bash_task = BashOperator( | |
task_id="bash_task", | |
bash_command='{{ var.json.dag1_config.var1 }} ', | |
dag=dag, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@saveriogzz The connections to db outside of dag and task objects should be as minimum as possible.
You can also use secrets backend i.e store Airflow Variable in environment variable - https://airflow.apache.org/docs/apache-airflow/1.10.10/concepts.html#storing-variables-in-environment-variables and then you can have as many variable outside dag objects.