Skip to main content

a friendly airflow dag build tool

Project description

from airflow.utils.task_group import TaskGroupfrom airflow import DAG

DAG Tool

A Friendly Airflow DAG Build Tool for Data Engineer with YAML file template.

[!WARNING] This project will reference the DAG generate code from the Astronomer: DAG-Factory. But I replace some logic that fit with ETL propose for Data Engineer.

[!NOTE] Disclaimer: This project will override all necessary parameters that should pass to Airflow object with ETL context for Data Engineer use-case. So, if you want to use and enhance this project, you can fork this project anytime without notice me.

Feature Supported:

  • โœ… JSON Schema Validation
  • ๐Ÿ” Passing environment variable
  • ๐Ÿ’š Allow Passing Airflow Template

From my opinion, a data Engineer should focus on the user requirement instead of focusing on the Python code when it need creates a new DAG in our Airflow application.

So, this project focus for this plain to make sure that all DAG can readable and easy to maintain with the same standard when we want to scale up and out the Airflow application support 10 to 1000 DAGs.

File Structure:

dags/
โ”œโ”€โ”€ { domain }/
โ”‚     โ”œโ”€โ”€ { module-dags }/
โ”‚     โ”‚     โ”œโ”€โ”€ __init__.py
โ”‚     โ”‚     โ”œโ”€โ”€ dag.yml
โ”‚     โ”‚     โ”œโ”€โ”€ variables.yml
โ”‚     โ”‚     โ””โ”€โ”€ assets/
โ”‚     โ”‚         โ”œโ”€โ”€ dag-schema-mapping.json
โ”‚     โ”‚         โ””โ”€โ”€ dag-transform-query.sql
โ”‚     โ”‚
โ”‚     โ””โ”€โ”€ { module-dags }/
โ”‚           โ”œโ”€โ”€ __init__.py

[!NOTE] I think this project should support multiple DAGs structure like:

dags/
โ”œโ”€โ”€ { domain }/
โ”‚     โ”œโ”€โ”€ { module-dags }/
โ”‚     โ”‚     โ”œโ”€โ”€ __init__.py
โ”‚     โ”‚     โ”œโ”€โ”€ dag-{ name-1 }.yml
โ”‚     โ”‚     โ”œโ”€โ”€ dag-{ name-2 }.yml
โ”‚     โ”‚     โ”œโ”€โ”€ variables.yml
โ”‚     โ”‚     โ””โ”€โ”€ assets/
โ”‚     โ”‚         โ”œโ”€โ”€ dag-case-1-schema-mapping.json
โ”‚     โ”‚         โ”œโ”€โ”€ dag-case-1-transform-query.sql
โ”‚     โ”‚         โ”œโ”€โ”€ dag-case-2-schema-mapping.json
โ”‚     โ”‚         โ””โ”€โ”€ dag-case-2-transform-query.sql
โ”‚     โ”‚
โ”‚     โ””โ”€โ”€ { module-dags }/
โ”‚           โ”œโ”€โ”€ __init__.py

Execution Flow:

The flow of this project provide the interface Pydantic Model before passing it to Airflow objects.

S --> Template --> Pydantic Model --> DAG/Operator Objects --> Execute --> E

CI Flow:

S --> DAGs      --> GitSync     --> Airflow K8s Pod
  --> Variables --> API Sync    --> Airflow Variables
  --> Assets    --> CI Merge

๐Ÿ“ฆ Installation

[!WARNING] This package does not publish to PyPI yet.

uv pip install -U dagtool
Airflow Version Supported Noted
2.7.1 โœ… This is the first Airflow version that this project supported.
>=2.7.1,<3.0.0 โŒ Common version support for Airflow version 2.x.x
>=3.x.x โŒ Common version support for Airflow version 3.x.x

๐ŸŽฏ Usage

This DAG generator engine need you define the dag.yml file and set engine object to get the current path on __init__.py file.

DAG Template

[!NOTE] If you want to dynamic environment config on the dag.yaml file, you can use a variable.yaml file for dynamic value that marking on config template via macro function, {{ vars('keystore-on-dag-name') }}.

On the dag-transaction.yml file:

name: transaction
schedule: "@daily"
owner: "de-oncall@email.com,de@email.com"
catchup: "{{ vars('catchup') }}"
tags:
  - "domain:sales"
  - "tier:1"
  - "schedule:daily"
tasks:
  - task: start
    op: empty

  - group: etl_master
    upstream: start
    tasks:
      - type: extract
        op: python
        caller: get_api_data
        params:
          path: gcs://{{ vars("PROJECT_ID") }}/sales/master/date/{ exec_date:%y }

      - task: transform
        upstream: extract
        op: operator
        uses: gcs_transform_data
        params:
          path: gcs://{{ vars("PROJECT_ID") }}/landing/master/date/{ exec_date:%y }

      - task: sink
        upstream: transform
        op: custom
        uses: write_iceberg
        params:
          path: gcs://{{ vars("PROJECT_ID") }}

  - task: end
    upstream: etl_master
    op: empty

On the __inti__.py file:

"""# SALES DAG

This DAG will extract data from Google Cloud Storage to Google BigQuery LakeHouse
via DuckDB engine.

> This DAG is the temp DAG for ingest data to GCP.
"""
from dagtool import DagTool, BaseTask, Context

from airflow.models import DAG
from airflow.utils.dates import days_ago
from airflow.utils.task_group import TaskGroup
from airflow.operators.empty import EmptyOperator

# NOTE: Some external provider operator object.
from airflow.providers.google.cloud.operators import GCSTransformData

# NOTE: Some function that want to use with PythonOperator
def get_api_data(path: str):
    return {}

# NOTE: Some custom task that create any Airflow Task instance object.
class WriteIceberg(BaseTask):
    path: str

    def build(
        self,
        dag: DAG | None = None,
        task_group: TaskGroup | None = None,
        context: Context | None = None,
    ) -> TaskGroup:
        with TaskGroup(
            group_id="write_iceberg",
            parent_group=task_group,
            dag=dag,
        ) as tg:
            t1 = EmptyOperator(task_id="prepare", dag=dag)
            t2 = EmptyOperator(task_id="write", dag=dag)
            t2.set_upstream(t1)
        return tg


tool = DagTool(
    name="sales",
    path=__file__,
    docs=__doc__,
    operators={"gcs_transform_data": GCSTransformData},
    python_callers={"get_api_data": get_api_data},
    tasks={"write_iceberg": WriteIceberg},
)
tool.build_airflow_dags_to_globals(
    gb=globals(),
    default_args={"start_date": days_ago(2)},
)

Output:

The DAG that was built from this package will have the name is, sales_transaction.

๐Ÿ’ฌ Contribute

I do not think this project will go around the world because it has specific propose, and you can create by your coding without this project dependency for long term solution. So, on this time, you can open the GitHub issue on this project :raised_hands: for fix bug or request new feature if you want it.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dagtool-0.0.4.tar.gz (32.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dagtool-0.0.4-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file dagtool-0.0.4.tar.gz.

File metadata

  • Download URL: dagtool-0.0.4.tar.gz
  • Upload date:
  • Size: 32.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for dagtool-0.0.4.tar.gz
Algorithm Hash digest
SHA256 de0893bf3099ef3a37cc8721aa97394c56fc45b51dbdb008452c10052c370df8
MD5 f686b7c11518e822e0bf577c09285707
BLAKE2b-256 5eba8e133c3c3ec2109f3aeba54887f6bb5a2cc6aef986cc2e9d658e5e7b98a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for dagtool-0.0.4.tar.gz:

Publisher: publish.yml on ddeutils/dagtool

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dagtool-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: dagtool-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for dagtool-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1bfee028d730457c3e8b3431c41f75f5db99f1b8617e3d5d0805ddb08d63932a
MD5 9b4da292e45dde806a778671528e85a8
BLAKE2b-256 0d35a6af2fdb3dd1a2253be0ed9231cdc1dd4ab5584487dc76fc3fe0314e0f7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for dagtool-0.0.4-py3-none-any.whl:

Publisher: publish.yml on ddeutils/dagtool

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page