This project implements an ETL (Extract, Transform, Load) pipeline using Apache Airflow to extract weather data from an external API (WeatherAPI), transform it, and then load it into a PostgreSQL database. It also includes a task for printing the transformed data to logs. This pipeline is useful for regularly updating a database with the latest weather information.
- Python 3.x
- Apache Airflow
- PostgreSQL
- Python Libraries:
pendulum,requests,psycopg2,json,os - A
.envfile for environment variables
-
Environment Variables: Set up your
.envfile with the following variables:API_KEY=your_weatherapi_key USER_PASSWORD=your_postgres_password USER=your_postgres_username DB_NAME=your_postgres_dbname -
Python Dependencies: Install the required Python libraries (listed in
requirements.txt). -
Airflow Setup: Configure Apache Airflow in your environment. Ensure Airflow is installed and properly configured to run DAGs.
-
Database Setup: Ensure that PostgreSQL is installed and running. Create a table named
temperaturein your PostgreSQL database with appropriate columns (location,temp_c,wind_kph,time).
- DAG ID:
ETLWeatherPostgresAndPrint - Start Date: January 1, 2024
- Schedule Interval: None (manual triggering)
- Tags:
Loading weatherAPI to PostgresSQL with Airflow
-
Extract: Connects to WeatherAPI using the provided API key, queries for weather data for a specified location (Berlin in this case), and extracts the JSON response.
-
Transform: Processes the JSON response to extract relevant information (location, wind, temperature, and time) and converts it into a suitable format for database insertion.
-
Load: Connects to the PostgreSQL database and inserts the transformed data into the
temperaturetable. -
Query and Print: Logs the transformed weather data. This is useful for debugging and monitoring purposes.
To run the DAG, ensure your Airflow environment is active and the DAG is correctly placed in your Airflow's DAG directory. Trigger the DAG manually through the Airflow UI. i attached a docker compose file you can spin up on docker to get your airflow and postgres running.
- This pipeline is designed for educational and demonstration purposes and may require modifications for production use.
- Ensure that your API key and database credentials are kept secure and not exposed in your code.