PlusMagi's Blog By Pitt Phunsanit Data Engineering Automate ETL/ELT Pipelines with Apache Airflow and dbt

Automate ETL/ELT Pipelines with Apache Airflow and dbt

In the era where data is the most powerful asset, organizations face the challenge of managing massive amounts of complex data. Traditional data management methods are no longer sufficient. Senior Data Engineers must design a data ecosystem that not only collects data but also operates automatically, reliably, and is ready for business decisions in real-time.

💡 Transitioning to ELT: The Heart of Data Modernization

The key to upgrading data systems is transitioning from the traditional ETL (Extract, Transform, Load) approach to the ELT (Extract, Load, Transform) paradigm. The ELT approach involves loading raw data into a large-scale data warehouse, such as PostgreSQL, Snowflake, or BigQuery, followed by transforming the data directly within the warehouse. Using PostgreSQL as the primary database allows us to fully leverage the power of computing and the flexibility of SQL for data management.

⚙️ Apache Airflow: The Data Workflow Orchestrator

After the raw data is loaded into PostgreSQL, the next step is to ensure that the entire process occurs systematically and automatically. Apache Airflow plays the role of ‘orchestrator’ in this workflow. It manages and schedules the tasks, ensuring that the data pipeline runs smoothly and efficiently. Here is an example of a simple DAG (Directed Acyclic Graph) in Apache Airflow:

SQL

from airflow import DAG<br>from airflow.operators.python_operator import PythonOperator<br>from datetime import datetime<br><br>def extract_data():<br>    # Example extraction logic<br>    print('Extracting data...')<br><br>def load_data():<br>    # Example loading logic<br>    print('Loading data into PostgreSQL...')<br><br>def transform_data():<br>    # Example transformation logic<br>    print('Transforming data in PostgreSQL...')<br><br>default_args = {<br>    'owner': 'airflow',<br>    'start_date': datetime(2023, 1, 1)<br>}<br><br>dag = DAG(<br>    'data_pipeline',<br>    default_args=default_args,<br>    schedule_interval='@daily'<br>)<br><br>extract_task = PythonOperator(<br>    task_id='extract_data',<br>    python_callable=extract_data,<br>    dag=dag<br>)<br><br>load_task = PythonOperator(<br>    task_id='load_data',<br>    python_callable=load_data,<br>    dag=dag<br>)<br><br>transform_task = PythonOperator(<br>    task_id='transform_data',<br>    python_callable=transform_data,<br>    dag=dag<br>)<br><br>extract_task >> load_task >> transform_task

🛠️ dbt: Transforming Data with SQL

dbt (data build tool) is a command-line tool that allows data engineers to transform data using SQL. It provides a framework for writing, testing, and deploying data transformations. Here is an example of a simple dbt model:

SQL

version: 2<br>models:<br>  - name: transformed_data<br>    description: 'Transformed data model'<br>    columns:<br>      - name: id<br>        description: 'Primary key'<br>        data_type: integer<br>      - name: value<br>        description: 'Transformed value'<br>        data_type: float<br>    config:<br>      materialized: table<br>    sql: |<br>      SELECT id, value * 2 AS transformed_value<br>      FROM raw_data

Conclusion

Automating ETL/ELT pipelines with Apache Airflow and dbt allows data engineers to manage large-scale data efficiently and reliably. By adopting the ELT paradigm and leveraging the power of PostgreSQL, data engineers can transform data directly within the data warehouse, taking full advantage of SQL’s capabilities. Apache Airflow orchestrates the workflow, ensuring that the entire process runs smoothly, while dbt provides a robust framework for writing and deploying data transformations.