The security team wants to detect fraud as orders happen. You need to write a streaming query that processes real-time sales streams, filters out orders with values greater than $5,000, and writes them to a sink.
The processing pipeline will ingest the following local mock file structures (e.g. simulated as `dataset.csv`):
| order_id | card_id | amount |
|---|---|---|
| 5001 | 9001 | 150.0 |
| 5002 | 9002 | 6000.0 |
| 5003 | 9001 | 450.0 |
| 5004 | 9003 | 5500.0 |
| 5005 | 9002 | 120.0 |
order_id,card_id,amount 5001,9001,150.0 5002,9002,6000.0 5003,9001,450.0 5004,9003,5500.0 5005,9002,120.0
Copy this starter template to write the data transformation logic:
# starter.py - Streaming Order Processing
from pyspark.sql import SparkSession
from pyspark.sql.functions import col
def process_stream():
spark = SparkSession.builder.appName("StreamingLab").getOrCreate()
# TODO: Configure streaming source, filter amounts > 5000, write stream
pass
if __name__ == "__main__":
process_stream()amount > 5000 to flag high-value, potentially fraudulent charges.
readStream and writeStream to run this as a continuous streaming job. Here, we run it as a batch query on the delta log to verify the logic.