Skip to main content
PYSPARK • LESSON 225

Duplicate Identification & Audit Log Extraction

How can we extract all duplicate records (rn > 1) into an audit log table for compliance investigation?

Advanced3 Minutes840 XP
🤔 THE QUESTION

How can we extract all duplicate records (rn > 1) into an audit log table for compliance investigation?

💡 WHAT IS IT?

Filtering for row_number() > 1 isolates every discarded duplicate record along with its rank for auditing.

🎯 WHAT IS IT USED FOR?

Regulatory compliance reporting and identifying upstream double-billing or re-transmission anomalies.

💻 EXAMPLE
w = Window.partitionBy("transaction_id").orderBy(col("received_at").desc())
df_duplicates_audit = df.withColumn("rn", row_number().over(w)).filter(col("rn") > 1)

🎯 Mission Objectives

Practice typing production-grade PySpark code for Duplicate Identification & Audit Log Extraction.

  • Isolating discarded duplicates
  • Audit log extraction
  • Data anomaly reporting