execution_date的气流变量/宏

时间:2017-05-08 20:08:16

标签: python-3.6 airflow

我想使用一些Airflow宏作为变量,我可以放在我的文件顶部而不是函数中。

这是我目前设置代码的方式。在这个例子中我有3个任务,每个任务都引用一个函数。我想避免使用xcoms,所以我将** kwargs放在每个函数中。我还看到有一个宏来设置execution_date的格式,所以如果可能的话我想用它替换我的两个函数。

def select_remote_file(execution_date):
    remote_file_date = execution_date.strftime(REMOTE_DATE_FORMAT)
    remote_file_path = REMOTE_PATH + PREFIX + remote_file_date + EXTENSION
    return remote_file_path

def prepare_local_file(execution_date):
    local_file_date = execution_date.strftime(LOCAL_DATE_FORMAT)
    local_file_path = str(LOCAL_PATH / PREFIX) + local_file_date + EXTENSION
    return local_file_path

def extract(**kwargs):
    execution_date = kwargs['execution_date'] - dt.timedelta(days=3)
    local_file_path = prepare_local_file(execution_date=execution_date)
    remote_file_path = select_remote_file(execution_date=execution_date)

def transform(**kwargs):
    execution_date = kwargs['execution_date'] - dt.timedelta(days=3)
    local_file_path = prepare_local_file(execution_date=execution_date)
    remote_file_path = select_remote_file(execution_date=execution_date)

def load(**kwargs):
    execution_date = kwargs['execution_date'] - dt.timedelta(days=3)
    local_file_path = prepare_local_file(execution_date=execution_date)

t1 = PythonOperator(task_id=f'extract_{FILE}',
                         python_callable=download_raw_from_sftp,
                         provide_context=True,
                         dag=dag)
t2 = PythonOperator(task_id=f'convert_utf_{FILE}',
                        python_callable=convert_utf,
                        provide_context=True,
                        dag=dag)

t3 = PythonOperator(task_id=f'compress_{FILE}',
                          python_callable=compress_file,
                          provide_context=True,
                          dag=dag)

我的目标是在文件的顶部有一个变量,我可以在我的函数中引用它,而不是逐个编辑它们。这可能吗?

EXECUTION_DATE = {{ds}}

或某些文件

EXECUTION_DATE = ds_add('ds', - 3)

0 个答案:

没有答案