The Medallion architecture helps organize data in layers to make it easier to clean, improve, and analyze step by step.
Medallion architecture (bronze, silver, gold) in Apache Spark
bronzeDF = spark.read.format('json').load('/path/bronze') silverDF = bronzeDF.filter('quality = true') goldDF = silverDF.groupBy('category').agg({'sales': 'sum'})
Bronze layer stores raw or minimally processed data.
Silver layer contains cleaned and filtered data.
Gold layer holds aggregated or business-level data ready for use.
bronzeDF = spark.read.json('/data/bronze') silverDF = bronzeDF.dropDuplicates() goldDF = silverDF.groupBy('product').sum('revenue')
bronzeDF = spark.read.format('csv').load('/data/bronze') silverDF = bronzeDF.filter("status = 'active'") goldDF = silverDF.select('user_id', 'purchase_amount')
This example shows how to start with raw data (bronze), filter good quality rows (silver), and then sum sales by product (gold).
from pyspark.sql import SparkSession spark = SparkSession.builder.appName('MedallionExample').getOrCreate() # Bronze layer: load raw data bronzeDF = spark.createDataFrame([ (1, 'apple', 10, True), (2, 'banana', 5, False), (3, 'apple', 15, True), (4, 'banana', 7, True) ], ['id', 'product', 'sales', 'quality']) # Silver layer: filter only good quality data silverDF = bronzeDF.filter('quality = true') # Gold layer: aggregate sales by product from pyspark.sql.functions import sum as sum_ goldDF = silverDF.groupBy('product').agg(sum_('sales').alias('total_sales')) goldDF.show()
Each layer builds on the previous one to improve data quality and usefulness.
Bronze data is often large and raw; silver is cleaner; gold is ready for business use.
Using this architecture helps teams work together and track data changes easily.
The Medallion architecture organizes data into bronze, silver, and gold layers.
Bronze is raw data, silver is cleaned data, and gold is aggregated or business-ready data.
This approach makes data easier to manage and analyze step by step.
Practice
bronze layer in the Medallion architecture?Solution
Step 1: Understand the role of the bronze layer
The bronze layer is the first stage where raw data is ingested without transformation.Step 2: Differentiate from silver and gold layers
Silver contains cleaned data, and gold contains aggregated data, so they are not raw.Final Answer:
Store raw, unprocessed data as it arrives -> Option CQuick Check:
Bronze = Raw data [OK]
- Confusing bronze with silver or gold layers
- Thinking bronze stores aggregated data
- Assuming bronze is cleaned data
Solution
Step 1: Recall the Medallion architecture flow
Data flows from raw to cleaned to aggregated: bronze, then silver, then gold.Step 2: Match the order to options
Bronze -> Silver -> Gold correctly lists bronze first, then silver, then gold.Final Answer:
Bronze -> Silver -> Gold -> Option AQuick Check:
Order = Bronze, Silver, Gold [OK]
- Reversing the order of layers
- Mixing silver and bronze positions
- Assuming gold comes before silver
bronzeDF = spark.read.format('json').load('/data/raw/events')
silverDF = bronzeDF.filter('event_type IS NOT NULL').dropDuplicates()
goldDF = silverDF.groupBy('event_type').count()Solution
Step 1: Analyze each DataFrame's transformation
bronzeDF loads raw JSON data (raw data = bronze). silverDF filters and removes duplicates (cleaned data = silver). goldDF groups and counts (aggregated data = gold).Step 2: Identify the layer of goldDF
goldDF performs aggregation, which matches the gold layer's purpose.Final Answer:
goldDF is gold layer -> Option AQuick Check:
Aggregation = Gold layer [OK]
- Confusing raw load as silver
- Thinking filtering is aggregation
- Mixing up silver and gold transformations
Solution
Step 1: Understand where duplicates should be removed
Duplicates should be removed during cleaning, which happens in the silver layer.Step 2: Evaluate options for removing duplicates
Adding.dropDuplicates()in silver layer after cleaning transformations is the correct fix.Final Answer:
Add.dropDuplicates()after cleaning transformations in silver layer -> Option BQuick Check:
Remove duplicates in silver layer [OK]
- Removing duplicates too early in bronze layer
- Using groupBy instead of dropDuplicates
- Filtering duplicates only in gold layer
Solution
Step 1: Identify the dashboard data needs
The dashboard requires aggregated, business-ready data (total sales by region).Step 2: Match data needs to Medallion layers
Gold layer contains aggregated and business-level data, ideal for dashboards.Final Answer:
Query the gold layer for aggregated sales data -> Option DQuick Check:
Dashboard needs gold layer data [OK]
- Using raw or cleaned data instead of aggregated
- Querying bronze layer directly for reports
- Filtering bronze layer instead of using gold
Ready to prove it?
Pass the test and this pattern moves unknown
