如何从Airflow REST API中获取所有DAG的详细信息

huangapple go评论51阅读模式
英文:

How can i get all dags details from airflow rest api

问题

我在我的Airflow帐户中有更多的DAG(约150个DAG)。
我的Airflow版本= 2.4.0

我正在使用URL和查询参数
http://localhost:8081/api/v1/dags?limit=1000&offset=0

但我的响应只包含100条记录(DAG)。
我认为查询参数在这里不起作用。

我该如何获取所有可用的Airflow DAG?

英文:

i have more dags (say 150dags) in my airflow account.
My airflow version = 2.4.0

im using url and query parameter
http://localhost:8081/api/v1/dags?limit=1000&offset=0

But my response consists only 100 records(dags).
i think query parameters are not working here.

How can i fetch all dags available inside my airflow.

答案1

得分: 0

以下是您要翻译的内容:

有一个PR解释了限制不能被超越,如果您超过了限制,回退将是限制。

如果您想在API中更改限制,您可以将airflow.cfg中的"maximum_page_limit"更改为其他数字(默认= 100)。

另一个选项是玩偏移量,直到您不再在列表中获取到dags。例如:第一次调用限制= 100,偏移= 0,第二次调用限制= 100,偏移= 101,依此类推,直到获得空响应。

http://localhost:8081/api/v1/dags?limit=100&offset=0
http://localhost:8081/api/v1/dags?limit=100&offset=101

此外,在API中没有的选项是创建一个带有任务的dag,并使用DagBag获取所有dag的详细信息。

在这个示例中,我打印了所有dag的ID。

from datetime import datetime
from airflow import DAG, settings
from airflow.decorators import task
from airflow.models import DagBag

with DAG(
        dag_id="test_dag",
        schedule_interval=None,
        default_args={
            "start_date": datetime(2022, 1, 1),
            "retries": 0,
            "catchup": False,
        },
        render_template_as_native_obj=True,
        tags=["test"],
) as dag:
    dag.doc_md = __doc__

    @task
    def print_dags():
        dagbag = DagBag(settings.DAGS_FOLDER)
        print(dagbag.dags.keys())

    (print_dags())

<details>
<summary>英文:</summary>

there is a [PR][1] that explain that the limit can not be passed and if you put more then the limit the fallback would be the limit

if you want to change the limit in the api, you can change &quot;maximum_page_limit&quot; in airflow.cfg to other number (default = 100)

Another option is to play with offset until you do not get dags in the list. for example : first call limit=100, offset=0, second call limit=100, offset=101 and so on until empty response.


    http://localhost:8081/api/v1/dags?limit=100&offset=0
    http://localhost:8081/api/v1/dags?limit=100&offset=101

also, an option not in the api is to create a dag with a task and using DagBag to get all dags details.

in this example I print all the dag ids

    from datetime import datetime

    from airflow import DAG, settings
    from airflow.decorators import task
    from airflow.models import DagBag
    
    with DAG(
            dag_id=&quot;test_dag&quot;,
            schedule_interval=None,
            default_args={
                &quot;start_date&quot;: datetime(2022, 1, 1),
                &quot;retries&quot;: 0,
                &quot;catchup&quot;: False,
            },
            render_template_as_native_obj=True,
            tags=[&quot;test&quot;],
    ) as dag:
        dag.doc_md = __doc__
    
    
        @task
        def print_dags():
            dagbag = DagBag(settings.DAGS_FOLDER)
            print(dagbag.dags.keys())
    
    
        (print_dags())


  [1]: https://github.com/apache/airflow/pull/29773/files

</details>



huangapple
  • 本文由 发表于 2023年6月29日 16:04:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定