Airflow DAG声明的3种方式

先说明一下我使用的airflow 2.2.4版本

  • 第一种使用标准构造函数,将dag通过参数传递进去

    import pendulum
    from airflow import DAG
    from airflow.operators.python import PythonOperator
    ​
    dag = DAG(
        dag_id='hello_world',
        start_date=pendulum.datetime(2022, 1, 1),
        schedule_interval=None,
        catchup=False,
        tags=['example']
    )
    ​
    ​
    def print_context():
        print('---------hello world---------')
    ​
    print_task = PythonOperator(task_id='print_context',
                                python_callable=print_context,
                                dag=dag)
  • 第二种,使用隐式的DAG

    import pendulum
    from airflow import DAG
    from airflow.decorators import task
    ​
    with DAG(
        dag_id = 'hello_world',
        start_date=pendulum.datetime(2022, 1, 1),
        schedule_interval = None,
        catchup=False,
        tags=['example']
    ) as dag:
        @task(task_id = 'print_context')
        def print_context():
            print('---------hello world---------')
        print_task = print_context()
  • 第三种,使用装饰器将函数转换成DAG生成器

    import pendulum
    from airflow.decorators import dag, task
    ​
    ​
    @dag(dag_id = 'hello_world',
         start_date=pendulum.datetime(2022, 1, 1),
         schedule_interval = None,
         catchup=False,
         tags=['example']
    )
    def hello_world2():
    ​
        @task(task_id = 'print_context')
        def print_context():
            print('---------hello world---------')
        print_task = print_context()
    ​
    dag = hello_world2()

个人推荐使用第二种方式定义

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用Airflow DAG进行网络接口请求的示例代码: ```python import requests from datetime import datetime, timedelta from airflow.models import DAG from airflow.operators.python_operator import PythonOperator default_args = { 'owner': 'airflow', 'depends_on_past': False, 'start_date': datetime(2021, 7, 1), 'retries': 1, 'retry_delay': timedelta(minutes=5) } dag = DAG( 'network_api_request', default_args=default_args, schedule_interval=timedelta(days=1) ) def get_api_data(): url = 'https://jsonplaceholder.typicode.com/todos' response = requests.get(url) data = response.json() return data def save_api_data(**context): data = context['task_instance'].xcom_pull(task_ids='get_api_data') with open('/path/to/save/data.json', 'w') as f: f.write(data) get_api_data_task = PythonOperator( task_id='get_api_data', python_callable=get_api_data, dag=dag ) save_api_data_task = PythonOperator( task_id='save_api_data', python_callable=save_api_data, provide_context=True, dag=dag ) get_api_data_task >> save_api_data_task ``` 在这个例子中,我们使用Python的requests库向一个API发送请求,并将其返回的数据保存到本地文件中。我们使用两个PythonOperator来执行两个任务:get_api_data和save_api_data。第一个任务使用get_api_data函数获取API数据,并将数据存储在XCom中。第二个任务使用save_api_data函数从XCom中获取数据,并将其写入本地文件中。 这个DAG每隔一天执行一次,并在执行过程中处理任何错误。你可以根据自己的需要修改该DAG,例如更改请求的API地址或更改数据的保存位置等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值