【发布时间】:2020-04-28 06:36:47
【问题描述】:
我正在尝试创建一个 DAG,它根据位于存储中的 JSON 文件动态生成任务。我一步一步地遵循了这个指南:
https://bigdata-etl.com/apache-airflow-create-dynamic-dag/
但是 DAG 卡住了以下消息:
是否可以读取外部文件并使用它在 Composer 中动态创建任务?当我只从气流变量中读取数据时,我可以这样做,但是当我读取外部文件时,dag 卡在isn't available in the web server's DagBag object 状态。我需要从外部文件中读取,因为 JSON 的内容会随着每次执行而改变。
我正在使用composer-1.8.2-airflow-1.10.2。
我读到了一个类似问题的答案:
Dynamic task definition in Airflow
但我并没有尝试基于单独的任务创建任务,仅基于外部文件。
这是我的第二种方法,也陷入了错误状态:
import datetime
import airflow
from airflow.operators import bash_operator
from airflow.operators.dummy_operator import DummyOperator
from airflow.models import Variable
import json
import os
products = json.loads(Variable.get("products"))
default_args = {
'owner': 'Composer Example',
'depends_on_past': False,
'email': [''],
'email_on_failure': False,
'email_on_retry': False,
'retries': 0,
'retry_delay': datetime.timedelta(minutes=5),
'start_date': datetime.datetime(2020, 1, 10),
}
with airflow.DAG(
'json_test2',
default_args=default_args,
# Not scheduled, trigger only
schedule_interval=None) as dag:
# Print the dag_run's configuration, which includes information about the
# Cloud Storage object change.
def read_json_file(file_path):
if os.path.exists(file_path):
with open(file_path, 'r') as f:
return json.load(f)
def get_run_list(files):
run_list = []
#The file is uploaded in the storage bucket used as a volume by Composer
last_exec_json = read_json_file("/home/airflow/gcs/data/last_execution.json")
date = last_exec_json["date"]
hour = last_exec_json["hour"]
for file in files:
#Testing by adding just date and hour
name = file['name']+f'_{date}_{hour}'
run_list.append(name)
return run_list
rl = get_run_list(products)
start = DummyOperator(task_id='start', dag=dag)
end = DummyOperator(task_id='end', dag=dag)
for name in rl:
tsk = DummyOperator(task_id=name, dag=dag)
start >> tsk >> end
【问题讨论】:
-
检查您的网络服务器日志,很可能将您的 DAG 导入 DAGag 时出错
-
您的回答描述了我遵循的完全相同的步骤,但它不起作用。我稍后会重试并更新您。
标签: python airflow google-cloud-composer orchestration