Подключение к Airflow через CLI
Подключение
Airflow предоставляет интерфейс командной строки, реализованный в виде скрипта bin/airflow, который расположен в директории /opt/airflow/bin.
Хотя рекомендуется управлять Airflow через ADCM или веб-интерфейс Airflow, CLI предоставляет доступ к большему количеству возможностей при работе с Airflow.
Чтобы взаимодействовать с Airflow через CLI, подключитесь к хосту кластера с компонентом Airflow через SSH и запустите скрипт, выполнив:
$ sudo /opt/airflow/bin/airflow
Пример вывода:
Usage: airflow [-h] GROUP_OR_COMMAND ...
Positional Arguments:
GROUP_OR_COMMAND
Groups:
celery Celery components
config View configuration
connections Manage connections
dags Manage DAGs
db Database operations
jobs Manage jobs
kubernetes Tools to help run the KubernetesExecutor
pools Manage pools
providers Display providers
roles Manage roles
tasks Manage tasks
users Manage users
variables Manage variables
Commands:
cheat-sheet Display cheat sheet
dag-processor Start a standalone Dag Processor instance
info Show information about current Airflow and environment
kerberos Start a kerberos ticket renewer
plugins Dump information about loaded plugins
rotate-fernet-key
Rotate encrypted connection credentials and variables
scheduler Start a scheduler instance
standalone Run an all-in-one copy of Airflow
sync-perm Update permissions for existing roles and optionally DAGs
triggerer Start a triggerer instance
version Show the version
webserver Start a Airflow webserver instance
Options:
-h, --help show this help message and exit
Подробное описание каждой команды доступно на странице Airflow CLI.
Чтобы протестировать работу CLI, запросите версию Airflow:
$ sudo /opt/airflow/bin/airflow version
Вывод:
2.6.3
Чтобы увидеть справку по определенной команде, используйте опцию --help:
$ sudo /opt/airflow/bin/airflow dags --help
Команды имеют следующий синтаксис:
$ airflow [GROUP_OR_COMMAND] [SUB_COMMAND] [OPTIONS]
Здесь:
-
GROUP_OR_COMMAND— команда Airflow CLI или группа команд (напримерdags,jobsи т. д.). -
SUB_COMMAND— команда, относящаяся к группе команд, например,dags list. -
OPTIONS— параметры выбранной команды.
Примеры использования
Airflow предоставляет несколько инструментов, одним из которых является команда dags, которая позволяет управлять DAG с помощью CLI.
Например, чтобы получить список существующих DAG, выполните команду:
$ sudo /opt/airflow/bin/airflow dags list
Пример вывода:
dag_id | filepath | owner | paused
========================================================+====================================================================================+=========+=======
dataset_consumes_1 | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/example_datasets.py | airflow | None
dataset_consumes_1_and_2 | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/example_datasets.py | airflow | None
dataset_consumes_1_never_scheduled | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/example_datasets.py | airflow | None
dataset_consumes_unknown_never_scheduled | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/example_datasets.py | airflow | None
dataset_produces_1 | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/example_datasets.py | airflow | None
dataset_produces_2 | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/example_datasets.py | airflow | None
example_bash_operator | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/example_bash_operat | airflow | None
| or.py | |
example_branch_datetime_operator | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/example_branch_date | airflow | None
| time_operator.py | |
example_branch_datetime_operator_2 | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/example_branch_date | airflow | None
| time_operator.py | |
example_branch_datetime_operator_3 | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/example_branch_date | airflow | None
| time_operator.py | |
example_kubernetes_executor | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/example_kubernetes_ | airflow | None
| executor.py | |
example_local_kubernetes_executor | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/example_local_kuber | airflow | None
| netes_executor.py | |
example_nested_branch_dag | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/example_nested_bran | airflow | None
| ch_dag.py | |
example_params_trigger_ui | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/example_params_trig | airflow | None
| ger_ui.py | |
tutorial | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/tutorial.py | airflow | None
tutorial_dag | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/tutorial_dag.py | airflow | None
tutorial_taskflow_api | /opt/airflow/lib/python3.10/site-packages/airflow/example_dags/tutorial_taskflow_a | airflow | None
| pi.py | |
Чтобы выгрузить список DAG в файл, используйте опцию --output и укажите один из следующих форматов в качестве аргумента: json, yaml, plain, table.
Чтобы запустить DAG, используйте команду trigger:
$ sudo /opt/airflow/bin/airflow dags trigger tutorial
Здесь tutorial — это ID того DAG, который нужно запустить.