Creating dynamic tasks in airflow (in composer) based on bigquery response

怎甘沉沦 提交于 2019-12-11 16:25:01

问题


I am trying to create a airflow DAG which generates task depending on the response from server.

Here is my approach :

getlist of tables from bigquery -> loop through the list and create tasks

This is my latest code and I have tried all possible code found in stack overflow. Nothing seems to work. What am I doing wrong?

with models.DAG(dag_id="xt", default_args=default_args, schedule_interval="0 1 * * *", catchup=True) as dag:
tables = get_tables_from_bq()

    bridge = DummyOperator(
        task_id='bridge',
        dag=dag
    )


    for t in tables:
        sql = ("SELECT * FROM `{project}.{dataset}.{table}` LIMIT 5;".format(
                project=project, dataset=dataset, table=t))

    materialize_t = BigQueryOperator(bql=sql,
                                     destination_dataset_table=dataset+'.' + table_prefix + t,
                                     task_id = 'x_' + t,
                                     bigquery_conn_id = 'bigquery_default',
                                     use_legacy_sql = False,
                                     write_disposition = 'WRITE_APPEND',
                                     create_disposition = 'CREATE_IF_NEEDED',
                                     query_params = {},
                                     allow_large_results = True,
                                     dag = dag)

bridge >> materialize_t

Even the run option is not showing with this code. I tried multiple codes and finally reached here but still no luck. Any help???


回答1:


I don't know if it is a typo in the copy and paste of the DAG but tables = get_tables_from_bq() should be before with models.DAG(...) Also, bridge >> materialize_t seems to miss indentation and therefore be outside the with models.DAG(...) scope. On a side note, you do not need the bridge task.



来源:https://stackoverflow.com/questions/57689837/creating-dynamic-tasks-in-airflow-in-composer-based-on-bigquery-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!