Cloud Composer 1 è in modalità post-manutenzione. Google non rilascia ulteriori aggiornamenti a Cloud Composer 1, incluse nuove versioni di Airflow, correzioni di bug e aggiornamenti della sicurezza. Ti consigliamo di pianificare la migrazione a Cloud Composer 2.
Questa pagina spiega come connettersi a un'istanza Cloud SQL che esegue il database Airflow del tuo ambiente Cloud Composer ed eseguire query SQL.
Ad esempio, potresti voler eseguire query direttamente sul database Airflow, eseguire il backup del database, raccogliere statistiche in base ai contenuti del database o recuperare altre informazioni personalizzate dal database.
Prima di iniziare
Esegui una query SQL sul database Airflow
Per connetterti al database Airflow:
Crea un DAG con uno o più operatori PostgresOperator. Per iniziare,
puoi utilizzare il DAG di esempio.
Nel parametro sql dell'operatore, specifica la query SQL.
Ad esempio, puoi attivare il DAG manualmente o attendere che venga eseguito in base a una pianificazione.
DAG di esempio:
importdatetimeimportosimportairflowfromairflow.providers.postgres.operators.postgresimportPostgresOperatorSQL_DATABASE=os.environ["SQL_DATABASE"]withairflow.DAG("airflow_db_connection_example",start_date=datetime.datetime(2024,1,1),schedule_interval=None,catchup=False)asdag:PostgresOperator(task_id="run_airflow_db_query",dag=dag,postgres_conn_id="airflow_db",database=SQL_DATABASE,sql="SELECT * FROM dag LIMIT 10;",)
Esegui il dump dei contenuti del database e trasferiscili in un bucket
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-04-02 UTC."],[[["This page outlines how to connect to and query the Cloud SQL instance that hosts the Airflow database for Cloud Composer environments."],["While direct access to the Airflow database is possible, it is generally recommended to utilize the Airflow REST API or CLI commands instead."],["You can execute SQL queries on the Airflow database by creating a DAG with `PostgresOperator` operators and specifying your SQL query in the `sql` parameter, while setting schedule intervals accordingly to prevent multiple runs."],["Directly adding custom tables or modifying the schema of the Airflow database is strictly prohibited."],["Backing up the Airflow database contents should be done using snapshots rather than dumping database contents to a bucket."]]],[]]