How do we architect a complete end-to-end PySpark pipeline combining schema enforcement, deduplication, broadcast joins, ranking, and partitioned export?
The comprehensive PySpark Developer Capstone combining all core Data Engineering concepts in a single production pipeline.
Enterprise-grade high-throughput data processing pipelines deployed on Databricks, EMR, and Google Cloud Dataproc.
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, StringType, DoubleType, TimestampType
from pyspark.sql.window import Window
from pyspark.sql.functions import broadcast, col, count, current_timestamp, rank, round, row_number, sum, trim, upper
# 1. Initialize Distributed Session
spark = SparkSession.builder.appName("EnterpriseDataEngineeringPipeline").getOrCreate()
# 2. Strict Schema Ingestion
event_schema = StructType([
StructField("event_id", StringType(), False),
StructField("account_id", StringType(), False),
StructField("amount", DoubleType(), True),
StructField("event_time", TimestampType(), True)
])
raw_events = spark.read.schema(event_schema).parquet("s3://lakehouse/bronze/events")
# 3. Cleanse & Deduplicate
dedup_window = Window.partitionBy("event_id").orderBy(col("event_time").desc())
silver_events = raw_events \
.filter(col("event_id").isNotNull() & (col("amount") >= 0)) \
.withColumn("rn", row_number().over(dedup_window)) \
.filter(col("rn") == 1) \
.drop("rn")
# 4. Broadcast Dimension Enrichment & Gold Aggregation
dim_accounts = spark.read.parquet("s3://lakehouse/silver/dim_accounts")
gold_summary = silver_events \
.join(broadcast(dim_accounts), "account_id", "inner") \
.groupBy("account_id", "account_tier") \
.agg(
count("event_id").alias("total_txns"),
round(sum("amount"), 2).alias("total_volume")
) \
.withColumn("tier_rank", rank().over(Window.partitionBy("account_tier").orderBy(col("total_volume").desc()))) \
.withColumn("processed_timestamp", current_timestamp())
# 5. Optimized Partitioned Storage
gold_summary.coalesce(8).write \
.mode("overwrite") \
.partitionBy("account_tier") \
.option("compression", "snappy") \
.parquet("s3://lakehouse/gold/account_kpis")Practice typing production-grade PySpark code for Enterprise Data Engineering Capstone Pipeline.