Bird
Raised Fist0
Apache Sparkdata~20 mins

Writing output with partitioning in Apache Spark - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Partitioning Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of partitioned write with multiple columns

What will be the folder structure created when writing a DataFrame partitioned by two columns?

Consider this Spark code:

df.write.partitionBy("year", "month").parquet("/data/events")

The DataFrame has data for years 2022 and 2023, and months 1 and 2.

Which folder paths will be created inside /data/events?

A/data/events/year=2022/month=1/, /data/events/year=2022/month=2/, /data/events/year=2023/month=1/, /data/events/year=2023/month=2/
B/data/events/month=1/year=2022/, /data/events/month=2/year=2022/, /data/events/month=1/year=2023/, /data/events/month=2/year=2023/
C/data/events/year=2022/, /data/events/year=2023/, /data/events/month=1/, /data/events/month=2/
D/data/events/year=2022_month=1/, /data/events/year=2022_month=2/, /data/events/year=2023_month=1/, /data/events/year=2023_month=2/
Attempts:
2 left
💡 Hint

Partition folders are created in the order of the columns specified in partitionBy.

❓ data_output
intermediate
2:00remaining
Number of files created with partitioning

You write a DataFrame with 3 partitions using partitionBy("category"). The DataFrame has 2 unique categories: 'A' and 'B'.

If the DataFrame has 6 total partitions before writing, how many parquet files will be created inside the output folder?

A2 files
B6 files
C3 files
D1 file
Attempts:
2 left
💡 Hint

Each partition of the DataFrame writes one file per partition folder.

🔧 Debug
advanced
2:00remaining
Error when writing partitioned data with unsupported column

What error will this Spark code produce?

df.write.partitionBy("nonexistent_column").parquet("/output/path")

The DataFrame df does not have a column named nonexistent_column.

AAnalysisException: 'Partition column nonexistent_column not found in schema'
BNullPointerException
CNo error, writes data ignoring the partition column
DFileNotFoundException
Attempts:
2 left
💡 Hint

Partition columns must exist in the DataFrame schema.

🚀 Application
advanced
2:00remaining
Choosing partition columns for efficient queries

You have a large sales dataset with columns: date, region, product, and sales_amount.

You want to write the data partitioned to speed up queries filtering by region and date. Which partitioning strategy is best?

APartition by <code>product</code>
BPartition by <code>date</code> only
CPartition by <code>region</code> only
DPartition by <code>region</code> and <code>date</code> (in that order)
Attempts:
2 left
💡 Hint

Partition columns should match common filter columns in queries.

🧠 Conceptual
expert
2:00remaining
Effect of partitioning on data skew and shuffle

Which statement best describes the impact of partitioning output data on data skew and shuffle during a Spark write?

APartitioning always reduces shuffle and eliminates data skew
BPartitioning increases shuffle and always causes data skew
CPartitioning can reduce shuffle but may cause data skew if partition keys are unevenly distributed
DPartitioning has no effect on shuffle or data skew
Attempts:
2 left
💡 Hint

Think about how data distribution affects partitioning.

Practice

(1/5)
1. What does the partitionBy method do when writing data in Apache Spark?
easy
A. It sorts the data before saving to disk.
B. It compresses the output data to save disk space.
C. It splits the output data into folders based on column values.
D. It encrypts the output data for security.

Solution

  1. Step 1: Understand the purpose of partitionBy

    The partitionBy method organizes output files into folders named by unique values of specified columns.
  2. Step 2: Recognize its effect on data storage

    This splitting helps in faster data reading and better organization by grouping similar data together.
  3. Final Answer:

    It splits the output data into folders based on column values. -> Option C
  4. Quick Check:

    partitionBy splits data folders by column values [OK]
Hint: partitionBy creates folders by column values when saving [OK]
Common Mistakes:
  • Thinking partitionBy compresses data
  • Confusing partitionBy with sorting
  • Assuming partitionBy encrypts data
2. Which of the following is the correct syntax to write a DataFrame df partitioned by column country in Spark?
easy
A. df.write.save.partitionBy("country")("path/to/output")
B. df.partitionBy.write("country").save("path/to/output")
C. df.write.save("path/to/output").partitionBy("country")
D. df.write.partitionBy("country").save("path/to/output")

Solution

  1. Step 1: Recall the method chaining order

    In Spark, partitionBy is called on the write object before save.
  2. Step 2: Verify correct syntax

    The correct syntax is df.write.partitionBy("country").save("path"). Other options misuse method order or call.
  3. Final Answer:

    df.write.partitionBy("country").save("path/to/output") -> Option D
  4. Quick Check:

    partitionBy called after write, before save [OK]
Hint: Use df.write.partitionBy(...).save(...) syntax [OK]
Common Mistakes:
  • Calling partitionBy after save
  • Using partitionBy on df instead of df.write
  • Incorrect method chaining order
3. Given the following code, what folder structure will be created when saving the DataFrame?
df.write.partitionBy("year", "month").mode("overwrite").parquet("/data/events")

Assuming year has values 2023 and 2024, and month has values 1 and 2.
medium
A. /data/events/year=2023/month=1/, /data/events/year=2023/month=2/, /data/events/year=2024/month=1/, /data/events/year=2024/month=2/
B. /data/events/2023/1/, /data/events/2023/2/, /data/events/2024/1/, /data/events/2024/2/
C. /data/events/year/month/2023/1/, /data/events/year/month/2023/2/, /data/events/year/month/2024/1/, /data/events/year/month/2024/2/
D. /data/events/year=month=2023_1/, /data/events/year=month=2023_2/, /data/events/year=month=2024_1/, /data/events/year=month=2024_2/

Solution

  1. Step 1: Understand partition folder naming

    Spark creates folders named by each partition column and its value, nested in order of columns.
  2. Step 2: Apply to given columns and values

    Folders will be nested as /year=2023/month=1/ etc., not just values or combined keys.
  3. Final Answer:

    /data/events/year=2023/month=1/, /data/events/year=2023/month=2/, /data/events/year=2024/month=1/, /data/events/year=2024/month=2/ -> Option A
  4. Quick Check:

    Partition folders named as column=value pairs [OK]
Hint: Partition folders use column=value format nested by columns [OK]
Common Mistakes:
  • Using only values without column names in folders
  • Combining columns into one folder name
  • Adding extra folder levels incorrectly
4. Identify the error in this Spark code that tries to write a DataFrame partitioned by category:
df.write.save("/output/path").partitionBy("category")
medium
A. The path should be a directory, not a file.
B. partitionBy should be called before save, not after.
C. partitionBy requires a list of columns, not a single string.
D. The DataFrame must be cached before writing.

Solution

  1. Step 1: Check method chaining order

    In Spark, partitionBy must be called on the write object before save.
  2. Step 2: Identify the error in the code

    The code calls save first, then partitionBy, which is invalid and causes an error.
  3. Final Answer:

    partitionBy should be called before save, not after. -> Option B
  4. Quick Check:

    partitionBy before save is required [OK]
Hint: Call partitionBy before save to avoid errors [OK]
Common Mistakes:
  • Calling partitionBy after save
  • Passing a single string instead of list (both allowed but string is valid)
  • Confusing caching with writing order
5. You have a large DataFrame df with columns country, year, and sales. You want to save it partitioned by country and year to speed up queries filtering by these columns. Which approach is best to write the data efficiently?
hard
A. Use df.write.partitionBy("country", "year").mode("overwrite").parquet("/data/sales") to save partitioned parquet files.
B. Save the DataFrame without partitioning and filter later during reading.
C. Use df.write.mode("overwrite").csv("/data/sales") without partitioning for simplicity.
D. Write the DataFrame multiple times, once per country and year combination.

Solution

  1. Step 1: Understand partitioning benefits

    Partitioning by country and year organizes data into folders, speeding up queries filtering on these columns.
  2. Step 2: Evaluate options for efficient writing

    Use df.write.partitionBy("country", "year").mode("overwrite").parquet("/data/sales") to save partitioned parquet files. This uses partitionBy with parquet format and overwrite mode, which is efficient and standard practice.
  3. Step 3: Reject less efficient options

    Options B and C do not partition, causing slower queries. Writing the DataFrame multiple times, once per country and year combination, is inefficient and error-prone.
  4. Final Answer:

    Use df.write.partitionBy("country", "year").mode("overwrite").parquet("/data/sales") to save partitioned parquet files. -> Option A
  5. Quick Check:

    partitionBy with parquet speeds queries [OK]
Hint: Partition by query filter columns using parquet format [OK]
Common Mistakes:
  • Not partitioning data before saving
  • Writing multiple times instead of once
  • Using CSV without partitioning for large data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard