airflow 文档学习(一) 基本Operator

本文介绍了Airflow中的Operator和BaseSensorOperator,包括BaseOperator的基本参数、方法和可调用值,以及Core Operators如BaseSensorOperator、BashOperator、PythonOperator等的详细参数和使用场景。
摘要由CSDN通过智能技术生成

1. Operator和task

简单来说,Operator就是task的抽象类

2. BaseOperator

所有的功能性Operator的来源

2.1 参数:

task_id (string) :唯一标识task的id
owner (string)
retries (int):任务重试此时
retry_delay (timedelta) :重试间隔
start_date (datetime):任务开始时间,第一个任务实例的执行时间
end_date (datetime):如果指定的话,任务执行不会超过这个时间
depends_on_past (bool):如果指定的话,任务实例将以此运行并且依赖上一个任务的成功
wait_for_downstream (bool):在这个参数指定的任何地方,depends_on_past都将强制为true
queue(str):指定到队列,CeleryExcutor指定特定队列
dag(DAG):指定dag
pool(str):此任务运行的插槽池,限制任务的并发
execution_timeout (datetime.timedelta):执行此任务实例允许的最长时间,超过最长时间则任务失败
sla(datetime.timedelta):作业预计成功时间
on_failure_callback(callable):当此任务实例失败时调用的函数
on_retry_callback (callable) :与on_failure_callback相似,只是重试任务时触发
on_success_callback (callable) :与on_failure_callback相似,任务成功时触发
trigger_rule (str):定义依赖的触发规则
  ·包括的选项有:{all_success | all_failed | all_done | one_success | one_failed | dummy},默认为all_success
run_as_user(str):在运行任务时使用unix用户名进行模拟
executor_config (dict):特定的执行程序解释其任务参数
task_concurrency (int):设置后,任务将限制execution_dates之间的并发运行
resources (dict):资源参数名称(Resources构造函数的参数名称)与其值的映射。
priority_weight(int):此任务的优先级权重
weight_rule(str):用于任务的有效总优先级权重的加权方法
max_retry_delay (timedelta):重试最大延迟间隔
retry_exponential_backoff (bool) 

2.2 方法

set_downstream(task_or_task_list)
将任务或者任务列表设置为当前任务的下游
set_upstream(task_or_task_list)
将任务或者任务列表设置为当前任务的上游

clear(start_date=None, end_date=None, upstream=False, downstream=False, session=None)
根据指定参数清除与任务关联的任务实例的状态
execute(context)
创建operator时派生的主要方法
get_direct_relative_ids(upstream=False)
获取当前任务上游或者下游的直接相对id
get_direct_relatives(upstream=False)
获取当前任务的上游或者下游直接关联对象
get_flat_relative_ids(upstream=False, found_descendants=None)
获取上游或者下游的关联对象的id列表
get_flat_relatives(upstream=False)
获取上游或者下游的关联对象列表
get_task_instances(session, start_date=None, end_date=None)
获取特定时间范围的与此任务相关的任务实例
has_dag()
是否设置dag
on_kill()
当任务实例被杀死时,重写方法以清除子进程
run(start_date=None, end_date=None, ignore_first_depends_on_past=False, ignore_ti_state=False, mark_success=False)
为日期范围运行一组任务实例
post_execute(context, result=None)
调用self.execute()后立即触发,它传递上下文和operator返回的结果
pre_execute(context)
调用self.execute()之前触发
prepare_template()
模板化字段被其内容替换之后触发
render_template(attr, content, context)
从文件或直接在字段中呈现模板,并返回呈现结果
render_template_from_field(attr, content, context, jinja_env)
从字段中呈现模板
xcom_pull(context, task_ids=None, dag_id=None, key='return_value', include_prior_dates=None)
xcom_push(context, key, value, execution_date=None) 

2.3 可调用值

dag:如果设置了则返回dag,否则报错
deps:返回依赖任务列表
downstream_list:下游任务列表
schedule_interval:任务排列
upstream_list:上游任务列表

3. BaseSensorOperator

基于 airflow.models.BaseOperator, airflow.models.SkipMixin

3.1 参数:

soft_fail (bool):设置为true以将任务标记为失败时的skipped
poke_interval (int):作业在每次尝试之间应等待的时间(单位:秒)
timeout (int):超时时间

3.2 方法:

execute(context)
poke(context)

4. Core Operators

Airflow PythonOperator is a task in Apache Airflow that allows you to execute a Python function as a task within an Airflow DAG (Directed Acyclic Graph). It is one of the most commonly used operators in Airflow. The PythonOperator takes a python_callable argument, which is the function you want to execute, and any other necessary arguments for that function. When the task is executed, Airflow will call the specified Python function and perform the logic inside it. Here's an example of how to use PythonOperator in an Airflow DAG: ```python from airflow import DAG from airflow.operators.python_operator import PythonOperator from datetime import datetime def my_python_function(): # Your logic here print("Hello, I am running inside a PythonOperator") dag = DAG( 'my_dag', start_date=datetime(2022, 1, 1), schedule_interval='@daily' ) my_task = PythonOperator( task_id='my_task', python_callable=my_python_function, dag=dag ) ``` In this example, we define a DAG called 'my_dag' with a daily schedule interval. We then create a PythonOperator called 'my_task' that executes the function `my_python_function`. Whenever the DAG is triggered, Airflow will execute the logic inside `my_python_function`. You can add more parameters to the PythonOperator based on your requirements, such as providing arguments to the python_callable function or defining the pool for task execution. The output of the function can also be used by downstream tasks in the DAG. I hope this answers your question about the Airflow PythonOperator! Let me know if you have any further queries.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值