Skip to content

Latest commit

Β 

History

12 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›’ Real-Time E-Commerce insights and sales forecasting

Python Apache Kafka Microsoft Fabric Power BI Apache Spark Delta Lake

A production-grade, end-to-end real-time data streaming and analytics pipeline built on Microsoft Fabric β€” simulating live e-commerce order data from ingestion to interactive Power BI dashboards.

Architecture β€’ Tech Stack β€’ Pipeline Layers β€’ Getting Started β€’ Project Structure


πŸ“Œ Project Overview

This project implements a complete real-time streaming data pipeline for e-commerce insights and sales forecasting. Fake order data is continuously generated using Python and streamed via Apache Kafka into Microsoft Fabric Eventstream, where it flows through a Medallion Architecture (Bronze β†’ Silver β†’ Gold) before being served to a live Power BI dashboard.

The entire workflow is orchestrated by a Fabric Pipeline that can be scheduled or triggered on demand.


πŸ—οΈ Architecture

Project Architecture

projectarchitecture

Data Flow Diagram

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         DATA GENERATION                                 β”‚
β”‚                                                                         β”‚
β”‚   Python + Faker + Kafka Producer                                       β”‚
β”‚   └── Simulates real-time e-commerce orders (order_id, customer,        β”‚
β”‚       product, price, quantity, city, state, delivery_status ...)       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚ Kafka Topic
                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      MICROSOFT FABRIC EVENTSTREAM                       β”‚
β”‚                                                                         β”‚
β”‚   Source: Kafka Connection                                              β”‚
β”‚   Destination: Lakehouse (lh_ecommerce_orders)                          β”‚
β”‚   └── Streams data into Tables/dbo/stream_data (Delta format)           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚           MICROSOFT FABRIC LAKEHOUSE β€” MEDALLION ARCHITECTURE           β”‚
β”‚                                                                         β”‚
β”‚           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”‚
β”‚           β”‚   BRONZE    │───▢│   SILVER    │───▢│    GOLD    β”‚         β”‚
β”‚           β”‚             β”‚    β”‚             β”‚    β”‚             β”‚         β”‚
β”‚           β”‚ Raw ingestedβ”‚    β”‚ Cleaned &   β”‚    β”‚ Aggregated  β”‚         β”‚
β”‚           β”‚ orders with β”‚    β”‚ enriched    β”‚    β”‚ sales per   β”‚         β”‚
β”‚           β”‚ metadata    β”‚    β”‚ USA orders  β”‚    β”‚ state/minuteβ”‚         β”‚
β”‚           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β”‚
β”‚           bronze.orders       silver.orders      gold.orders            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   FABRIC WAREHOUSE (wh_ecommerce_orders)                β”‚
β”‚                                                                         β”‚
β”‚   dbo.gold_orders  ← MERGE from lh_ecommerce_orders.gold.orders         β”‚
β”‚   (Upsert: insert new rows, update existing aggregations)               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  SEMANTIC MODEL β†’ POWER BI DASHBOARD                    β”‚
β”‚                                                                         β”‚
β”‚   Real-time visuals: Sales by State, Orders by Category,                β”‚
β”‚   Revenue Trends, Delivery Status breakdown                             β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β–²
                            β”‚ Orchestrated by
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              FABRIC PIPELINE (pl_ecommerce_orders)                      β”‚
β”‚                                                                         β”‚
β”‚  stream_orders_to_bronze β†’ cleaned_values_to_silver β†’                   β”‚
β”‚  aggregated_to_gold β†’ warehouse_script                                  β”‚
β”‚                                                                         β”‚
β”‚  βœ… Schedulable   βœ… Monitored   βœ… Retriable                          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Lineage view

lineage


🧰 Tech Stack

Layer Technology
Data Simulation Python, Faker, kafka-python
Message Broker Apache Kafka
Ingestion Microsoft Fabric Eventstream
Storage Microsoft Fabric Lakehouse (OneLake / Delta Lake)
Processing PySpark (Structured Streaming)
Warehousing Microsoft Fabric Warehouse (T-SQL)
Orchestration Microsoft Fabric Pipeline
Semantic Layer Microsoft Fabric Semantic Model
Visualization Power BI (Real-time Dashboard)

πŸ”„ Pipeline Layers

1️⃣ Data Simulation β€” Kafka Producer

Generates realistic fake e-commerce orders continuously and publishes them to a Kafka topic.

# Key libraries
from faker import Faker
from kafka import KafkaProducer

# Simulated fields per order
{
    "order_id":        uuid,
    "timestamp":       datetime,
    "customer_id":     uuid,
    "product_id":      uuid,
    "category":        ["Electronics", "Clothing", "Books", "Toys", "Home Decor"],
    "price":           float,
    "quantity":        int,
    "total_amount":    float,
    "city":            str,
    "state":           str,
    "country":         str,
    "latitude":        float,
    "longitude":       float,
    "delivery_status": ["Processing", "Shipped", "Delivered", "Cancelled"]
}

2️⃣ Eventstream β€” Real-Time Ingestion

Microsoft Fabric Eventstream connects the Kafka topic as a source and the Lakehouse as a destination, writing incoming events directly into Tables/dbo/stream_data as a Delta table in real time.


3️⃣ Bronze Layer β€” stream_orders_to_bronze.ipynb

Reads from the live stream table and appends raw data to bronze.orders with added metadata columns.

df_orders = (
    df_raw
        .withColumn("ingested_at", current_timestamp())
        .withColumn("source", lit("eventstream"))
)

query = (
    df_orders.writeStream
        .format("delta")
        .outputMode("append")
        .option("checkpointLocation", bronze_checkpoint)
        .trigger(availableNow=True)
        .toTable("bronze.orders")
)
query.awaitTermination()

4️⃣ Silver Layer β€” cleaned_values_to_silver.ipynb

Cleans, validates and enriches the bronze data β€” filtering for USA orders, handling nulls, removing duplicates and computing total_amount.

df_clean = (
    df_bronze
        .withColumn("timestamp", to_timestamp("timestamp"))
        .withWatermark("timestamp", "1 minute")
        .withColumn("price",    when(col("price").isNull(),    0.0).otherwise(col("price")))
        .withColumn("quantity", when(col("quantity").isNull(), 1  ).otherwise(col("quantity")))
        .withColumn("total_amount", col("price") * col("quantity"))
        .dropDuplicates(["order_id", "timestamp"])
        .filter(col("country") == "USA")
        .filter(col("state").isNotNull())
)

5️⃣ Gold Layer β€” aggregated_to_gold.ipynb

Aggregates silver data into 1-minute tumbling windows per state β€” total sales revenue and total items sold.

df_gold = (
    df_silver
        .withWatermark("timestamp", "1 minute")
        .groupBy(window("timestamp", "1 minute"), "state")
        .agg(
            sum("total_amount").alias("total_sales"),
            sum("quantity").alias("total_items")
        )
        .select(
            col("window.start").alias("window_start"),
            col("window.end").alias("window_end"),
            "state", "total_sales", "total_items"
        )
)

Note: Uses outputMode("complete") since windowed aggregations require it.


6️⃣ Warehouse Script β€” warehouse_script

Syncs the gold layer into the Fabric Warehouse using an upsert (MERGE) β€” updating existing time-window records and inserting new ones.

MERGE dbo.gold_orders AS target
USING (SELECT * FROM lh_ecommerce_orders.gold.orders) AS source
ON (
    target.window_start = source.window_start AND
    target.window_end   = source.window_end   AND
    target.state        = source.state
)
WHEN MATCHED THEN
    UPDATE SET
        target.total_sales = source.total_sales,
        target.total_items = source.total_items
WHEN NOT MATCHED BY TARGET THEN
    INSERT (window_start, window_end, state, total_sales, total_items)
    VALUES (source.window_start, source.window_end, source.state,
            source.total_sales, source.total_items);

7️⃣ Orchestration β€” Fabric Pipeline

The pipeline pl_ecommerce_orders runs all four steps in sequence:

stream_orders_to_bronze
        ↓
cleaned_values_to_silver
        ↓
aggregated_to_gold
        ↓
warehouse_script

βœ… Can be manually triggered or scheduled βœ… Full run monitoring via Fabric Monitoring Hub βœ… Each activity shows duration, input, output and error details


8️⃣ Semantic Model + Power BI Dashboard

A Fabric Semantic Model connects to wh_ecommerce_orders and feeds a Power BI report with:

  • πŸ“Š Total sales revenue by state
  • πŸ“¦ Orders by product category
  • πŸ“ˆ Revenue trend over time windows
  • 🚚 Delivery status breakdown
  • πŸ—ΊοΈ Geographic sales map

dashboard


πŸ“ Project Structure

ecommerce-realtime-pipeline/
β”‚
β”œβ”€β”€ simulator/
β”‚   └── get_orders.py          # Fake order data generator (Faker + Kafka)
β”‚
β”œβ”€β”€ images/
β”‚    └── architecture.png
β”‚
β”œβ”€β”€ notebooks/
β”‚   β”œβ”€β”€ stream_orders_to_bronze.ipynb   # Bronze layer ingestion
β”‚   β”œβ”€β”€ cleaned_values_to_silver.ipynb  # Silver layer cleaning
β”‚   └── aggregated_to_gold.ipynb        # Gold layer aggregation
β”‚
β”œβ”€β”€ warehouse/
β”‚   └── warehouse_script.sql       # MERGE script for Fabric Warehouse
β”‚
β”œβ”€β”€ pipeline/
β”‚   └── pl_ecommerce_orders.json   # Fabric Pipeline definition (exported)
β”‚
β”œβ”€β”€ powerbi/
β”‚   └── ecommerce_orders_report.pbix
β”‚
β”œβ”€β”€ .env
β”œβ”€β”€ .gitignore
β”‚
└── README.md

πŸš€ Getting Started

Prerequisites

  • Microsoft Fabric workspace (with Lakehouse, Warehouse, Eventstream, Pipeline enabled)
  • Apache Kafka cluster (local or cloud)
  • Python 3.10+

1. Install Python Dependencies

pip install kafka-python faker

2. Start the Kafka Producer

python producer/kafka_producer.py

3. Set Up Fabric Eventstream

  • Create an Eventstream in your Fabric workspace
  • Add Kafka as the source (point to your topic)
  • Add Lakehouse (lh_ecommerce_orders) as the destination
  • Activate the Eventstream β€” data will land in Tables/dbo/stream_data

4. Create Lakehouse & Warehouse

  • Create Lakehouse: lh_ecommerce_orders
  • Create Warehouse: wh_ecommerce_orders
  • Add the Lakehouse as a linked source inside the Warehouse

5. Upload & Run Notebooks

Upload the three notebooks to your Fabric workspace and attach them to lh_ecommerce_orders as the default Lakehouse.

6. Create the Pipeline

Create a Fabric Pipeline pl_ecommerce_orders with these activities in order:

Order Activity Type
1 stream_orders_to_bronze Notebook
2 cleaned_values_to_silver Notebook
3 aggregated_to_gold Notebook
4 warehouse_script Warehouse Script

7. Connect Power BI

  • Create a Semantic Model pointing to wh_ecommerce_orders
  • Open Power BI Desktop or Fabric Power BI
  • Build your dashboard on top of dbo.gold_orders

βš™οΈ Key Design Decisions

Decision Reason
trigger(availableNow=True) + awaitTermination() Processes all available data then stops β€” safe for pipeline chaining
Checkpoints on every layer Prevents reprocessing old data on pipeline reruns
outputMode("complete") on Gold Required for windowed groupBy aggregations in Spark Structured Streaming
MERGE in Warehouse Upserts gold data safely β€” no duplicates, no data loss
Medallion Architecture Separates raw, clean and aggregated concerns for maintainability

πŸ“Š Data Flow Summary

Kafka Producer
    β†’ 1 order/second (configurable)
    β†’ Fabric Eventstream
    β†’ Lakehouse: dbo.stream_data (raw Delta)
    β†’ Bronze:    bronze.orders  (+ metadata)
    β†’ Silver:    silver.orders  (cleaned, USA only)
    β†’ Gold:      gold.orders    (1-min window aggregations by state)
    β†’ Warehouse: dbo.gold_orders (upserted)
    β†’ Power BI:  Live dashboard

πŸ™Œ Author

Pratik Salunkhe Built with Microsoft Fabric, PySpark, Apache Kafka and Power BI.


⭐ If you found this project helpful, give it a star!

About

A Real-time streaming data pipeline for e-commerce insights and sales forecasting, Fake order data is continuously generated using Python and streamed via Kafka into Microsoft Fabric Eventstream, where it flows through a Medallion Architecture before being served to a live Power BI dashboard

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages