-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR introduces the DbtCloneOperator. For more details, refer to the dbt documentation: https://docs.getdbt.com/reference/commands/clone. ## Testing **Airflow DAG** ```python from datetime import datetime from airflow import DAG from cosmos import DbtSeedLocalOperator, DbtRunLocalOperator, DbtCloneLocalOperator, ProfileConfig DBT_PROJ_DIR="/usr/local/airflow/dbt/jaffle_shop" profile_config1=ProfileConfig( profile_name="bigquery_dev", target_name="dev", profiles_yml_filepath="/usr/local/airflow/dbt/jaffle_shop/profiles.yml" ) profile_config2=ProfileConfig( profile_name="bigquery_clone", target_name="dev", profiles_yml_filepath="/usr/local/airflow/dbt/jaffle_shop/profiles.yml" ) with DAG("test-id-1", start_date=datetime(2024, 1, 1), catchup=False) as dag: seed_operator = DbtSeedLocalOperator( profile_config=profile_config1, project_dir=DBT_PROJ_DIR, task_id="seed", dbt_cmd_flags=["--select", "raw_customers"], install_deps=True, append_env=True, ) run_operator = DbtRunLocalOperator( profile_config=profile_config1, project_dir=DBT_PROJ_DIR, task_id="run", dbt_cmd_flags=["--models", "stg_customers"], install_deps=True, append_env=True, ) clone_operator = DbtCloneLocalOperator( profile_config=profile_config2, project_dir=DBT_PROJ_DIR, task_id="clone", dbt_cmd_flags=["--models", "stg_customers", "--state", "/usr/local/airflow/dbt/jaffle_shop/target"], install_deps=True, append_env=True, ) seed_operator >> run_operator >> clone_operator ``` **DBT Profile** ``` bigquery_dev: target: dev outputs: dev: type: bigquery method: service-account project: astronomer-dag-authoring dataset: bq_dev threads: 4 # Must be a value of 1 or greater keyfile: /usr/local/airflow/include/key.json location: US bigquery_clone: target: dev outputs: dev: type: bigquery method: service-account project: astronomer-dag-authoring dataset: bq_clone threads: 4 # Must be a value of 1 or greater keyfile: /usr/local/airflow/include/key.json location: US ``` **Airflow DAG Run** <img width="1660" alt="Screenshot 2024-11-15 at 6 06 50 PM" src="https://github.com/user-attachments/assets/4a3af37e-3f6c-4859-814f-aeff3a252ac6"> **BQ data WH** <img width="1454" alt="Screenshot 2024-11-15 at 6 04 29 PM" src="https://github.com/user-attachments/assets/69b45f57-3ff5-43d9-a8f1-bb04e7ad5735"> ## Related Issue(s) closes: #1268 closes: #878 ## Breaking Change? No ## Limitation - The `dbt clone` command was introduced in dbt-core 1.6.0, so this feature is only available to users with dbt-core version 1.6 or higher https://github.com/dbt-labs/dbt-core/blob/1.6.latest/CHANGELOG.md - Users should ensure their database is supported for cloning operations.
- Loading branch information
1 parent
573a90a
commit 48055d8
Showing
22 changed files
with
231 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import os | ||
from datetime import datetime | ||
from pathlib import Path | ||
|
||
from airflow import DAG | ||
|
||
from cosmos import DbtCloneLocalOperator, DbtRunLocalOperator, DbtSeedLocalOperator, ProfileConfig | ||
|
||
DEFAULT_DBT_ROOT_PATH = Path(__file__).parent / "dbt" | ||
DBT_ROOT_PATH = Path(os.getenv("DBT_ROOT_PATH", DEFAULT_DBT_ROOT_PATH)) | ||
DBT_PROJ_DIR = DBT_ROOT_PATH / "jaffle_shop" | ||
DBT_PROFILE_PATH = DBT_PROJ_DIR / "profiles.yml" | ||
DBT_ARTIFACT = DBT_PROJ_DIR / "target" | ||
|
||
profile_config = ProfileConfig( | ||
profile_name="default", | ||
target_name="dev", | ||
profiles_yml_filepath=DBT_PROFILE_PATH, | ||
) | ||
|
||
with DAG("example_operators", start_date=datetime(2024, 1, 1), catchup=False) as dag: | ||
seed_operator = DbtSeedLocalOperator( | ||
profile_config=profile_config, | ||
project_dir=DBT_PROJ_DIR, | ||
task_id="seed", | ||
dbt_cmd_flags=["--select", "raw_customers"], | ||
install_deps=True, | ||
append_env=True, | ||
) | ||
run_operator = DbtRunLocalOperator( | ||
profile_config=profile_config, | ||
project_dir=DBT_PROJ_DIR, | ||
task_id="run", | ||
dbt_cmd_flags=["--models", "stg_customers"], | ||
install_deps=True, | ||
append_env=True, | ||
) | ||
|
||
# [START clone_example] | ||
clone_operator = DbtCloneLocalOperator( | ||
profile_config=profile_config, | ||
project_dir=DBT_PROJ_DIR, | ||
task_id="clone", | ||
dbt_cmd_flags=["--models", "stg_customers", "--state", DBT_ARTIFACT], | ||
install_deps=True, | ||
append_env=True, | ||
) | ||
# [END clone_example] | ||
|
||
seed_operator >> run_operator >> clone_operator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
.. _operators: | ||
|
||
Operators | ||
========= | ||
|
||
Cosmos exposes individual operators that correspond to specific dbt commands, which can be used just like traditional | ||
`Apache Airflow® <https://airflow.apache.org/>`_ operators. Cosmos names these operators using the format ``Dbt<dbt-command><execution-mode>Operator``. For example, ``DbtBuildLocalOperator``. | ||
|
||
Clone | ||
----- | ||
|
||
Requirement | ||
|
||
* Cosmos >= 1.8.0 | ||
* dbt-core >= 1.6.0 | ||
|
||
The ``DbtCloneLocalOperator`` implement `dbt clone <https://docs.getdbt.com/reference/commands/clone>`_ command. | ||
|
||
Example of how to use | ||
|
||
.. literalinclude:: ../../dev/dags/example_operators.py | ||
:language: python | ||
:start-after: [START clone_example] | ||
:end-before: [END clone_example] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.