【问题标题】:Airflow branch errors with object is not iterable对象的气流分支错误不可迭代
【发布时间】:2020-11-02 00:58:25
【问题描述】:

我正在尝试让 BranchPythonOperator 工作,但出现以下错误:

'BigQueryInsertJobOperator' object is not iterable

这是我的分支操作员:

branching = BranchPythonOperator(
        task_id='branching',
        python_callable=return_branch,
        provide_context=True)

这是我的 Python 可调用对象:

def return_branch(ds, **kwargs):
    execution_year = kwargs['execution_date'].strftime("%Y")
    type = dataset_metadata[f'{execution_year}']['var']
    if type == 'foo':
        return x
    return y

x 和 y 是 BigQueryInsertJobOperator:

x = BigQueryInsertJobOperator(
    task_id='x',
    configuration={
        "query": {
            "query": "{% include 'q.sql' %}",
            "use_legacy_sql": False
            }
        },
    dag=dag)

【问题讨论】:

  • 你在哪里定义x?里面return_branch?
  • @lionbigcat 我已经编辑了我的问题,希望它更清楚。

标签: airflow


【解决方案1】:

我想参考this answer。您的方法return_branch 不应返回运算符。它必须返回您的运营商的task_id。你会得到这样的东西:

def return_branch(ds, **kwargs):
    next_task_id = "a" # <some kind of logic>

    return next_task_id


branching = BranchPythonOperator(
    task_id="pick_query",
    python_callable=return_branch,
    provide_context=True,
)

option_1 = DummyOperator(task_id="a")
option_2 = DummyOperator(task_id="b")

branching >> [option_1, option_2]

【讨论】:

  • 好的。谢谢。我只是感到困惑,因为我的任务和任务 ID 具有相同的名称,并且我的分支不是返回字符串而是返回运算符本身。
猜你喜欢
  • 2011-10-04
  • 2011-12-13
  • 2016-09-11
  • 2019-01-12
  • 1970-01-01
  • 2013-07-02
  • 2018-02-02
  • 2017-06-22
  • 2014-10-09
相关资源
最近更新 更多