📖 Scenario: You work at a small online bookstore. You have a list of book titles and want to process them to find interesting information.
🎯 Goal: Learn how to use map, filter, and flatMap operations in Apache Spark to transform and analyze book titles.
📋 What You'll Learn
Create an RDD with given book titles
Create a filter condition variable
Use map to convert titles to uppercase
Use filter to keep titles longer than a threshold
Use flatMap to split titles into words
Print the final results
💡 Why This Matters
🌍 Real World
Processing text data like book titles is common in data science to prepare data for analysis or search indexing.
💼 Career
Understanding <code>map</code>, <code>filter</code>, and <code>flatMap</code> is essential for working with big data frameworks like Apache Spark in data engineering and data science roles.
Progress0 / 4 steps
1
Create the initial RDD with book titles
Create an RDD called books_rdd from the list ["The Hobbit", "War and Peace", "Pride and Prejudice", "The Catcher in the Rye"] using sc.parallelize().
Apache Spark
Hint
Use sc.parallelize() to create an RDD from a Python list.
2
Set the minimum title length filter
Create a variable called min_length and set it to 12. This will be used to filter book titles longer than this length.
Apache Spark
Hint
Just create a variable min_length and assign the number 12.
3
Apply map, filter, and flatMap operations
Use map on books_rdd to convert each title to uppercase and save as upper_rdd. Then use filter on upper_rdd to keep titles with length greater than min_length and save as filtered_rdd. Finally, use flatMap on filtered_rdd to split each title into words and save as words_rdd.
Apache Spark
Hint
Use map to uppercase, filter with len(title) > min_length, and flatMap with split().
4
Print the final list of words
Collect the words_rdd into a list and print it using print().
Apache Spark
Hint
Use collect() to get the list and then print it.
Practice
(1/5)
1. What does the filter operation do in Apache Spark?
easy
A. Splits each item into multiple items and flattens the result
B. Changes each item in the dataset to a new item
C. Selects items from the dataset based on a condition
D. Combines two datasets into one
Solution
Step 1: Understand the purpose of filter
The filter operation keeps only those items that meet a specific condition or rule.
Step 2: Compare with other operations
Unlike map or flatMap, filter does not change items but selects some based on a test.
Final Answer:
Selects items from the dataset based on a condition -> Option C
Hint: flatMap flattens lists from each item into one list [OK]
Common Mistakes:
Expecting flatMap to keep nested lists
Confusing map output with flatMap output
Thinking flatMap returns original words
4. Identify the error in this Spark code snippet:
val rdd = sc.parallelize(List(1, 2, 3, 4))
val filtered = rdd.filter(x => x > 2)
val mapped = filtered.map(x => x * 2)
val result = mapped.flatMap(x => x)
result.collect()
medium
A. flatMap function argument should return an iterable, but it returns an integer
B. filter condition syntax is incorrect
C. map function cannot multiply integers
D. parallelize method is used incorrectly
Solution
Step 1: Analyze flatMap argument
flatMap expects a function returning a list or iterable, but here it returns a single integer.
Step 2: Check other parts
filter and map are correctly used; parallelize is correct.
Final Answer:
flatMap function argument should return an iterable, but it returns an integer -> Option A
Quick Check:
flatMap needs iterable return [OK]
Hint: flatMap needs iterable output, not single values [OK]
Common Mistakes:
Passing single value to flatMap instead of iterable
Misreading filter syntax as error
Thinking map cannot multiply numbers
5. You have an RDD of sentences:
val sentences = sc.parallelize(List("hello world", "apache spark", "map filter flatMap"))
How would you use flatMap and filter to get all words longer than 4 characters?
hard
A. sentences.flatMap(sentence => sentence.split(" ")).filter(word => word.length > 4)
B. sentences.map(sentence => sentence.split(" ")).filter(word => word.length > 4)
C. sentences.filter(sentence => sentence.length > 4).flatMap(word => word.split(" "))
D. sentences.flatMap(sentence => sentence.length > 4)
Solution
Step 1: Use flatMap to split sentences into words
flatMap splits each sentence into words and flattens them into one list.
Step 2: Use filter to select words longer than 4 characters
filter keeps only words where length is greater than 4.