dbt models

How to create DBT Models

In this post we will explain how to create DBT models, we will continue the hospital example and we will transform the data from bronze schema and load to silver schema, first we will analyze the data, make some queries filter and transform the raw data from Bronze schema.

After filter the silver schema, we will create models to golden schema to show in our dashboards.

We will use macros in our models maybe i will explain in other post.

Default Models

As a default we found 2 models:

  • my_first_dbt_model.sql
  • my_second_dbt_model.sql
default dbt models

my_first_dbt_model.sql :

my_second_dbt_model.sql :

schema.yml :

If we execute dbt run to see this on the database :

dbt run default model

We can see the table and view created:

DBeaver view to see table and view of default models

Dev Container + DBT + Postgresql + Metabase Part 4

Default macros

In our project we want to create every model in a schema that have name of the folder that contain the models. we have to modify the default macro :

macros/generate_schema_name.sql

In this macro concatenate the default schema in our case bronze with the custom_schema_name = folder name, we will delete the concatenation

Now we have to create 3 folders each for one schema:

  • bronze
  • silver
  • gold
dbt Medallion Architecture folders schemas

dbt_project.yml

Then we have to modify the dbt_project.yml file, to add the 3 schemas :

We can see in the silver section of dbt_proj, the property +materializad: table , we can put view or materialized view, instead create a table create a view and in +schema : we can put the name of the schema like silver or plata

source.yml

We have to create source file to define the tables and schema.

patients_clean.sql

We have to clean the patient table, for that we create the file patients_clean.sql, and filter the patient_id is not empty, to select the table patients in bronze schema we have to write the table and schema like this : {{ source(‘bronze’, ‘patients’) }}, we transform the name to change the string of the name

We execute dbt run

Then we check in the database on DBeaver the result:

DBeaver view to see postgres tables of dbt execution

Finally we see the table patients_clean like the name of the file sql.

To summarize the steps to create the models :

  • modify generate_schema_name.sql
  • edit dbt_project.yml
  • create folders with the name of the schema: bronze, silver and gold
  • add source.yml
  • create the sql model file in our case patients_clean.sql
  • execute dbt run

Links:

https://docs.getdbt.com/reference/model-configs

Comments

Leave a Reply