prefect-dbt
, you can trigger and observe dbt Cloud jobs, execute dbt Core CLI commands, and incorporate other tools, such as Snowflake, into your dbt runs.
Prefect provides a global view of the state of your workflows and allows you to take action based on state changes.
Getting started
Prerequisites
- Prefect installed.
- A dbt Cloud account if using dbt Cloud.
Install prefect-dbt
Install or update to the latest version of the prefect-dbt library and dependencies.Register newly installed blocks types
Register the block types in the prefect-dbt module to make them available for use.dbt Cloud
If you have an existing dbt Cloud job, use the pre-built flowrun_dbt_cloud_job
to trigger a job run and wait until the job run is finished.
If some nodes fail, run_dbt_cloud_job
efficiently retries the unsuccessful nodes.
Prior to running this flow, save your dbt Cloud credentials to a DbtCloudCredentials block:
Save dbt Cloud credentials to a block
Blocks can be created through code or through the UI. To create a dbt Cloud Credentials block:- Go to your dbt Cloud profile.
- Log in to your dbt Cloud account.
- Scroll to API or click API Access on the sidebar.
- Copy the API Key.
- Click Projects on the sidebar.
- Copy the account ID from the URL:
https://cloud.getdbt.com/settings/accounts/<ACCOUNT_ID>
. - Create and run the following script, replacing the placeholders:
- Navigate to your dbt home page.
- On the top nav bar, click on Deploy -> Jobs.
- Select a job.
- Copy the job ID from the URL:
https://cloud.getdbt.com/deploy/<ACCOUNT_ID>/projects/<PROJECT_ID>/jobs/<JOB_ID>
- Create and run the following script, replacing the placeholders.
dbt Core
Prefect-dbt supports execution of dbt Core CLI commands. If you don’t have aDbtCoreOperation
block saved, create one and set the commands that you want to run.
Optionally, specify the project_dir
.
If profiles_dir
is not set, the DBT_PROFILES_DIR
environment variable will be used.
If DBT_PROFILES_DIR
is not set, the default directory will be used $HOME/.dbt/
.
Use an existing profile
If you have an existing dbtprofiles.yml
file, specify the profiles_dir
where the file is located:
Use environment variables with Prefect secret blocks
If you use environment variables inprofiles.yml
, set a Prefect Secret block as an environment variable:
profiles.yml
file could then access that variable.
Programmatic Invocation
prefect-dbt
has some pre-built tasks that use dbt’s programmatic invocation. For example:
Create a summary artifact
These pre-built tasks can also create artifacts. These artifacts have extra information about dbt Core runs, such as messages and compiled code for nodes that fail or have errors.
Create a new profile with blocks
Use a DbtCliProfile block to createprofiles.yml
.
Then, specify profiles_dir
where profiles.yml
will be written.
Here’s example code with placeholders:
Supplying the
dbt_cli_profile
argument will overwrite existing profiles.yml
filesIf you already have a profiles.yml
file in the specified profiles_dir
, the file will be overwritten. If you do not specify a profiles directory, profiles.yml
at ~/.dbt/
would be overwritten.TargetConfigs
blocks.
If the desired service profile is not available, you can build one from the generic TargetConfigs
class.
BigQuery profile example
To create dbt Core target config and profile blocks for BigQuery:- Save and load a
GcpCredentials
block. - Determine the schema / dataset you want to use in BigQuery.
- Create a short script, replacing the placeholders.
- Determine the dbt commands you want to run.
- Create a short script, replacing the placeholders.
Resources
For assistance using dbt, consult the dbt documentation. Refer to theprefect-dbt
API documentation linked in the sidebar to explore all the capabilities of the prefect-dbt library.
Additional installation options
Additional installation options for dbt Core with BigQuery, Snowflake, and Postgres are shown below.Additional functionality for dbt Core and Snowflake profiles
To install for Prefect 3, include the--pre
flag.