Skip to main content
๐Ÿ“˜ Complete PDF

PySpark Certification Practice Test 07

Find The Problem In The Code

Question 1 of 20
๐Ÿ“‹ Multiple Choice
Question 121
A developer writes the following PySpark code to check the size of a sales dataset: ```python df = spark.read.parquet("s3://lake/sales_transactions/") result = df.collect() print(f"Total records: {len(result)}") ``` The dataset contains 2 billion records (~800 GB uncompressed). What is the BIGGEST problem with this code?
A. Missing DataFrame cache before the action
โœ“
B. Fatal Driver Out-Of-Memory (OOM) Risk: `collect()` attempts to pull 2 billion records across the network into the Driver's local heap
C. Missing `repartition()` before reading Parquet files
D. Missing broadcast join hint on the DataFrame
Answer
โœ… B. Fatal Driver Out-Of-Memory (OOM) Risk: `collect()` attempts to pull 2 billion records across the network into the Driver's local heap
Explanation

1. Core Concept Overview Calling `collect()` transfers all partition data across the network into the Driver node's local JVM heap. For datasets containing billions of rows, this causes network saturation and a fatal JVM `OutOfMemoryError`.

2. Step-by-Step Technical Breakdown - **Option B is Correct:** `collect()` pulls all distributed rows into local Driver memory. To simply count records, calling `df.count()` computes partition counts in parallel on executors and returns a single 8-byte integer to the Driver, keeping Driver memory usage near zero. - **Option A is Incorrect:** Caching before `collect()` would not prevent Driver memory exhaustion. - **Option C is Incorrect:** Repartitioning does not fix the Driver bottleneck caused by `collect()`. - **Option D is Incorrect:** Broadcast hints are used during joins, not during record counting.

3. Why Other Options Are Incorrect - **A preserves the fatal Driver bottleneck**, adding memory overhead. - **C changes executor partition layout**, but `collect()` still routes all data to the driver. - **D is a join optimization construct**, irrelevant to counting rows.

4. Architecture & Platform Execution Execution comparison: `df.count()` runs a distributed Map-side count on each executor partition, sends only the subtotal integers over the network, and sums them on the Driver. `df.collect()` forces executors to serialize all 2 billion row objects and transmit 800 GB over Netty RPC sockets directly to the Driver.

5. Practical Code Example

# FATAL (Anti-Pattern: Pulls 800 GB into Driver memory):
# result = df.collect()
# print(len(result))

# OPTIMAL: Distributed count returning only an integer to the Driver
total_records = df.count()
print(f"Total records: {total_records}")

6. Key Takeaway Never call `.collect()` to count rows or inspect data sizes; always use `.count()` to keep execution distributed.

7. Best Practice & Production Recommendation If you need to inspect sample rows on the Driver, always limit the dataset first using `df.limit(10).collect()` or `df.take(10)`.

8. Real-World Engineering Scenario An analytics engineer ran `len(df.collect())` in a shared Databricks workspace on a 1.2-billion row table. The Driver node crashed with an Out-of-Memory error, terminating the workspace cluster and dropping 8 other team members' active notebooks.

9. Common Exam Trap / Anti-Pattern / Misconception Using Python's native `len(df.collect())` instead of PySpark's native `df.count()`. The native method computes counts in parallel on executors.

10. Related InsightfulSaga Tutorial Master these concepts in [PySpark Optimization & Debugging](/tutorials/pyspark/pyspark-dataframe-basics).

Answered: 0 / 20
Question Navigator (Test 07)0% Answered