How to get uri from connection_id inside a python script in Airflow?

Viewed 1556

I have been following this official documentation https://airflow.apache.org/docs/apache-airflow/stable/howto/connection.html to try to set up connections in Airflow. I want to access the uri of the connections in my python scripts so that I can work with my databases.

airflow connections get sqlite_default in bash gives me details of the connection. I want to do a similar thing inside python script. How can it be possible?

I have saved my airflow connections from the UI as is mentioned in the documentation.

1 Answers

For anyone trying to figure this out, I was able to get the uri with the following steps :

  1. https://airflow.apache.org/docs/apache-airflow/stable/howto/connection.html shows how to store connections in Airflow
  2. To get the connection uri
from airflow.hooks.base_hook import BaseHook
BaseHook.get_connection(CONN_ID).get_uri()

assuming CONN_ID is the name of the saved connection.

Related