Map phase explained in Hadoop - Time & Space Complexity
We want to understand how the time needed for the Map phase changes as the input data grows.
How does the Map phase handle more data and how does that affect processing time?
Analyze the time complexity of the following code snippet.
public class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] words = line.split(" ");
for (String word : words) {
context.write(new Text(word), new IntWritable(1));
}
}
}
This code reads each line of input, splits it into words, and outputs each word with a count of one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over all words in each input line.
- How many times: Once per word in the input data.
As the input data grows, the number of words grows roughly in proportion.
| Input Size (n words) | Approx. Operations |
|---|---|
| 10 | 10 loops over words |
| 100 | 100 loops over words |
| 1000 | 1000 loops over words |
Pattern observation: The work grows directly with the number of words; doubling words doubles work.
Time Complexity: O(n)
This means the Map phase time grows linearly with the number of words processed.
[X] Wrong: "The Map phase time stays the same no matter how much data there is."
[OK] Correct: The Map phase processes each word once, so more words mean more work and more time.
Understanding how the Map phase scales helps you explain how big data jobs handle large inputs efficiently.
"What if the map function also had to check each word against a dictionary? How would the time complexity change?"
Practice
Map phase in Hadoop's data processing?Solution
Step 1: Understand the Map phase purpose
The Map phase reads input data and processes it piece by piece.Step 2: Identify output of Map phase
It produces intermediate key-value pairs for the next phase (Reduce) to process.Final Answer:
To process input data and produce intermediate key-value pairs -> Option CQuick Check:
Map phase = intermediate key-value pairs [OK]
- Confusing Map phase with Reduce phase
- Thinking Map phase stores final results
- Assuming Map phase cleans data
Solution
Step 1: Recall Map function signature in Hadoop Java
The Map method signature includes input key, input value, and context for output.Step 2: Match correct parameters and exceptions
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException matches the correct signature with LongWritable key, Text value, Context context, and throws exceptions.Final Answer:
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException -> Option DQuick Check:
Map method signature = public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException [OK]
- Mixing Map and Reduce method signatures
- Wrong parameter order or types
- Omitting exception declarations
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] words = value.toString().split(" ");
for (String word : words) {
context.write(new Text(word), new IntWritable(1));
}
}Solution
Step 1: Split input line into words
The input "apple orange apple" splits into ["apple", "orange", "apple"].Step 2: Emit each word with count 1
The Map function outputs each word paired with 1: ('apple',1), ('orange',1), ('apple',1).Final Answer:
[('apple', 1), ('orange', 1), ('apple', 1)] -> Option AQuick Check:
Map outputs word counts as pairs [OK]
- Summing counts in Map phase (done in Reduce)
- Outputting entire line as one key
- Missing duplicate words in output
public void map(LongWritable key, Text value, Context context) {
String[] words = value.toString().split(" ");
for (String word : words) {
context.write(new Text(word), new IntWritable(1));
}
}Solution
Step 1: Check method signature requirements
Hadoop Map method must declare throwing IOException and InterruptedException.Step 2: Verify method signature correctness
The given method lacks "throws IOException, InterruptedException", causing a compile error.Final Answer:
Missing throws IOException, InterruptedException in method signature -> Option AQuick Check:
Map method must declare exceptions [OK]
- Ignoring required exceptions in method signature
- Assuming context cannot be used
- Thinking split delimiter is wrong
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] words = value.toString().split(" ");
for (String word : words) {
context.write(new Text(word), new IntWritable(1));
}
}Solution
Step 1: Identify filtering requirement in Map phase
We want to output only words longer than 3 characters during Map.Step 2: Add condition to output only qualifying words
Adding if (word.length() > 3) before context.write filters words correctly.Final Answer:
Add an if condition: if (word.length() > 3) before context.write -> Option BQuick Check:
Filter words by length in Map phase [OK]
- Trying to filter by regex in split (wrong approach)
- Filtering only in Reduce phase (too late)
- Writing all words at once outside loop
Ready to prove it?
Pass the test and this pattern moves unknown
