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

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

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,依此类推,直到获得空响应。

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

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

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

  1. from datetime import datetime
  2. from airflow import DAG, settings
  3. from airflow.decorators import task
  4. from airflow.models import DagBag
  5. with DAG(
  6. dag_id="test_dag",
  7. schedule_interval=None,
  8. default_args={
  9. "start_date": datetime(2022, 1, 1),
  10. "retries": 0,
  11. "catchup": False,
  12. },
  13. render_template_as_native_obj=True,
  14. tags=["test"],
  15. ) as dag:
  16. dag.doc_md = __doc__
  17. @task
  18. def print_dags():
  19. dagbag = DagBag(settings.DAGS_FOLDER)
  20. print(dagbag.dags.keys())
  21. (print_dags())
  1. <details>
  2. <summary>英文:</summary>
  3. 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
  4. 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)
  5. 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.
  6. http://localhost:8081/api/v1/dags?limit=100&offset=0
  7. http://localhost:8081/api/v1/dags?limit=100&offset=101
  8. also, an option not in the api is to create a dag with a task and using DagBag to get all dags details.
  9. in this example I print all the dag ids
  10. from datetime import datetime
  11. from airflow import DAG, settings
  12. from airflow.decorators import task
  13. from airflow.models import DagBag
  14. with DAG(
  15. dag_id=&quot;test_dag&quot;,
  16. schedule_interval=None,
  17. default_args={
  18. &quot;start_date&quot;: datetime(2022, 1, 1),
  19. &quot;retries&quot;: 0,
  20. &quot;catchup&quot;: False,
  21. },
  22. render_template_as_native_obj=True,
  23. tags=[&quot;test&quot;],
  24. ) as dag:
  25. dag.doc_md = __doc__
  26. @task
  27. def print_dags():
  28. dagbag = DagBag(settings.DAGS_FOLDER)
  29. print(dagbag.dags.keys())
  30. (print_dags())
  31. [1]: https://github.com/apache/airflow/pull/29773/files
  32. </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:

确定