Bird
Raised Fist0
Apache Sparkdata~10 mins

Error handling and dead letter queues in Apache Spark - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to read a JSON file into a DataFrame.

Apache Spark
df = spark.read.[1]("data/input.json")
Drag options to blanks, or click blank then click option'
Ajson
Bcsv
Cparquet
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'csv' or 'text' to read JSON files causes errors.
Forgetting to specify the correct format method.
2fill in blank
medium

Complete the code to write a DataFrame to a dead letter queue path in Parquet format.

Apache Spark
df_dead_letter.write.mode("append").[1]("data/dead_letter")
Drag options to blanks, or click blank then click option'
Ajson
Bparquet
Ccsv
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'json' or 'csv' which may be less efficient.
Forgetting to set the write mode to 'append'.
3fill in blank
hard

Fix the error in the code to catch exceptions during data processing.

Apache Spark
try:
    df = spark.read.json("data/input.json")
    df.show()
except [1] as e:
    print(f"Error: {e}")
Drag options to blanks, or click blank then click option'
ASparkException
BError
CException
DValueError
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined exception classes causes NameError.
Catching too specific exceptions misses other errors.
4fill in blank
hard

Fill both blanks to filter out bad records and collect them into a dead letter DataFrame.

Apache Spark
dead_letter_df = df.filter(df["status"] [1] "error").select([2])
Drag options to blanks, or click blank then click option'
A==
B"*"
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' filters the wrong records.
Selecting columns without quotes causes syntax errors.
5fill in blank
hard

Fill all three blanks to create a dictionary of error counts by type from the dead letter DataFrame.

Apache Spark
error_counts = dead_letter_df.groupBy([1]).count().rdd.map(lambda row: (row[[2]], row[[3]])).collectAsMap()
Drag options to blanks, or click blank then click option'
A"error_type"
B0
C1
D"status"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong column names in groupBy causes errors.
Mixing up row indices in the lambda function.

Practice

(1/5)
1. What is the main purpose of using a dead letter queue in Apache Spark data pipelines?
easy
A. To speed up the processing of good data by caching it
B. To store bad or invalid data separately for later analysis or correction
C. To automatically fix errors in the data during processing
D. To merge multiple data sources into one unified dataset

Solution

  1. Step 1: Understand the role of error handling in data pipelines

    Error handling separates good data from bad data to prevent pipeline failures.
  2. Step 2: Define the purpose of a dead letter queue

    A dead letter queue stores bad or invalid data separately so it can be reviewed or fixed later without stopping the main pipeline.
  3. Final Answer:

    To store bad or invalid data separately for later analysis or correction -> Option B
  4. Quick Check:

    Dead letter queue = store bad data separately [OK]
Hint: Dead letter queue holds bad data separately [OK]
Common Mistakes:
  • Thinking dead letter queue fixes data automatically
  • Confusing dead letter queue with caching good data
  • Assuming it merges data sources
2. Which of the following Spark code snippets correctly filters out invalid records and writes them to a dead letter queue DataFrame named dlqDF?
easy
A. val dlqDF = inputDF.filter(col("age") < 0)
B. val dlqDF = inputDF.select("age")
C. val dlqDF = inputDF.filter(col("age") > 0)
D. val dlqDF = inputDF.drop("age")

Solution

  1. Step 1: Identify the condition for invalid data

    Invalid records have age less than 0, so filter with col("age") < 0.
  2. Step 2: Check which code filters invalid data correctly

    val dlqDF = inputDF.filter(col("age") < 0) filters rows where age is less than 0, capturing invalid data for the dead letter queue.
  3. Final Answer:

    val dlqDF = inputDF.filter(col("age") < 0) -> Option A
  4. Quick Check:

    Filter invalid age < 0 = val dlqDF = inputDF.filter(col("age") < 0) [OK]
Hint: Filter invalid rows with condition in filter() [OK]
Common Mistakes:
  • Filtering valid data instead of invalid
  • Using select instead of filter
  • Dropping columns instead of filtering rows
3. Given the following Spark code, what will be the count of records in dlqDF?
val inputDF = spark.createDataFrame(Seq((1, 25), (2, -5), (3, 30), (4, -1))).toDF("id", "age")
val dlqDF = inputDF.filter(col("age") < 0)
dlqDF.count()
medium
A. 2
B. 4
C. 0
D. 3

Solution

  1. Step 1: Identify records with age less than 0

    Records with age -5 and -1 meet the condition, so 2 records are invalid.
  2. Step 2: Count filtered records in dlqDF

    dlqDF contains these 2 invalid records, so count is 2.
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Count invalid age < 0 = 2 [OK]
Hint: Count rows where age < 0 to find dlq size [OK]
Common Mistakes:
  • Counting all rows instead of filtered
  • Miscounting invalid records
  • Confusing filter condition direction
4. The following Spark code is intended to separate invalid records (age < 0) into a dead letter queue DataFrame dlqDF. However, it does not work as expected. What is the error?
val dlqDF = inputDF.filter("age > 0")
medium
A. The DataFrame name dlqDF is reserved and cannot be used
B. The filter syntax is incorrect; it should use col() function
C. The filter method cannot be used with string conditions
D. The filter condition is reversed; it selects valid data instead of invalid

Solution

  1. Step 1: Analyze the filter condition

    The condition "age > 0" selects records with positive age, which are valid, not invalid.
  2. Step 2: Identify the correct condition for invalid data

    Invalid data has age less than 0, so the filter should be "age < 0" to capture bad records.
  3. Final Answer:

    The filter condition is reversed; it selects valid data instead of invalid -> Option D
  4. Quick Check:

    Filter condition reversed = The filter condition is reversed; it selects valid data instead of invalid [OK]
Hint: Check filter logic matches invalid data condition [OK]
Common Mistakes:
  • Using wrong comparison operator in filter
  • Thinking string filter syntax is invalid
  • Assuming DataFrame names are restricted
5. You have a Spark DataFrame inputDF with a column status that can be 'valid' or 'invalid'. You want to create two DataFrames: cleanDF with only valid records and dlqDF with invalid records. Which code correctly implements this error handling with dead letter queue?
hard
A. val cleanDF = inputDF.filter(col("status") = "valid") val dlqDF = inputDF.filter(col("status") = "invalid")
B. val cleanDF = inputDF.select("status" == "valid") val dlqDF = inputDF.select("status" == "invalid")
C. val cleanDF = inputDF.filter(col("status") === "valid") val dlqDF = inputDF.filter(col("status") === "invalid")
D. val cleanDF = inputDF.drop("invalid") val dlqDF = inputDF.drop("valid")

Solution

  1. Step 1: Use correct equality operator in filter

    In Spark Scala, use triple equals (===) to compare column values to strings.
  2. Step 2: Filter valid and invalid records separately

    Filter status === "valid" for cleanDF and status === "invalid" for dlqDF to separate data correctly.
  3. Final Answer:

    val cleanDF = inputDF.filter(col("status") === "valid") val dlqDF = inputDF.filter(col("status") === "invalid") -> Option C
  4. Quick Check:

    Use === for equality and filter separately [OK]
Hint: Use === for string equality in filter() [OK]
Common Mistakes:
  • Using single = instead of === in filter
  • Using select instead of filter for conditions
  • Dropping columns instead of filtering rows
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