Skip to content

astronomer/astronomer-cosmos

Repository files navigation

image


fury ossrank downloads pre-commit.ci status

Run your dbt Core projects as Apache Airflow DAGs and Task Groups with a few lines of code. Benefits include:

  • Run dbt projects against Airflow connections instead of dbt profiles
  • Native support for installing and running dbt in a virtual environment to avoid dependency conflicts with Airflow
  • Run tests immediately after a model is done to catch issues early
  • Utilize Airflow's data-aware scheduling to run models immediately after upstream ingestion
  • Turn each dbt model into a task/task group complete with retries, alerting, etc.

Quickstart

Check out the Getting Started guide on our docs. See more examples at /dev/dags and at the cosmos-demo repo.

Example Usage

You can render a Cosmos Airflow DAG using the DbtDag class. Here's an example with the jaffle_shop project:

"""
An example DAG that uses Cosmos to render a dbt project.
"""
import os
from datetime import datetime
from pathlib import Path
from cosmos import DbtDag, ProjectConfig, ProfileConfig
from cosmos.profiles import PostgresUserPasswordProfileMapping
DEFAULT_DBT_ROOT_PATH = Path(__file__).parent / "dbt"
DBT_ROOT_PATH = Path(os.getenv("DBT_ROOT_PATH", DEFAULT_DBT_ROOT_PATH))
profile_config = ProfileConfig(
profile_name="default",
target_name="dev",
profile_mapping=PostgresUserPasswordProfileMapping(
conn_id="airflow_db",
profile_args={"schema": "public"},
),
)
# [START local_example]
basic_cosmos_dag = DbtDag(
# dbt/cosmos-specific parameters
project_config=ProjectConfig(
DBT_ROOT_PATH / "jaffle_shop",
),
profile_config=profile_config,
operator_args={
"install_deps": True, # install any necessary dependencies before running any dbt command
"full_refresh": True, # used only in dbt commands that support this flag
},
# normal dag parameters
schedule_interval="@daily",
start_date=datetime(2023, 1, 1),
catchup=False,
dag_id="basic_cosmos_dag",
default_args={"retries": 2},
)
# [END local_example]

This will generate an Airflow DAG that looks like this:

Community

  • Join us on the Airflow Slack at #airflow-dbt

Changelog

We follow Semantic Versioning for releases. Check CHANGELOG.rst for the latest changes.

Contributing Guide

All contributions, bug reports, bug fixes, documentation improvements, enhancements are welcome.

A detailed overview an how to contribute can be found in the Contributing Guide.

As contributors and maintainers to this project, you are expected to abide by the Contributor Code of Conduct.

License

Apache License 2.0