Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DBT Test #1429

Open
AkashSonu14 opened this issue Dec 26, 2024 · 2 comments
Open

DBT Test #1429

AkashSonu14 opened this issue Dec 26, 2024 · 2 comments
Labels
area:testing Related to testing, like unit tests, integration tests, etc dbt:test Primarily related to dbt test command or functionality question Further information is requested

Comments

@AkashSonu14
Copy link

Hi,
How to orchestrate a custom test in the dbt project in the airflow give dag using cosmos and dbt_project.yml structure

Copy link

dosubot bot commented Dec 26, 2024

To orchestrate a custom test in a dbt project using Airflow with a DAG, Cosmos, and the dbt_project.yml structure, follow these steps:

  1. Set Up the Environment: Create a virtual environment in your Dockerfile to install dbt. This helps avoid dependency conflicts between dbt and Airflow.

    FROM my-image:latest
    
    # install dbt into a virtual environment
    RUN python -m venv dbt_venv && source dbt_venv/bin/activate && \
        pip install --no-cache-dir <your-dbt-adapter> && deactivate
  2. Install Cosmos: Add astronomer-cosmos to your requirements.txt to use Cosmos for running dbt projects as Airflow DAGs.

  3. Organize Your dbt Project: Move your dbt project into the dags directory of your Airflow setup. The structure should look like this:

    ├── dags/
    │   ├── dbt/
    │   │   └── my_dbt_project/
    │   │       ├── dbt_project.yml
    │   │       ├── models/
    │   │       │   ├── my_model.sql
    │   │       │   └── my_other_model.sql
    │   │       └── macros/
    │   │           ├── my_macro.sql
    │   │           └── my_other_macro.sql
    │   └── my_cosmos_dag.py
    ├── Dockerfile
    ├── requirements.txt
    └── ...
    
  4. Create the DAG: In the my_cosmos_dag.py file, import the DbtDag class from Cosmos and create a new DAG instance. Use the dbt_executable_path to point to the virtual environment.

    import os
    from datetime import datetime
    from cosmos import DbtDag, ProjectConfig, ProfileConfig, ExecutionConfig
    from cosmos.profiles import PostgresUserPasswordProfileMapping
    
    profile_config = ProfileConfig(
        profile_name="default",
        target_name="dev",
        profile_mapping=PostgresUserPasswordProfileMapping(
            conn_id="airflow_db",
            profile_args={"schema": "public"},
        ),
    )
    
    execution_config = ExecutionConfig(
        dbt_executable_path=f"{os.environ['AIRFLOW_HOME']}/dbt_venv/bin/dbt",
    )
    
    my_cosmos_dag = DbtDag(
        project_config=ProjectConfig(
            dbt_project_path=f"{os.environ['AIRFLOW_HOME']}/dags/dbt/my_dbt_project",
        ),
        profile_config=profile_config,
        execution_config=execution_config,
        schedule_interval="@daily",
        start_date=datetime(2023, 1, 1),
        catchup=False,
        dag_id="my_cosmos_dag",
        default_args={"retries": 2},
    )
  5. Start Your Project: Use astro dev start to start your project. You should see your Airflow DAG in the Airflow UI, where you can trigger it.

This setup will allow you to orchestrate your dbt project as an Airflow DAG using Cosmos, with the configuration specified in your dbt_project.yml. If you encounter a DagBag import timeout error, consider increasing the core.dagbag_import_timeout value in your Airflow configuration [1][2][3].

To continue talking to Dosu, mention @dosu.


Help Dosu learn! Give it feedback: Great Response | Irrelevant Answer | Incorrect Sources | Too Verbose | Hallucination | Other

@dosubot dosubot bot added area:testing Related to testing, like unit tests, integration tests, etc dbt:test Primarily related to dbt test command or functionality labels Dec 26, 2024
@tatiana
Copy link
Collaborator

tatiana commented Dec 27, 2024

@AkashSonu14 some follow up questions?

  1. What type of custom test are you trying to run?
  2. How would you run locally?
  3. Did you face any particular issues when running it using Cosmos?

@tatiana tatiana added the question Further information is requested label Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:testing Related to testing, like unit tests, integration tests, etc dbt:test Primarily related to dbt test command or functionality question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants