Interview Focus: DataFramesDifficulty: Easy

Convert CSV to Parquet

Estimated duration: 10 mins

Lab Overview & Scenario

Read CSV transaction lists, filter bad records, and write the output as optimized Parquet.

Business Scenario

To optimize warehouse query costs, you must write a pipeline that reads transactional CSV logs, filters out negative amount lines, and saves the cleaned logs as columnar Parquet files.

1. Local Raw Mock Dataset

The processing pipeline will ingest the following local mock file structures (e.g. simulated as `dataset.csv`):

txn_idaccount_idamount
30011001150.0
30021002-50.0
30031003300.0
300410040.0
30051005450.0
View Raw CSV Dataset
txn_id,account_id,amount
3001,1001,150.0
3002,1002,-50.0
3003,1003,300.0
3004,1004,0.0
3005,1005,450.0

2. Your Tasks

3. Pipeline Skeleton Script

Copy this starter template to write the data transformation logic:

python
# starter.py - Convert CSV to Parquet
from pyspark.sql import SparkSession
from pyspark.sql.functions import col

def process_formats():
    spark = SparkSession.builder.appName("FormatConversion").getOrCreate()
    # TODO: Read dataset.csv, filter amounts > 0, write parquet
    pass

if __name__ == "__main__":
    process_formats()

4. Interactive Solution & Execution Output

Reveal Solution Pipeline Code
Implement the correct transformations using PySpark to achieve the aggregated values metrics output.

Pipeline Flow

dataset.csv
Read CSV
Filter amount > 0
Write Parquet Mode
Verify Read Back

Step-by-Step Implementation Guide

Step 1

Load the dirty transactions CSV dataset.

Step 2

Filter out rows where amount <= 0 to isolate valid transactions.

Step 3

Save the cleaned DataFrame as Parquet format: call df.write.mode("overwrite").parquet("path") which automatically writes files using columnar partitioning.

Step 4

Read back the written Parquet data using spark.read.parquet() to verify it matches.

Common Mistakes

  • Forgetting to specify the write mode (e.g. overwrite), causing job failures if output files already exist.
  • Saving raw CSVs directly without converting data types, which ruins schema storage in Parquet headers.

Interviewer's Expectations

Why this is asked:

Columnar storage conversion is a vital design practice in high-performance cloud data warehouses.

What they look for:

Familiarity with DataFrame write APIs, modes, and format paths.

Common mistakes:

Not checking write modes, resulting in crash behaviors.

An excellent answer includes:

Explain that Parquet saves schema metadata, provides columnar compression, and supports projection pushdowns.

What You Learned

  • Write DataFrame outputs as Parquet files
  • Set write modes to avoid file path conflict crashes
  • Validate pipeline file structures