Création d'un plugin personnalisé pour Apache Airflow PythonVirtualenvOperator - Amazon Managed Workflows for Apache Airflow

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Création d'un plugin personnalisé pour Apache Airflow PythonVirtualenvOperator

L'exemple suivant explique comment appliquer un correctif à Apache Airflow PythonVirtualenvOperator avec un plugin personnalisé sur Amazon Managed Workflows for Apache Airflow.

Version

Vous pouvez utiliser l'exemple de code présenté sur cette page avec Apache Airflow v2 en Python 3.10 et Apache Airflow v3 en Python 3.11.

Prérequis

Pour utiliser l'exemple de code présenté sur cette page, vous aurez besoin des éléments suivants :

Autorisations

Aucune autorisation supplémentaire n'est requise pour utiliser l'exemple de code présenté sur cette page.

Prérequis

Pour utiliser l'exemple de code de cette page, ajoutez les dépendances suivantes à votrerequirements.txt. Pour en savoir plus, reportez-vous àInstallation des dépendances Python.

virtualenv

Exemple de code de plugin personnalisé

Apache Airflow exécutera le contenu des fichiers Python dans le dossier des plugins au démarrage. Ce plugin corrigera le module intégré PythonVirtualenvOperator au cours de ce processus de démarrage pour le rendre compatible avec Amazon MWAA. Les étapes suivantes affichent l'exemple de code du plugin personnalisé.

  1. Dans votre invite de commande, accédez au plugins répertoire de la section précédente. Exemples :

    cd plugins
  2. Copiez le contenu de l'exemple de code suivant et enregistrez-le localement sousvirtual_python_plugin.py.

    """ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from airflow.plugins_manager import AirflowPlugin import airflow.utils.python_virtualenv from typing import List def _generate_virtualenv_cmd(tmp_dir: str, python_bin: str, system_site_packages: bool) -> List[str]: cmd = ['python3','/usr/local/airflow/.local/lib/python3.7/site-packages/virtualenv', tmp_dir] if system_site_packages: cmd.append('--system-site-packages') if python_bin is not None: cmd.append(f'--python={python_bin}') return cmd airflow.utils.python_virtualenv._generate_virtualenv_cmd=_generate_virtualenv_cmd class VirtualPythonPlugin(AirflowPlugin): name = 'virtual_python_plugin'

Plugins.zip

Les étapes suivantes expliquent comment créer leplugins.zip.

  1. Dans votre invite de commande, accédez au répertoire figurant virtual_python_plugin.py dans la section précédente. Exemples :

    cd plugins
  2. Compressez le contenu de votre plugins dossier.

    zip plugins.zip virtual_python_plugin.py

Exemple de code

Les étapes suivantes décrivent comment créer le code DAG pour le plugin personnalisé.

  1. Dans votre invite de commande, accédez au répertoire dans lequel votre code DAG est stocké. Exemples :

    cd dags
  2. Copiez le contenu de l'exemple de code suivant et enregistrez-le localement sousvirtualenv_test.py.

    """ Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from airflow import DAG from airflow.operators.python import PythonVirtualenvOperator from airflow.utils.dates import days_ago import os os.environ["PATH"] = os.getenv("PATH") + ":/usr/local/airflow/.local/bin" def virtualenv_fn(): import boto3 print("boto3 version ",boto3.__version__) with DAG(dag_id="virtualenv_test", schedule_interval=None, catchup=False, start_date=days_ago(1)) as dag: virtualenv_task = PythonVirtualenvOperator( task_id="virtualenv_task", python_callable=virtualenv_fn, requirements=["boto3>=1.17.43"], system_site_packages=False, dag=dag, )

Options de configuration d'Airflow

Si vous utilisez Apache Airflow v2, ajoutez-le en core.lazy_load_plugins : False tant qu'option de configuration d'Apache Airflow. Pour en savoir plus, reportez-vous à la section Utilisation des options de configuration pour charger des plug-ins en 2.

Quelle est la prochaine étape ?