The map operation applies a function to each element of an RDD or DataFrame column and returns a new RDD or column with the transformed elements.
Click to reveal answer
beginner
Explain the purpose of the filter operation in Spark.
The filter operation selects elements from an RDD or DataFrame column that satisfy a given condition, removing all others.
Click to reveal answer
intermediate
What is the difference between map and flatMap in Spark?
map transforms each element into exactly one element, while flatMap can transform each element into zero or more elements, flattening the results into a single list.
Click to reveal answer
beginner
Give a real-life example of using filter in data processing.
Filtering a list of customer orders to keep only those with an amount greater than $100 is a real-life example of using filter.
Click to reveal answer
intermediate
Why is flatMap useful when working with text data in Spark?
flatMap is useful for splitting lines of text into words because it can turn one line into many words, flattening them into a single list for easier processing.
Click to reveal answer
What does the map operation return when applied to an RDD?
AA new RDD with each element transformed by a function
BOnly elements that satisfy a condition
CA flattened list of elements
DThe original RDD unchanged
✗ Incorrect
map applies a function to each element and returns a new RDD with transformed elements.
Which operation would you use to keep only even numbers from an RDD?
Afilter
Bmap
CflatMap
Dreduce
✗ Incorrect
filter selects elements that meet a condition, such as being even.
What is the key feature of flatMap compared to map?
AIt returns fewer elements
BIt can return zero or more elements per input element
CIt only works on numbers
DIt filters elements
✗ Incorrect
flatMap can return multiple elements per input, flattening the results.
If you want to split sentences into words in Spark, which operation is best?
Amap
Bfilter
CflatMap
DgroupBy
✗ Incorrect
flatMap splits each sentence into multiple words and flattens them.
Which operation removes elements from an RDD?
Acollect
Bmap
CflatMap
Dfilter
✗ Incorrect
filter removes elements that do not meet the condition.
Describe how you would use map, filter, and flatMap to process a list of sentences to get all words longer than 3 letters.
Think about splitting, selecting, and transforming steps.
You got /3 concepts.
Explain the difference between map and flatMap with an example.
Focus on output size per input element.
You got /3 concepts.
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.