With code, how do you update an airflow variable?

Justin Besteman picture Justin Besteman · Jan 4, 2019 · Viewed 11.7k times · Source

I need to update a variable I have made in Airflow programmatically but I can not find the answer on how to do that with code.

I have retrieved my variable with this code:

column_number = Variable.get('column_number')

At the end of the function, I would like to increment the column_number by one

I have tried this: Variable.set_val("column_number", int(column_number) + 1)

And it does not work.

Here is the full code for reference:

import airflow
from datetime import datetime, timedelta
from random import randint
from airflow import DAG
from airflow.hooks.postgres_hook import PostgresHook
from airflow.models import Variable
from airflow.operators.python_operator import PythonOperator

args = {
    'owner': 'besteman',
    'start_date': datetime.utcnow(),
    'retries': 1,
    'retry_delay': timedelta(minutes=30)
}

dag = DAG(dag_id='test-postgres', default_args=args, schedule_interval='@hourly')

def add_columns_and_values():

    column_number = Variable.get('column_number')

    pg_hook = PostgresHook(postgres_conn_id='airflow-test')

    add_columns = f'ALTER TABLE students ADD COLUMN test{column_number} smallint;'

    pg_hook.run(add_columns) 

    for i in range(8):
        add_values = f"UPDATE students SET test{column_number} = '{randint(50, 100)}' WHERE id = {i+1};"
        pg_hook.run(add_values)

    Variable.set_val("column_number", int(column_number) + 1)


t1 = PythonOperator(task_id='add_columns_values',
    python_callable=add_columns_and_values,
    dag=dag)

Answer

Daniel Huang picture Daniel Huang · Jan 5, 2019

Use Variable.set instead of Variable.set_val. set_val() is a setter for the val attribute and not intended for outside use. This should do what you want:

Variable.set("column_number", int(column_number) + 1)

It will make the actual update to the database, along with handling session and serialization for you if needed.

Reference: https://github.com/apache/incubator-airflow/blob/1.10.1/airflow/models.py#L4558-L4569