LoginSignup
2
2

More than 1 year has passed since last update.

Airflow2.0のTaskflowAPIでmacroを使う方法

Last updated at Posted at 2021-08-17

背景

Airflow2.0で、PythonOperatorやXComの受け渡しをシンプルに書ける TaskflowAPI が登場しました。TaskflowAPIでAirflowのMacroを使う方法なかなか見つからなかったのでメモしておきます。

実装

タスク内で from airflow.operators.python import get_current_contextget_current_context() を呼び出すとマクロが取得できます。

sample.py
from airflow.decorators import dag, task
from airflow.utils.dates import days_ago
from airflow.operators.python import get_current_context


@dag(default_args={'owner': 'airflow'}, schedule_interval=None, start_date=days_ago(2))
def fetch_pd_log_entries():

    @task
    def execute_task():
        print(get_current_context()['execution_date']) # pendulumオブジェクトが取得できる

    execute_task()


hoge_dag = fetch_pd_log_entries()

参考

Passing arguements using Taskflow API in Airflow 2.0

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2