a friendly airflow dag build tool
Project description
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 (Set IDE with
json-schema.json) - ๐ Allow Passing Variable to DAG Template before build
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
๐ฆ Installation
uv pip install -U dagtool
| Airflow Version | Supported | Noted |
|---|---|---|
>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 |
[!NOTE] I recommend to use Airflow2 util Airflow3 stable.
๐ฏ 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.yamlfile, you can use avariable.yamlfile 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"
start_date: "{{ vars('start_date') }}"
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 | fmt('%y') }}
- task: transform
upstream: extract
op: operator
operator_name: gcs_transform_data
params:
path: gcs://{{ vars("project_id") }}/landing/master/date/{{ exec_date | fmt('%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 Factory, TaskModel, 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 MockGCSTransformData
from pydantic import Field
# NOTE: Some function that want to use with PythonOperator
def get_api_data(path: str) -> dict[str, list[str]]:
return {"data": [f"src://{path}/table/1", f"src://{path}/table/2"]}
# NOTE: Some custom task that create any Airflow Task instance object.
class WriteIceberg(TaskModel):
"""Custom Task for user defined inside of template path."""
path: str = Field(description="An Iceberg path.")
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
factory = Factory(
name="sales",
path=__file__,
docs=__doc__,
operators={"gcs_transform_data": MockGCSTransformData},
python_callers={"get_api_data": get_api_data},
tasks={"write_iceberg": WriteIceberg},
)
factory.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.
[!NOTE] On the
variables.ymlfile:type: variable variables: - key: transaction stages: dev: start_date: "2025-01-01" catchup: false project_id: "sales_project_dev"
๐ฌ 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dagtool-0.0.9.tar.gz.
File metadata
- Download URL: dagtool-0.0.9.tar.gz
- Upload date:
- Size: 33.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be2b3c5d210f3526d3d3f1f256fa4d9397d6afced8e1481b8ac3e2db7b338922
|
|
| MD5 |
c84baf550a7fe223f4231156b699c898
|
|
| BLAKE2b-256 |
78f97fa26ae59c470122355e0b3a346705326bbf93e2e3bce8e2ca354269d6e0
|
Provenance
The following attestation bundles were made for dagtool-0.0.9.tar.gz:
Publisher:
publish.yml on ddeutils/dagtool
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dagtool-0.0.9.tar.gz -
Subject digest:
be2b3c5d210f3526d3d3f1f256fa4d9397d6afced8e1481b8ac3e2db7b338922 - Sigstore transparency entry: 456656207
- Sigstore integration time:
-
Permalink:
ddeutils/dagtool@00075ea8ffe541264e0f393ac302be8d05ea517b -
Branch / Tag:
refs/tags/v0.0.9 - Owner: https://github.com/ddeutils
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@00075ea8ffe541264e0f393ac302be8d05ea517b -
Trigger Event:
release
-
Statement type:
File details
Details for the file dagtool-0.0.9-py3-none-any.whl.
File metadata
- Download URL: dagtool-0.0.9-py3-none-any.whl
- Upload date:
- Size: 24.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1dbf1bfbf1c7cbd7c6c3ae9d8745113121d2dcb038cbe7ce5cd55012fd4495fa
|
|
| MD5 |
b6ac835d2cabe32a9c474956322e7282
|
|
| BLAKE2b-256 |
964a946d910e830a12249bd4e037fedeb770540c1cf7896b72b1518225859cd2
|
Provenance
The following attestation bundles were made for dagtool-0.0.9-py3-none-any.whl:
Publisher:
publish.yml on ddeutils/dagtool
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dagtool-0.0.9-py3-none-any.whl -
Subject digest:
1dbf1bfbf1c7cbd7c6c3ae9d8745113121d2dcb038cbe7ce5cd55012fd4495fa - Sigstore transparency entry: 456656236
- Sigstore integration time:
-
Permalink:
ddeutils/dagtool@00075ea8ffe541264e0f393ac302be8d05ea517b -
Branch / Tag:
refs/tags/v0.0.9 - Owner: https://github.com/ddeutils
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@00075ea8ffe541264e0f393ac302be8d05ea517b -
Trigger Event:
release
-
Statement type: