Airflow 如何从DAG读取配置文件?
气流似乎是理智生产气流部署的重要组成部分 我有一个带有动态子DAG的DAG,由配置文件驱动,类似于: config.yaml:Airflow 如何从DAG读取配置文件?,airflow,google-cloud-composer,Airflow,Google Cloud Composer,气流似乎是理智生产气流部署的重要组成部分 我有一个带有动态子DAG的DAG,由配置文件驱动,类似于: config.yaml: imports: - project_foo - project_bar` 这将产生子DAG任务,如imports.project{foo|bar}.step{1 | 2 | 3} 我通常使用python的open函数读取配置文件,laconfig=open(os.path.join(os.path.split(_文件)[0],'config.yaml') 不
imports:
- project_foo
- project_bar`
这将产生子DAG任务,如imports.project{foo|bar}.step{1 | 2 | 3}
我通常使用python的open
函数读取配置文件,laconfig=open(os.path.join(os.path.split(_文件)[0],'config.yaml')
不幸的是,在使用打包DAG时,这会导致错误:
Broken DAG: [/home/airflow/dags/workflows.zip] [Errno 20] Not a directory: '/home/airflow/dags/workflows.zip/config.yaml'
有什么想法/最佳实践可以推荐吗?这有点困难,但我最终还是通过
ZipFile
阅读了zip文件内容
import yaml
from zipfile import ZipFile
import logging
import re
def get_config(yaml_filename):
"""Parses and returns the given YAML config file.
For packaged DAGs, gracefully handles unzipping.
"""
zip, post_zip = re.search(r'(.*\.zip)?(.*)', yaml_filename).groups()
if zip:
contents = ZipFile(zip).read(post_zip.lstrip('/'))
else:
contents = open(post_zip).read()
result = yaml.safe_load(contents)
logging.info('Parsed config: %s', result)
return result
正如您从主dag.py
中所期望的那样:
get_config(os.path.join(path.split(__file__)[0], 'config.yaml'))
这还是唯一的办法吗(