concept MapReduce program in category data
appears as: MapReduce program, MapReduce programs, A MapReduce program

This is an excerpt from Manning's book Hadoop in Action.
The list of (key/value) pairs is broken up and each individual (key/value) pair, <k1, v1>, is processed by calling the map function of the mapper. In practice, the key k1 is often ignored by the mapper. The mapper transforms each <k1, v1> pair into a list of <k2, v2> pairs. The details of this transformation largely determine what the MapReduce program does. Note that the (key/value) pairs are processed in arbitrary order. The transformation must be self-contained in that its output is dependent only on one single (key/value) pair. For word counting, our mapper takes <String filename, String file_ content> and promptly ignores filename. It can output a list of <String word, Integer count> but can be even simpler. As we know the counts will be aggregated in a later stage, we can output a list of <String word, Integer 1> with repeated entries and let the complete aggregation be done later. That is, in the output list we can have the (key/value) pair <"foo", 3> once or we can have the pair <"foo", 1> three times. As we’ll see, the latter approach is much easier to program. The former approach may have some performance benefits, but let’s leave such optimization alone until we have fully grasped the MapReduce framework.
As you’ve seen in our examples so far, a MapReduce program is often not very big, and you can keep a certain structure across them to simplify development. Most of the work is in thinking through the data flow.
We have discovered that patents 5312208, 4944640, and 5071294 cited patent 1000067. For this section we won’t focus too much on the MapReduce data flow, which we’ve already covered in chapter 3. Instead we focus on the structure of a MapReduce program. We need only one file for the entire program as you can see in listing 4.1.
Listing 5.5. A MapReduce program to create a Bloom filter
public class BloomFilterMR extends Configured implements Tool { public static class MapClass extends MapReduceBase implements Mapper<Text, Text, Text, BloomFilter<String>> { BloomFilter<String> bf = new BloomFilter<String>(); OutputCollector<Text, BloomFilter<String>> oc = null; public void map(Text key, Text value, OutputCollector<Text, BloomFilter<String>> output, Reporter reporter) throws IOException { if (oc == null) oc = output; bf.add(key.toString()); } public void close() throws IOException { oc.collect(new Text("testkey"), bf); } } public static class Reduce extends MapReduceBase implements Reducer<Text, BloomFilter<String>, Text, Text> { JobConf job = null; BloomFilter<String> bf = new BloomFilter<String>(); public void configure(JobConf job) { this.job = job; } public void reduce(Text key, Iterator<BloomFilter<String>> values, OutputCollector<Text, Text> output, Reporter reporter) throws IOException { while (values.hasNext()) { bf.union((BloomFilter<String>)values.next()); } } public void close() throws IOException { Path file = new Path(job.get("mapred.output.dir") + "/bloomfilter"); FSDataOutputStream out = file.getFileSystem(job).create(file); bf.write(out); out.close(); } } public int run(String[] args) throws Exception { Configuration conf = getConf(); JobConf job = new JobConf(conf, BloomFilterMR.class); Path in = new Path(args[0]); Path out = new Path(args[1]); FileInputFormat.setInputPaths(job, in); FileOutputFormat.setOutputPath(job, out); job.setJobName("Bloom Filter"); job.setMapperClass(MapClass.class); job.setReducerClass(Reduce.class); job.setNumReduceTasks(1); job.setInputFormat(KeyValueTextInputFormat.class); job.setOutputFormat(NullOutputFormat.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(BloomFilter.class); job.set("key.value.separator.in.input.line", ","); JobClient.runJob(job); return 0; } public static void main(String[] args) throws Exception { int res = ToolRunner.run(new Configuration(), new BloomFilterMR(), args); System.exit(res); } }