跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
最近更改
随机页面
MediaWiki帮助
代码酷
搜索
搜索
中文(中国大陆)
外观
创建账号
登录
个人工具
创建账号
登录
未登录编辑者的页面
了解详情
贡献
讨论
编辑“︁
Airflow Variables管理
”︁(章节)
页面
讨论
大陆简体
阅读
编辑
编辑源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
编辑
编辑源代码
查看历史
常规
链入页面
相关更改
特殊页面
页面信息
外观
移至侧栏
隐藏
您的更改会在有权核准的用户核准后向读者展示。
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
= Airflow Variables管理 = '''Airflow Variables'''是Apache Airflow中用于存储和访问全局配置参数的键值对系统。它们允许用户动态管理工作流中的配置值,而无需硬编码到DAG文件中,从而提高代码的可维护性和灵活性。 == 基本概念 == Variables在Airflow中扮演以下角色: * 集中存储环境特定参数(如文件路径、API密钥) * 在不同DAG之间共享配置 * 通过UI或CLI动态更新值而不修改代码 === 变量类型 === Airflow支持存储多种数据类型: * 字符串(默认) * JSON(可存储复杂结构) * 数值 * 布尔值 == 变量操作 == === 创建变量 === 可通过三种方式创建变量: '''1. Web界面''' 在Airflow UI的'''Admin → Variables'''中手动添加 '''2. CLI''' <syntaxhighlight lang="bash"> airflow variables set 'my_var' 'example_value' </syntaxhighlight> '''3. Python API''' <syntaxhighlight lang="python"> from airflow.models import Variable Variable.set("database_url", "postgresql://user:pass@localhost:5432/db") </syntaxhighlight> === 访问变量 === 在DAG中使用: <syntaxhighlight lang="python"> from airflow.models import Variable # 直接访问 db_url = Variable.get("database_url") # 带默认值 timeout = Variable.get("timeout", default_var=300) # 反序列化JSON config = Variable.get("config", deserialize_json=True) </syntaxhighlight> == 高级用法 == === 变量继承 === Airflow支持变量层级结构: <mermaid> graph TD A[全局变量] --> B[环境变量] B --> C[DAG特定变量] </mermaid> === 加密变量 === 敏感信息应使用加密存储: <syntaxhighlight lang="python"> from airflow.models import Variable from cryptography.fernet import Fernet key = Fernet.generate_key() cipher_suite = Fernet(key) encrypted = cipher_suite.encrypt(b"secret_value") Variable.set("api_key", encrypted.decode()) </syntaxhighlight> == 最佳实践 == 1. '''命名规范''':使用下划线分隔的命名方式(如`max_retry_count`) 2. '''敏感数据''':结合Airflow的Connections功能管理凭证 3. '''性能考虑''':频繁访问的变量应缓存到本地 4. '''版本控制''':通过<code>airflow variables export/import</code>实现配置迁移 == 实际案例 == '''场景''':跨环境部署ETL管道 1. 定义环境特定变量: <syntaxhighlight lang="json"> { "dev": { "input_path": "/data/dev/input", "output_path": "/data/dev/output" }, "prod": { "input_path": "/data/prod/input", "output_path": "/data/prod/output" } } </syntaxhighlight> 2. DAG中使用: <syntaxhighlight lang="python"> env = Variable.get("environment") # 'dev' 或 'prod' config = Variable.get(f"{env}_config", deserialize_json=True) def process_data(**kwargs): input_path = config["input_path"] output_path = config["output_path"] # 处理逻辑... </syntaxhighlight> == 数学表示 == 变量存储可形式化为键值映射: <math> \mathcal{V} : K \rightarrow V \quad \text{其中} \quad K \text{为键集合}, V \text{为值集合} </math> == 常见问题 == '''Q:变量与XCom有何区别?''' * 变量:全局配置,长期存储 * XCom:任务间临时通信,短期存在 '''Q:如何批量管理变量?''' 使用JSON文件导入/导出: <syntaxhighlight lang="bash"> airflow variables import variables.json airflow variables export variables.json </syntaxhighlight> == 性能注意事项 == 频繁访问变量会导致数据库查询增加,建议: * 在任务开始时获取并缓存变量 * 对静态配置使用DAG顶层的变量获取 * 考虑使用<code>Variable.get()</code>的<code>deserialize_json=True</code>参数减少调用次数 [[Category:大数据框架]] [[Category:Airflow]] [[Category:Airflow变量与连接]]
摘要:
请注意,所有对代码酷的贡献均被视为依照知识共享署名-非商业性使用-相同方式共享发表(详情请见
代码酷:著作权
)。如果您不希望您的文字作品被随意编辑和分发传播,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)