Word count as MapReduce example in Hadoop - Time & Space Complexity
We want to understand how the time needed to count words grows as the input text gets bigger.
How does the number of words affect the work done by MapReduce?
Analyze the time complexity of the following code snippet.
public class WordCount {
public static class Mapper extends org.apache.hadoop.mapreduce.Mapper<Object, Text, Text, IntWritable> {
public void map(Object key, Text value, Context context) {
String[] words = value.toString().split("\\s+");
for (String word : words) {
context.write(new Text(word), new IntWritable(1));
}
}
}
public static class Reducer extends org.apache.hadoop.mapreduce.Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context) {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
context.write(key, new IntWritable(sum));
}
}
}
This code counts how many times each word appears in the input text using MapReduce.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over each word in the input text in the Mapper, and loop over counts for each word in the Reducer.
- How many times: The Mapper loops once per word in the input; the Reducer loops once per unique word's counts.
As the number of words grows, the Mapper processes each word once, so work grows directly with input size.
| Input Size (n words) | Approx. Operations |
|---|---|
| 10 | About 10 map operations + reduce on unique words |
| 100 | About 100 map operations + reduce on unique words |
| 1000 | About 1000 map operations + reduce on unique words |
Pattern observation: The total work grows roughly in direct proportion to the number of words.
Time Complexity: O(n)
This means the time to count words grows linearly with the number of words in the input.
[X] Wrong: "The reduce step takes as much time as the map step because it loops over all words again."
[OK] Correct: The reduce step only loops over counts for each unique word, which is usually much smaller than total words.
Understanding how MapReduce scales with input size helps you explain distributed processing clearly and confidently.
"What if the input text had many repeated words? How would that affect the time complexity of the reduce step?"
Practice
Map step in the Word Count MapReduce example?Solution
Step 1: Understand the Map step role
The Map step processes input text and breaks it into pairs where each word is paired with the number 1. The Reduce step sums these counts, so the Map step does not sum or sort but only creates (word, 1) pairs.Final Answer:
To split input text into pairs of (word, 1) -> Option AQuick Check:
Map step = (word, 1) pairs [OK]
- Confusing Map with Reduce step
- Thinking Map sorts words
- Assuming Map sums counts
Solution
Step 1: Recall Mapper output syntax
In Hadoop MapReduce, the Mapper outputs key-value pairs usingcontext.write(key, value);. For word count, the key is the word and the value is 1, socontext.write(word, 1);is correct.Final Answer:
context.write(word, 1); -> Option AQuick Check:
Mapper output = context.write(word, 1) [OK]
- Swapping key and value order
- Using incorrect method name
- Omitting the value in output
"cat dog cat bird dog cat", what will be the output of the Reduce step in the Word Count MapReduce example?Solution
Step 1: Count each word occurrences
Input words: cat (3 times), dog (2 times), bird (1 time). The Reduce step sums all counts for each word, resulting in ('cat', 3), ('dog', 2), ('bird', 1).Final Answer:
[('cat', 3), ('dog', 2), ('bird', 1)] -> Option BQuick Check:
Word counts = [('cat', 3), ('dog', 2), ('bird', 1)] [OK]
- Mixing counts of dog and cat
- Counting only unique words once
- Adding counts incorrectly
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String[] words = value.toString().split(" ");
for (String word : words) {
context.write(word, new IntWritable(1));
}
}Solution
Step 1: Check context.write parameter types
In Hadoop, context.write expects key and value types matching Mapper output types, usually Text and IntWritable. The variablewordis a String, but context.write requires a Text object as key, so it should benew Text(word).Final Answer:
The key type in context.write should be Text, not String -> Option DQuick Check:
Use Text for keys in context.write [OK]
- Passing String instead of Text as key
- Using wrong split delimiter
- Expecting map method to return a value
Solution
Step 1: Understand case normalization
To treat 'Cat' and 'cat' as the same, convert all words to the same case before counting. Changing words to lowercase in the Mapper ensures all keys are uniform before Reduce sums counts.Final Answer:
Convert each word to lowercase before writing: context.write(new Text(word.toLowerCase()), new IntWritable(1)); -> Option CQuick Check:
Normalize case in Mapper for consistent keys [OK]
- Changing case in Reducer instead of Mapper
- Sorting words instead of normalizing case
- Filtering words unrelated to case
Ready to prove it?
Pass the test and this pattern moves unknown
