Challenge - 5 Problems
Error Handling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Spark DataFrame error handling code?
Consider the following PySpark code snippet that tries to parse a JSON column and handle errors by sending bad records to a dead letter queue DataFrame. What will be the count of records in the dead letter queue DataFrame?
Apache Spark
from pyspark.sql import SparkSession from pyspark.sql.functions import from_json, col from pyspark.sql.types import StructType, StructField, StringType spark = SparkSession.builder.appName('ErrorHandling').getOrCreate() data = [ ('{"name":"Alice","age":"30"}',), ('{"name":"Bob","age":"not_a_number"}',), ('{"name":"Charlie","age":"25"}',), ('invalid_json',) ] schema = StructType([ StructField('name', StringType(), True), StructField('age', StringType(), True) ]) df = spark.createDataFrame(data, ['json_str']) parsed_df = df.withColumn('parsed', from_json(col('json_str'), schema)) valid_df = parsed_df.filter(col('parsed').isNotNull()) error_df = parsed_df.filter(col('parsed').isNull()) error_count = error_df.count()
Attempts:
2 left
💡 Hint
Think about which JSON strings are invalid or cannot be parsed correctly.
✗ Incorrect
from_json returns null only for malformed JSON. The first three records are valid JSON objects matching the StringType schema for both fields. The fourth record 'invalid_json' causes parsing to return null. Thus, error_df.count() is 1.
❓ data_output
intermediateHow many records are in the dead letter queue after applying this error handling logic?
Given a Spark DataFrame with a column 'data' containing JSON strings, you apply a schema to parse it. Records that fail parsing are sent to a dead letter queue. If the input data has 5 records and 2 of them are malformed JSON, how many records will be in the dead letter queue DataFrame?
Attempts:
2 left
💡 Hint
Dead letter queue contains only records that fail parsing.
✗ Incorrect
Only the 2 malformed JSON records fail parsing and go to the dead letter queue. The other 3 parse successfully.
🔧 Debug
advancedIdentify the error in this Spark error handling code snippet
This PySpark code tries to parse JSON and separate valid and invalid records. What error will it raise when run?
Apache Spark
from pyspark.sql.functions import from_json, col schema = 'name STRING, age INT' df = spark.createDataFrame([('{"name":"Alice","age":30}',)], ['json_str']) parsed_df = df.withColumn('parsed', from_json(col('json_str'), schema)) valid_df = parsed_df.filter(col('parsed').isNotNull()) error_df = parsed_df.filter(col('parsed').isNull()) error_count = error_df.count()
Attempts:
2 left
💡 Hint
Check the type of the schema argument passed to from_json.
✗ Incorrect
The from_json function requires a StructType object for the schema, not a string. Passing a string causes a TypeError.
🚀 Application
advancedWhich option correctly implements a dead letter queue for malformed JSON records in Spark?
You want to parse a DataFrame column containing JSON strings. Malformed JSON records should be captured in a separate dead letter queue DataFrame. Which code snippet correctly implements this?
Attempts:
2 left
💡 Hint
Malformed JSON results in null in the parsed column.
✗ Incorrect
The parsed column contains null for malformed JSON. Filtering where parsed is null captures bad records for the dead letter queue.
🧠 Conceptual
expertWhy is a dead letter queue important in Spark streaming error handling?
In Spark streaming pipelines, why do we use a dead letter queue for error handling instead of just dropping malformed records?
Attempts:
2 left
💡 Hint
Think about data quality and auditing needs.
✗ Incorrect
Dead letter queues keep bad records so teams can analyze and fix data issues later, improving data quality and traceability.
