Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 39

Building a Gold Delta Lake for Analytics

This lesson is built with resources from youtube.com, medium.com and 1 more site.

Hello! Welcome back to our course on practical data architecture.

In our last lesson, we built the middle-tier of our pipeline, transforming raw bronze data into a cleansed and enriched silver layer. This silver layer now serves as our reliable, queryable "single source of truth."

Today, we will complete the final stage of the Medallion Architecture. Our learning outcome is to implement a pipeline to aggregate silver data into a gold Delta table for analytics. This is where the data is specifically prepared for end-users, like business analysts and data scientists, to drive insights and reporting.

1. The Purpose of the Gold Layer

While the silver layer is clean and detailed, it's often too granular for high-level business reporting. Imagine trying to create a quarterly revenue chart by querying billions of individual transaction records—it would be slow and inefficient. The Gold layer solves this by providing pre-aggregated, business-level views of the data.

The key characteristics of the Gold layer are:

  • Aggregated: Data is summarized (e.g., daily sales per store, monthly active users).
  • Business-Oriented: Tables are often organized into data marts or star schemas that reflect business processes.
  • Optimized for Analytics: The structure is designed for fast, efficient queries by BI tools and data scientists.

This image provides a great overview of where the Gold layer fits and who consumes it.

Medallion Architecture in the Lakehouse
This diagram illustrates the full Medallion Architecture. Note how the Gold layer feeds directly into BI & Reporting, Streaming Analytics, and Data Science & ML consumption patterns.

To solidify this concept, let's hear a concise explanation of the "why" behind each layer, with a focus on the Gold layer's role in making data consumable.

What is Medallion Architecture?

This short video from the 'intricity101' channel clearly explains the purpose of each layer in the Medallion architecture.

Please watch from 01:56 to 03:33. Pay attention to how the speaker describes the transition from the Silver layer (application-pristine) to the Gold layer, where data is aggregated into structures like 'conformed dimensions and measures' to drive insights.


2. Key Transformations from Silver to Gold

Moving data from silver to gold involves specific transformations designed to prepare it for its final use. These transformations are less about cleaning (which was done in the previous step) and more about reshaping the data for analysis.

A good summary of these steps can be found in the article "Data Transformation: From Silver to Gold Layer in AdventureWorks".

Data Transformation: From Silver to Gold Layer in ...

This article outlines the typical steps involved in creating a Gold layer. We'll focus on its high-level summary of transformations.

Please read the introductory section before the code examples (from the start down to just before '1. Updating the process_data.py File'). Focus on the four bullet points: Data Aggregation, Dimensional Modeling, Data Enrichment, and Performance Optimization.

As the article highlights, the primary transformation is data aggregation. This is the process we will implement today.


3. Implementing the Silver-to-Gold Pipeline

Let's build a pipeline to create a gold table. The core logic involves using a GROUP BY clause in a SQL query to aggregate data from a silver table.

Imagine we have a silver.cleaned_sales_data table (like the one we designed in our last lesson) with individual sales records. A common requirement would be to create a gold table with total revenue per product category, per day.

The SQL logic would look like this:

SELECT
  order_date,
  category,
  SUM(revenue) as total_revenue,
  COUNT(sale_id) as total_orders
FROM
  silver.cleaned_sales_data
GROUP BY
  order_date,
  category;

Now, how do we operationalize this in a pipeline?

Option 1: Using a Standard CREATE TABLE AS SELECT (CTAS)

For simple, non-streaming use cases, you can create a gold table by simply running a CTAS statement. The article "Data Transformation: From Silver to Gold Layer..." shows this pattern where SQL queries are defined and then executed to create the final tables.

Data Transformation: From Silver to Gold Layer in ...

Let's examine the code from the same article. It provides clear, practical examples of SQL queries used for aggregation.

Review the code block under the heading 'Definition of Tables for Gold Processing'. Notice how each query uses functions like COUNT(*) and a GROUP BY clause to create aggregated tables from a silver source.

Option 2: Using a Delta Live Tables (DLT) Materialized View

For a more robust and scalable solution that integrates with the DLT pipeline we started in the last lesson, we can define our gold table as a Materialized View. DLT will handle the incremental updates automatically as new data arrives in the silver layer.

The Databricks documentation provides a perfect example of this.

Tutorial: Build an ETL pipeline using change data capture

This Databricks tutorial demonstrates creating an aggregated materialized view within a DLT pipeline, which is the recommended approach for this architecture.

Please read 'Step 7: Create a materialized view...'. You can review either the Python or SQL version. Focus on how the code reads from a silver-layer table (customers_history), uses groupBy() (or GROUP BY), and applies aggregate functions like count() to create a new gold-layer table (customers_history_agg).

Here is the SQL syntax from that tutorial, which you could add to your DLT pipeline:

CREATE OR REPLACE MATERIALIZED VIEW customers_history_agg
AS
SELECT
  id,
  count("address") as address_count,
  count("email") AS email_count
FROM customers_history
GROUP BY id

This is a powerful pattern. You declaratively define the aggregation, and Databricks manages the infrastructure and computation to keep this materialized view up-to-date efficiently.

Test your understanding!

You have a silver table named silver.employee_activity with the columns: employee_id, activity_date, project_id, and hours_worked.

Your BI team needs a gold table named gold.daily_project_hours that shows the total hours worked by all employees on each project for each day. Write the DLT SQL query to create this as a materialized view.

Show answer
CREATE OR REPLACE MATERIALIZED VIEW gold.daily_project_hours
COMMENT "Aggregated daily hours per project."
AS
SELECT
  activity_date,
  project_id,
  SUM(hours_worked) as total_hours
FROM
  live.silver.employee_activity -- 'live' is used to reference other tables in the DLT pipeline
GROUP BY
  activity_date,
  project_id;

4. An Architect's View: Views vs. Materialized Views

From your perspective as an architect, it's crucial to understand the distinction between a standard VIEW and a MATERIALIZED VIEW in the gold layer.

  • A standard VIEW is just a stored query. Every time you query the view, the aggregation logic is re-executed against the source data. This is great for real-time data but can be slow if the aggregation is complex or the underlying data is massive.
  • A MATERIALIZED VIEW (or a standard table created via CTAS) pre-computes and stores the aggregated results. Queries against it are extremely fast because the work has already been done. The trade-off is storage cost and a slight data latency, as the view needs to be refreshed.

For most gold layer use cases in analytics and BI, materialized views or pre-aggregated tables are the preferred choice for performance.

DATABRICKS - From Bronze to Silver/Gold - How to promote to Silver/Gold in Medallion architecture

This video discusses the difference between views and materialized views in the context of creating a gold layer.

Watch from 23:10 to 25:30. The speaker explains the concept of stored views versus materialized views and then shows how to create them. Note his comment that creating a materialized view in Databricks now points to using Delta Live Tables, reinforcing the pattern we just learned.


Conclusion

Excellent work! You have now implemented a complete, three-layer Medallion data pipeline. You can ingest raw data into Bronze, clean and enrich it into Silver, and now, aggregate it into business-ready Gold tables for high-performance analytics.

Key Takeaways:

  • The Gold layer serves aggregated, business-centric data optimized for analytics and reporting.
  • The primary transformation is aggregation using GROUP BY with functions like SUM(), COUNT(), AVG(), etc.
  • In a Databricks environment, you can implement this using a MATERIALIZED VIEW within a Delta Live Tables pipeline for an efficient, incrementally updating solution.
  • From an architectural standpoint, materialized views are often preferred over standard views in the gold layer to ensure fast query performance for BI tools.

In our next lesson, we will address a very common and important data warehousing problem: how to handle changes to dimension data over time. We will explore the concept of Slowly Changing Dimensions (SCDs) and learn how to implement an SCD Type 2 pipeline using Delta Lake's powerful MERGE capabilities.