# prefect-dbt **Repository Path**: amplitude/prefect-dbt ## Basic Information - **Project Name**: prefect-dbt - **Description**: No description available - **Primary Language**: Python - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-01-09 - **Last Updated**: 2024-01-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Coordinate and use dbt in your dataflow with prefect-dbt
Visit the full docs [here](https://PrefectHQ.github.io/prefect-dbt) to see additional examples and the API reference. With prefect-dbt, you can easily trigger and monitor dbt Cloud jobs, execute dbt Core CLI commands, and incorporate other services, like Snowflake, into your dbt runs! Check out the examples below to get started! ## Getting Started Be sure to install [prefect-dbt](#installation) and [save a block](#saving-credentials-to-block) to run the examples below! ### Integrate dbt Cloud jobs with Prefect flows If you have an existing dbt Cloud job, take advantage of the flow, `run_dbt_cloud_job`. This flow triggers the job and waits until the job run is finished. If certain nodes fail, `run_dbt_cloud_job` efficiently retries the specific, unsuccessful nodes. ```python from prefect import flow from prefect_dbt.cloud import DbtCloudJob from prefect_dbt.cloud.jobs import run_dbt_cloud_job @flow def run_dbt_job_flow(): result = run_dbt_cloud_job( dbt_cloud_job=DbtCloudJob.load("my-block-name"), targeted_retries=5, ) return result run_dbt_job_flow() ``` ### Integrate dbt Core CLI commands with Prefect flows `prefect-dbt` also supports execution of dbt Core CLI commands. To get started, if you don't have a `DbtCoreOperation` block already saved, set the commands that you want to run; it can include a mix of dbt and non-dbt commands. Then, optionally specify the `project_dir`. If `profiles_dir` is unset, it will try to use the `DBT_PROFILES_DIR` environment variable. If that's also not set, it will use the default directory `$HOME/.dbt/`. #### Using an existing profile If you already have an existing dbt profile, specify the `profiles_dir` where `profiles.yml` is located. ```python from prefect import flow from prefect_dbt.cli.commands import DbtCoreOperation @flow def trigger_dbt_flow() -> str: result = DbtCoreOperation( commands=["pwd", "dbt debug", "dbt run"], project_dir="PROJECT-DIRECTORY-PLACEHOLDER", profiles_dir="PROFILES-DIRECTORY-PLACEHOLDER" ).run() return result trigger_dbt_flow() ``` #### Writing a new profile To setup a new profile, first [save and load a DbtCliProfile block](#saving-credentials-to-block) and use it in `DbtCoreOperation`. Then, specify `profiles_dir` where `profiles.yml` will be written. ```python from prefect import flow from prefect_dbt.cli import DbtCliProfile, DbtCoreOperation @flow def trigger_dbt_flow(): dbt_cli_profile = DbtCliProfile.load("DBT-CORE-OPERATION-BLOCK-NAME-PLACEHOLDER") with DbtCoreOperation( commands=["dbt debug", "dbt run"], project_dir="PROJECT-DIRECTORY-PLACEHOLDER", profiles_dir="PROFILES-DIRECTORY-PLACEHOLDER", dbt_cli_profile=dbt_cli_profile, ) as dbt_operation: dbt_process = dbt_operation.trigger() # do other things before waiting for completion dbt_process.wait_for_completion() result = dbt_process.fetch_result() return result trigger_dbt_flow() ``` ## Resources If you need help getting started with or using dbt, please consult the [dbt documentation](https://docs.getdbt.com/docs/building-a-dbt-project/documentation). ### Installation To use `prefect-dbt` with dbt Cloud: ```bash pip install prefect-dbt ``` To use dbt Core (CLI): ```bash pip install "prefect-dbt[cli]" ``` To use dbt Core with Snowflake profiles: ```bash pip install "prefect-dbt[snowflake]" ``` To use dbt Core with BigQuery profiles: ```bash pip install "prefect-dbt[bigquery]" ``` To use dbt Core with Postgres profiles: ```bash pip install "prefect-dbt[postgres]" ``` !!! warning "Some dbt Core profiles require additional installation" According to dbt's [Databricks setup page](https://docs.getdbt.com/reference/warehouse-setups/databricks-setup), users must first install the adapter: ```bash pip install dbt-databricks ``` Check out the [desired profile setup page](https://docs.getdbt.com/reference/profiles.yml) on the sidebar for others. Requires an installation of Python 3.7+. We recommend using a Python virtual environment manager such as pipenv, conda or virtualenv. These tasks are designed to work with Prefect 2. For more information about how to use Prefect, please refer to the [Prefect documentation](https://orion-docs.prefect.io/). ### Saving credentials to block Note, to use the `load` method on Blocks, you must already have a block document [saved through code](https://orion-docs.prefect.io/concepts/blocks/#saving-blocks) or [saved through the UI](https://orion-docs.prefect.io/ui/blocks/). !!! info "Registering blocks" Register blocks in this module to [view and edit them](https://orion-docs.prefect.io/ui/blocks/) on Prefect Cloud: ```bash prefect block register -m prefect_dbt ``` A list of available blocks in `prefect-dbt` and their setup instructions can be found [here](https://PrefectHQ.github.io/prefect-dbt/blocks_catalog). #### dbt Cloud To create a dbt Cloud credentials block: 1. Head over to your [dbt Cloud profile](https://cloud.getdbt.com/settings/profile). 2. Login to your dbt Cloud account. 3. Scroll down to "API" or click "API Access" on the sidebar. 4. Copy the API Key. 5. Click Projects on the sidebar. 6. Copy the account ID from the URL: `https://cloud.getdbt.com/settings/accounts/