Setting up dbt with Snowflake

Sr. Data Engineer working mostly on Data and Observability problems. Writing mostly about Data and cloud, sometimes productivity and other musings.
Search for a command to run...

Sr. Data Engineer working mostly on Data and Observability problems. Writing mostly about Data and cloud, sometimes productivity and other musings.
No comments yet. Be the first to comment.
In today's data-driven world, real-time data processing and analytics have become crucial for businesses to stay competitive. Apache Hudi (Hadoop Upserts and Incremental) is an open-source data management framework that provides efficient data ingest...

Introduction Large Language Models (LLMs) have revolutionized the field of Natural Language Processing (NLP). These models, such as GPT-4, are designed to understand and generate human-like text. In this post, we will delve into how to work with LLMs...

Credit card fraud is a significant concern for financial institutions, as it can lead to considerable monetary losses and damage customer trust. Real-time fraud detection systems are essential for identifying and preventing fraudulent transactions as...

In a production ETL (extract, transform, load) pipeline, it is often helpful to manage environment variables to store sensitive information, such as database credentials or API keys. This allows you to keep this sensitive information separate from yo...

A Comprehensive Guide to Migrating from Redshift to BigQuery Migrating your data from Amazon Redshift to Google BigQuery can be a significant undertaking, but with careful planning and execution, it can lead to enhanced performance and scalability fo...

dbt (data build tool) is an open-source command-line tool that helps data analysts and data engineers automate the process of transforming and loading data from various sources into a data warehouse. In this tutorial, we will be setting up dbt with Snowflake, a popular cloud-based data warehouse.
Prerequisites
A Snowflake account
Python 3 and pip installed on your machine
dbt installed on your machine (instructions can be found here)
Setting up dbt with Snowflake
CREATE DATABASE my_database;
USE DATABASE my_database;
CREATE SCHEMA my_schema;
CREATE ROLE my_dbt_role;
GRANT SELECT ON SCHEMA my_schema TO ROLE my_dbt_role;
GRANT INSERT ON SCHEMA my_schema TO ROLE my_dbt_role;
GRANT UPDATE ON SCHEMA my_schema TO ROLE my_dbt_role;
GRANT DELETE ON SCHEMA my_schema TO ROLE my_dbt_role;
GRANT CREATE PROCEDURE ON SCHEMA my_schema TO ROLE my_dbt_role;
CREATE WAREHOUSE my_warehouse
WITH
AUTO_SUSPEND = 3600
AUTO_RESUME = TRUE
MIN_CLUSTER_COUNT = 1
MAX_CLUSTER_COUNT = 3
SCALING_POLICY = standard;
CREATE USER my_dbt_user PASSWORD = 'my_password';
GRANT USAGE ON WAREHOUSE my_warehouse TO USER my_dbt_user;
GRANT SELECT ON DATABASE my_database TO USER my_dbt_user;
Creating a dbt project
dbt init
This will create a new dbt project and generate the necessary files and directories.
profiles.yml file in the ~/.dbt directory and add the following content to it, replacing the placeholders with your own Snowflake account, role, user, and password:my_profile:
outputs:
my_database:
type: snowflake
account: <your_snowflake_account>
role: my_dbt_role
user: my_dbt_user
password: <your_password>
warehouse: my_warehouse
database: my_database
schema: my_schema
This will create a new dbt profile called my_profile that can be used to connect to your Snowflake database.
Writing dbt models
dbt models are SQL scripts that define the transformations and calculations to be performed on your data. They can be written in either Jinja or pure SQL.
Here is an example of a dbt model written in Jinja:
Copy code{{
config(
materialized='view',
unique_key='id'
)
}}
select *
from {{ ref('my_table') }}
This model simply selects all columns from a table called my_table and materializes the result as a view.
Here is an example of a dbt model written in pure SQL:
create or replace view {{ this }} as
select *,
upper(name) as name_upper
from {{ ref('my_table') }}
This model selects all columns from my_table and adds an additional column called name_upper that contains the uppercase version of the name column.
Running dbt
To run your dbt project and execute the models, run the following command:
dbt run
This will execute all of the models in your project and create the necessary tables and views in your Snowflake database.
You can also run specific models by specifying their names:
dbt run --models my_model_1 my_model_2
You can also use the dbt test command to verify that your models are producing the expected results.
Conclusion
In this tutorial, we learned how to set up dbt with Snowflake and how to use it to automate the process of transforming and loading data into a data warehouse. We also saw some examples of how to write dbt models and run them in a dbt project. I hope this helps you get started with dbt and Snowflake!