airflow的参数

来源[https://airflow.apache.org/code.html?highlight=ds_nodash#api-reference](https://airflow.apache.org/code.html?highlight=ds_nodash#api-reference)

Default Variables

VariableDescription
{{ ds }}the execution date as YYYY-MM-DD
{{ ds_nodash }}the execution date as YYYYMMDD
{{ yesterday_ds }}yesterday’s date as YYYY-MM-DD
{{ yesterday_ds_nodash }}yesterday’s date as YYYYMMDD
{{ tomorrow_ds }}tomorrow’s date as YYYY-MM-DD
{{ tomorrow_ds_nodash }}tomorrow’s date as YYYYMMDD
{{ ts }}same as execution_date.isoformat()
{{ ts_nodash }}same as ts without - and :
{{ execution_date }}the execution_date, (datetime.datetime)
{{ prev_execution_date }}the previous execution date (if available) (datetime.datetime)
{{ next_execution_date }}the next execution date (datetime.datetime)
{{ dag }}the DAG object
{{ task }}the Task object
{{ macros }}a reference to the macros package, described below
{{ task_instance }}the task_instance object
{{ end_date }}same as {{ ds }}
{{ latest_date }}same as {{ ds }}
{{ ti }}same as {{ task_instance }}
{{ params }}a reference to the user-defined params dictionary
{{ var.value.my_var }}global defined variables represented as a dictionary

{{ var.json.my_var.path }}

global defined variables represented as a dictionary with deserialized JSON object, append the path to the key within the JSON object

{{ task_instance_key_str }}

a unique, human-readable key to the task instance formatted {dag_id}_{task_id}_{ds}

{{ conf }}

the full configuration object located at airflow.configuration.conf which represents the content of your airflow.cfg

{{ run_id }}the run_id of the current DAG run
{{ dag_run }}a reference to the DagRun object

{{ test_mode }}

whether the task instance was called using the CLI’s test subcommand

Macros

VariableDescription
macros.datetimeThe standard lib’s datetime.datetime
macros.timedelta

The standard lib’s datetime.timedelta

macros.dateutilA reference to the dateutil package
macros.timeThe standard lib’s time
macros.uuidThe standard lib’s uuid
macros.randomThe standard lib’s random
<think>我们正在处理用户关于Apache Airflow工作流管理使用指南的请求。根据提供的引用资料,我们可以总结以下关键点: - Airflow是一个用于编程、调度和监控工作流的平台(引用[1]和引用[3])。 - 它使用Python代码定义任务依赖关系,以有向无环图(DAG)的形式管理数据处理流水线(引用[3])。 - Airflow提供了REST API和Python客户端库(引用[2])用于交互式管理。 用户需要的是使用指南,因此我们将从基础开始,逐步介绍如何使用Airflow进行工作流管理。 ### 回答结构: 1. **Airflow核心概念**:解释DAG、任务、算子等基本概念。 2. **安装与配置**:简要介绍安装步骤(基于引用[1])。 3. **创建第一个DAG**:通过一个简单示例展示如何定义工作流。 4. **Airflow的Web界面**:介绍如何监控和管理工作流。 5. **进阶功能**:提及一些高级特性如变量、连接、执行器等。 6. **API与客户端**:介绍如何使用REST API和Python客户端(引用[2])。 7. **最佳实践与常见问题**:提供一些使用建议。 ### 注意事项: - 使用中文回答。 - 数学公式(如果有)按规则使用$...$或$$...$$。 - 生成相关问题。 现在开始编写回答:</think>### Apache Airflow 工作流管理使用指南 Apache Airflow 是一个开源的工作流调度和监控平台,通过 Python 代码定义任务依赖关系,以**有向无环图(DAG)** 的形式管理数据处理流水线[^3]。以下是核心使用指南: --- #### 1. **核心概念** - **DAG(有向无环图)**:工作流的基本单位,由任务和依赖关系组成 - **Operator(算子)**:定义单个任务的执行逻辑(如 `PythonOperator`, `BashOperator`) - **Task**:Operator 的实例,代表具体执行单元 - **Task Dependency**:通过 `>>` 符号定义任务顺序(如 `task1 >> task2`) --- #### 2. **安装与初始化** ```bash # 安装 Airflow pip install apache-airflow # 初始化数据库 airflow db init # 创建管理员用户 airflow users create --username admin --password admin --firstname Admin --lastname User --role Admin --email admin@example.com # 启动 Web 服务器 airflow webserver --port 8080 # 启动调度器 airflow scheduler ``` > 访问 `http://localhost:8080` 登录 Web UI[^1] --- #### 3. **创建第一个 DAG** 在 `airflow/dags/` 目录下创建 `my_first_dag.py`: ```python from airflow import DAG from airflow.operators.bash import BashOperator from datetime import datetime with DAG( dag_id="hello_world", start_date=datetime(2023, 1, 1), schedule_interval="@daily" ) as dag: task1 = BashOperator( task_id="print_date", bash_command="date" ) task2 = BashOperator( task_id="echo_msg", bash_command='echo "Airflow is running!"' ) task1 >> task2 # 定义依赖关系 ``` - **Web UI 操作**:激活 DAG → 触发运行 → 查看任务日志[^3] --- #### 4. **关键功能实践** - **任务依赖控制**: ```python task1 >> [task2, task3] # 任务1完成后并行执行任务2和3 task3 >> task4 # 任务3完成后执行任务4 ``` - **参数传递**:使用 `XCom` 跨任务共享数据 - **错误处理**:设置 `retries` 和 `retry_delay` 实现自动重试 - **监控告警**:配置 `on_failure_callback` 发送邮件/钉钉通知 --- #### 5. **进阶功能** - **REST API**:通过 `apache-airflow-client-python` 管理 DAG[^2] ```python from airflow_client.client.api import dag_api from airflow_client.client import Configuration conf = Configuration(host="http://localhost:8080/api/v1") api = dag_api.DAGApi(conf) print(api.get_dags()) # 获取所有 DAG 信息 ``` - **执行器选择**:支持 `LocalExecutor`(单机)/ `CeleryExecutor`(分布式) - **集成扩展**:支持 KubernetesExecutor、DockerOperator 等容器化方案 --- #### 6. **最佳实践** 1. **DAG 设计原则**: - 单一职责:每个 DAG 专注一个业务目标 - 幂等性:任务可重复执行且结果一致 - 超时设置:避免僵尸任务 2. **性能优化**: - 使用 `@task` 装饰器简化 Python 任务 - 启用 `Pools` 控制并发资源 3. **安全配置**: - 加密敏感变量(`airflow connections add --conn-extra`) - 启用 RBAC 角色权限控制 > 生产环境建议使用 PostgreSQL 作为元数据库,并定期清理历史数据[^1] --- #### 7. **调试与排查** - **常见错误**: - DAG 未显示:检查文件语法错误或 `airflow list_dags` - 任务卡住:确认执行器资源是否充足 - **日志定位**: ```bash airflow tasks logs hello_world print_date --dag hello_world ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值