Bird
Raised Fist0
Hadoopdata~10 mins

Word count as MapReduce example in Hadoop - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to emit each word with count 1 in the mapper.

Hadoop
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
        StringTokenizer itr = new StringTokenizer(value.toString());
        while (itr.hasMoreTokens()) {
            word.set(itr.[1]());
            context.write(word, one);
        }
    }
}
Drag options to blanks, or click blank then click option'
Anext
BnextWord
CnextToken
DgetToken
Attempts:
3 left
💡 Hint
Common Mistakes
Using next() which does not exist in StringTokenizer.
Using nextWord() which is not a method of StringTokenizer.
2fill in blank
medium

Complete the reducer code to sum counts for each word.

Hadoop
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum = 0;
        for (IntWritable val : values) {
            sum [1] val.get();
        }
        result.set(sum);
        context.write(key, result);
    }
}
Drag options to blanks, or click blank then click option'
A/=
B-=
C*=
D+=
Attempts:
3 left
💡 Hint
Common Mistakes
Using -= which subtracts instead of adding.
Using *= or /= which multiply or divide instead of adding.
3fill in blank
hard

Fix the error in the mapper setup method to initialize the Text object.

Hadoop
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
    private final static IntWritable one = new IntWritable(1);
    private Text word;

    protected void setup(Context context) throws IOException, InterruptedException {
        word = new [1]();
    }

    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
        StringTokenizer itr = new StringTokenizer(value.toString());
        while (itr.hasMoreTokens()) {
            word.set(itr.nextToken());
            context.write(word, one);
        }
    }
}
Drag options to blanks, or click blank then click option'
AText
BString
CIntWritable
DWritable
Attempts:
3 left
💡 Hint
Common Mistakes
Using String instead of Text causes type errors.
Using IntWritable or Writable which are incorrect types here.
4fill in blank
hard

Fill both blanks to complete the driver code setting input and output paths.

Hadoop
public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "word count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.[1](job, new Path(args[0]));
    FileOutputFormat.[2](job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
}
Drag options to blanks, or click blank then click option'
AsetInputPath
BsetOutputPath
CaddInputPath
DaddOutputPath
Attempts:
3 left
💡 Hint
Common Mistakes
Using setInputPath instead of addInputPath causes errors.
Using addOutputPath which does not exist.
5fill in blank
hard

Fill all three blanks to complete the mapper class declaration and output types.

Hadoop
public static class TokenizerMapper extends Mapper<[1], [2], [3], IntWritable> {
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map([1] key, [2] value, Context context) throws IOException, InterruptedException {
        StringTokenizer itr = new StringTokenizer(value.toString());
        while (itr.hasMoreTokens()) {
            word.set(itr.nextToken());
            context.write(word, one);
        }
    }
}
Drag options to blanks, or click blank then click option'
AObject
BText
CLongWritable
DIntWritable
Attempts:
3 left
💡 Hint
Common Mistakes
Using LongWritable for input key but not matching method signature.
Using IntWritable for output key which is incorrect.

Practice

(1/5)
1. What is the main purpose of the Map step in the Word Count MapReduce example?
easy
A. To split input text into pairs of (word, 1)
B. To sum the counts of each word
C. To sort the words alphabetically
D. To output the final word counts

Solution

  1. 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.
  2. Final Answer:

    To split input text into pairs of (word, 1) -> Option A
  3. Quick Check:

    Map step = (word, 1) pairs [OK]
Hint: Map creates (word, 1) pairs, Reduce sums counts [OK]
Common Mistakes:
  • Confusing Map with Reduce step
  • Thinking Map sorts words
  • Assuming Map sums counts
2. Which of the following is the correct syntax for the Mapper output in a Hadoop Word Count example?
easy
A. context.write(word, 1);
B. context.write(1, word);
C. write.context(word, 1);
D. context.write(word);

Solution

  1. Step 1: Recall Mapper output syntax

    In Hadoop MapReduce, the Mapper outputs key-value pairs using context.write(key, value);. For word count, the key is the word and the value is 1, so context.write(word, 1); is correct.
  2. Final Answer:

    context.write(word, 1); -> Option A
  3. Quick Check:

    Mapper output = context.write(word, 1) [OK]
Hint: Mapper outputs (word, 1) using context.write(word, 1) [OK]
Common Mistakes:
  • Swapping key and value order
  • Using incorrect method name
  • Omitting the value in output
3. Given the following input text: "cat dog cat bird dog cat", what will be the output of the Reduce step in the Word Count MapReduce example?
medium
A. [('cat', 3), ('dog', 3), ('bird', 1)]
B. [('cat', 3), ('dog', 2), ('bird', 1)]
C. [('cat', 2), ('dog', 3), ('bird', 1)]
D. [('cat', 1), ('dog', 1), ('bird', 1)]

Solution

  1. 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).
  2. Final Answer:

    [('cat', 3), ('dog', 2), ('bird', 1)] -> Option B
  3. Quick Check:

    Word counts = [('cat', 3), ('dog', 2), ('bird', 1)] [OK]
Hint: Count each word's frequency, sum in Reduce step [OK]
Common Mistakes:
  • Mixing counts of dog and cat
  • Counting only unique words once
  • Adding counts incorrectly
4. Identify the error in this Mapper code snippet for Word Count:
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));
  }
}
medium
A. The split method should use "," instead of space
B. The map method should return a value
C. IntWritable should be replaced with Integer
D. The key type in context.write should be Text, not String

Solution

  1. 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 variable word is a String, but context.write requires a Text object as key, so it should be new Text(word).
  2. Final Answer:

    The key type in context.write should be Text, not String -> Option D
  3. Quick Check:

    Use Text for keys in context.write [OK]
Hint: Use Hadoop types (Text, IntWritable) in context.write [OK]
Common Mistakes:
  • Passing String instead of Text as key
  • Using wrong split delimiter
  • Expecting map method to return a value
5. You want to modify the Word Count MapReduce job to ignore case (treat 'Cat' and 'cat' as the same word). Which change should you apply in the Mapper?
hard
A. Sort words alphabetically in the Mapper
B. Convert words to uppercase in the Reducer
C. Convert each word to lowercase before writing: context.write(new Text(word.toLowerCase()), new IntWritable(1));
D. Filter out words shorter than 3 characters in the Mapper

Solution

  1. 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.
  2. Final Answer:

    Convert each word to lowercase before writing: context.write(new Text(word.toLowerCase()), new IntWritable(1)); -> Option C
  3. Quick Check:

    Normalize case in Mapper for consistent keys [OK]
Hint: Lowercase words in Mapper to unify counts [OK]
Common Mistakes:
  • Changing case in Reducer instead of Mapper
  • Sorting words instead of normalizing case
  • Filtering words unrelated to case
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard