Apache MapReduce
MapReduce is the key
algorithm that the Hadoop data processing engine uses to distribute work around
a cluster. A MapReduce job splits a large data set into independent chunks and
organizes them into key, value pairs for parallel processing. This parallel
processing improves the speed and reliability of the cluster, returning
solutions more quickly and with greater reliability.
The Map function divides the input into ranges by the
InputFormat and creates a map task for each range in the input. The JobTracker
distributes those tasks to the worker nodes. The output of each map task is
partitioned into a group of key-value pairs for each reduce.
·
map(key1,value) -> list<key2,value2>
The Reduce function then collects the various results and
combines them to answer the larger problem that the master node needs to solve.
Each reduce pulls the relevant partition from the machines where the maps
executed, then writes its output back into HDFS. Thus, the reduce is able to
collect the data from all of the maps for the keys and combine them to solve
the problem.
·
reduce(key2, list<value2>) -> list<value3>
The current Apache
Hadoop MapReduce System is composed of the JobTracker, which is the master, and
the per-node slaves called TaskTrackers. The JobTracker is responsible for resource
management (managing the worker
nodes i.e. TaskTrackers), tracking resource consumption/availability and also job life-cycle management (scheduling individual tasks of the job,
tracking progress, providing fault-tolerance for tasks etc).
The TaskTracker has simple responsibilities –
launch/teardown tasks on orders from the JobTracker and provide task-status
information to the JobTracker periodically.
The Apache Hadoop projects provide a series of
tools designed to solve big data problems. The Hadoop cluster implements a
parallel computing cluster using inexpensive commodity hardware. The cluster is
partitioned across many servers to provide a near linear scalability. The philosophy
of the cluster design is to bring the computing to the data. So each datanode
will hold part of the overall data and be able to process the data that it
holds. The overall framework for the processing software is called MapReduce.
Mapreduce
Overview
Hadoop MapReduce is a software framework for easily writing applications which
process vast amounts of data (multi-terabyte data-sets) in-parallel on large
clusters (thousands of nodes) of commodity hardware in a reliable,
fault-tolerant manner.
A MapReduce
job usually splits the input data-set into independent chunks which are
processed by the map tasks in a completely parallel manner. The framework sorts
the outputs of the maps, which are then input to the reduce tasks. Typically
both the input and the output of the job are stored in a file-system. The
framework takes care of scheduling tasks, monitoring them and re-executes the
failed tasks. Typically the compute nodes and the storage nodes are the same,
that is, the MapReduce framework and the Hadoop Distributed File System (see
HDFS Architecture Guide) are running on the same set of nodes.a reliable,
fault-tolerant manner.
The
MapReduce framework consists of a single master JobTracker and one slave
TaskTracker per cluster-node. The master is responsible for scheduling the
jobs' component tasks on the slaves, monitoring them and re-executing the
failed tasks. The slaves execute the tasks as directed by the master.
Inputs and
Outputs:
The MapReduce framework operates exclusively
on pairs, that is, the framework views the input to the job as a set of pairs
and produces a set of pairs as the output of the job, conceivably of different
types. The key and value classes have to be serializable by the framework and
hence need to implement the Writable interface. Additionally, the key
classes have
to implement the WritableComparable interface to facilitate sorting by the
framework. Input and Output types of a MapReduce job: (input) -> map ->
-> combine -> -> reduce -> (output)
Example: Before we jump into the details,
lets walk through an example MapReduce application to get a flavour for how
they work. WordCount is a simple application that counts the number of
occurences of each word in a given input set.
Source Code
WordCount.java
1. package org.myorg;
2.3. import java.io.IOException;
4. import
java.util.*;
5. 6. import
org.apache.hadoop.fs.Path;
7. import org.apache.hadoop.conf.*;
8. import
org.apache.hadoop.io.*;
9. import org.apache.hadoop.mapred.*;
10. import org.apache.hadoop.util.*;
11.12. public class WordCount {
13.14.
public static class Map extends MapReduceBase implements Mapper {
15. private final static IntWritable one = new
IntWritable(1);
16. private
Text word = new Text();
17. 18.
public void map(LongWritable key, Text value, OutputCollector output, Reporter
reporter) throws IOException {
19. String
line = value.toString();
20. StringTokenizer tokenizer = new
StringTokenizer(line);
21. while (tokenizer.hasMoreTokens()) {
22.
word.set(tokenizer.nextToken());
23. output.collect(word, one);
24. }
25. }
26. }
27. 28. public static class Reduce extends
MapReduceBase implements Reducer {
29. public
void reduce(Text key, Iterator values, OutputCollector output, Reporter
reporter) throws IOException {
30. int sum = 0;
31. while (values.hasNext()) {
32. sum += values.next().get();
33. }
34. output.collect(key, new IntWritable(sum));
35. }
36. }
37. 38.
public static void main(String[] args) throws Exception {
39. JobConf
conf = new JobConf(WordCount.class); 40.
conf.setJobName("wordcount");
41. 42.
conf.setOutputKeyClass(Text.class);
43.
conf.setOutputValueClass(IntWritable.class);
44.45. conf.setMapperClass(Map.class);
46.
conf.setCombinerClass(Reduce.class);
47.
conf.setReducerClass(Reduce.class);
48. 49.
conf.setInputFormat(TextInputFormat.class);
50.
conf.setOutputFormat(TextOutputFormat.class);
51. 52.
FileInputFormat.setInputPaths(conf, new Path(args[0]));
53. FileOutputFormat.setOutputPath(conf, new Path(args[1]));
54. 55. JobClient.runJob(conf);
57. }
58. }
Usage
Assuming
HADOOP_HOME is the root of the installation and HADOOP_VERSION is the Hadoop
version installed, compile WordCount.java and create a jar:
$ mkdir wordcount_classes
$ javac -classpath
${HADOOP_HOME}/hadoop-${HADOOP_VERSION}- core.jar -d wordcount_classes
WordCount.java
$ jar -cvf
/usr/joe/wordcount.jar -C wordcount_classes/ .
Assuming that:
•
/usr/joe/wordcount/input - input directory in HDFS
• /usr/joe/wordcount/output - output directory
in HDFS
Sample
text-files as input:
$ bin/hadoop
dfs -ls /usr/joe/wordcount/input/
/usr/joe/wordcount/input/file01
/usr/joe/wordcount/input/file02
$ bin/hadoop
dfs -cat /usr/joe/wordcount/input/file01 Hello World Bye World
$ bin/hadoop
dfs -cat /usr/joe/wordcount/input/file02 Hello Hadoop Goodbye Hadoop
Run the application:
$ bin/hadoop
jar /usr/joe/wordcount.jar org.myorg.WordCount / usr/joe/wordcount/input
/usr/joe/wordcount/output
Output:
$ bin/hadoop dfs -cat /usr/joe/wordcount/output/part-00000
Bye 1
Goodbye 1
Hadoop 2
Hello 2
World 2
Walk-through:
The WordCount application is quite
straight-forward.
The Mapper
implementation (lines 14-26), via the map method (lines 18-25), processes one
line at a time, as provided by the specified TextInputFormat (line 49). It then
splits the line into tokens separated by whitespaces, via the StringTokenizer,
and emits a keyvalue pair of < , 1>.
For the
given sample input the first map emits:
< Hello, 1>
< World,
1>
< Bye, 1>
< World, 1>
The second
map emits:
< Hello,
1>
< Hadoop,
1>
<
Goodbye, 1>
< Hadoop,
1>
WordCount also specifies a combiner (line 46).
Hence, the output of each map is passed through the local combiner (which is
same as the Reducer as per the job configuration) for local aggregation, after
being sorted on the keys.
The output
of the first map:
< Bye,
1>
< Hello, 1>
< World,
2>
The output
of the second map:
< Goodbye, 1>
< Hadoop,
2>
< Hello, 1>
The Reducer implementation (lines 28-36), via
the reduce method (lines 29-35) just sums up the values, which are the
occurence counts for each key (i.e. words in this example).
Thus the
output of the job is:
< Bye,
1>
<
Goodbye, 1>
< Hadoop,
2>
< Hello,
2>
< World, 2>
The run method specifies various facets of the
job, such as the input/output paths (passed via the command line), key/value
types, input/output formats etc., in the JobConf. It then calls the
JobClient.runJob (line 55) to submit the and monitor its progress.
MapReduce -
User Interfaces
Applications
typically implement the Mapper and Reducer interfaces to provide the map and
reduce methods. These form the core of the job.
Mapper:
Mapper maps input key/value pairs to a set of
intermediate key/value pairs. Maps are the individual tasks that transform
input records into intermediate records. The transformed intermediate records
do not need to be of the same type as the input records. A given input pair may
map to zero or many output pairs. Overall, Mapper implementations are passed
the JobConf for the job via the JobConfigurable.configure(JobConf) method and
override it to initialize themselves. The framework then calls
map(WritableComparable, Writable, OutputCollector, Reporter) for each key/value
pair in the InputSplit for that task. Applications can then override the
Closeable.close() method to perform any required cleanup.Output pairs do not
need to be of the same types as input pairs. A given input pair may map to zero
or many output pairs. Output pairs are collected with calls to
OutputCollector.collect(WritableComparable,Writable). Applications can use the
Reporter to report progress, set application-level status messages and update
Counters, or just indicate that they are alive. All intermediate values
associated with a given output key are subsequently grouped by the framework,
and passed to the Reducer(s) to determine the final output. Users can control
the grouping by specifying a Comparator via
JobConf.setOutputKeyComparatorClass(Class). The Mapper outputs are sorted and
then partitioned per Reducer. The total number of partitions is the same as the
number of reduce tasks for the job. Users can control which keys (and hence
records) go to which Reducer by implementing a custom Partitioner. Users can
optionally specify a combiner, via JobConf.setCombinerClass(Class), to perform
local aggregation of the intermediate outputs, which helps to cut down the
amount of data transferred from the Mapper to the Reducer. The intermediate,
sorted outputs are always stored in a simple (key-len, key, value-len, value)
format. Applications can control if, and how, the intermediate outputs are to
be compressed and the CompressionCodec to be used via the JobConf.
How
Many Maps?
The number of maps is usually driven by the
total size of the inputs, that is, the total number of blocks of the input
files. The right level of parallelism for maps seems to be around 10-100 maps
per-node, although it has been set up to 300 maps for very cpu-light map tasks.
Task setup takes awhile, so it is best if the maps take at least a minute to
execute. Thus, if you expect 10TB of input data and have a blocksize of 128MB,
you'll end up with 82,000 maps, unless setNumMapTasks(int) (which only provides
a hint to the framework) is used to set it even higher.
Reducer:
Reducer reduces a set of intermediate values
which share a key to a smaller set of values. The number of reduces for the job
is set by the user via JobConf.setNumReduceTasks(int). Overall, Reducer
implementations are passed the JobConf for the job via the
JobConfigurable.configure(JobConf) method and can override it to initialize
themselves. The framework then calls reduce(WritableComparable, Iterator,
OutputCollector, Reporter) method for each pair in the grouped inputs.
Applications can then override the Closeable.close() method to perform any
required cleanup. Reducer has 3 primary phases: shuffle, sort and reduce.
Shuffle:
Input to the
Reducer is the sorted output of the mappers. In this phase the framework
fetches the relevant partition of the output of all the mappers, via HTTP.
Sort:
The framework groups Reducer inputs by keys
(since different mappers may have output the same key) in this stage. The
shuffle and sort phases occur simultaneously; while map-outputs are being
fetched they are merged.
Secondary
Sort:
If equivalence rules for grouping the
intermediate keys are required to be different from those for grouping keys
before reduction, then one may specify a Comparator via
JobConf.setOutputValueGroupingComparator(Class). Since
JobConf.setOutputKeyComparatorClass(Class) can be used to control how
intermediate keys are grouped, these can be used in conjunction to simulate
secondary sort on values.
Reduce:
In this
phase the reduce(WritableComparable, Iterator, OutputCollector, Reporter)
method is called for each pair in the grouped inputs. The output of the reduce
task is typically written to the FileSystem via
OutputCollector.collect(WritableComparable, Writable). Applications can use the
Reporter to report progress, set application-level status messages and update
Counters, or just indicate that they are alive. The output of the Reducer is not
sorted.
How
Many Reduces?
The right number of reduces seems to be 0.95
or 1.75 multiplied by ( * mapred.tasktracker.reduce.tasks.maximum). With 0.95
all of the reduces can launch immediately and start transfering map outputs as
the maps finish. With 1.75 the faster nodes will finish their first round of
reduces and launch a second wave of reduces doing a much better job of load
balancing. Increasing the number of reduces increases the framework overhead,
but increases load balancing and lowers the cost of failures. The scaling
factors above are slightly less than whole numbers to reserve a few reduce
slots in the framework for speculative-tasks and failed tasks.
Reducer NONE:
It is legal to set the number of reduce-tasks
to zero if no reduction is desired. In this case the outputs of the map-tasks
go directly to the FileSystem, into the output path set by setOutputPath(Path).
The framework does not sort the map-outputs before writing them out to the
FileSystem.
Partitioner :
Partitioner
partitions the key space. Partitioner controls the partitioning of the keys of
the intermediate map-outputs. The key (or a subset of the key) is used to
derive the partition, typically by a hash function. The total number of
partitions is the same as the number of reduce tasks for the job. Hence this
controls which of the m reduce tasks the intermediate key (and hence the
record) is sent to for reduction. HashPartitioner is the default Partitioner.
Reporter :
Reporter is
a facility for MapReduce applications to report progress, set application-level
status messages and update Counters. Mapper and Reducer implementations can use
the Reporter to report progress or just indicate that they are alive. In
scenarios where the application takes a significant amount of time to process
individual key/value pairs, this is crucial since the framework might assume
that the task has timed-out and kill that task. Another way to avoid this is to
set the configuration parameter mapred.task.timeout to a high-enough value (or
even set it to zero for no time-outs). Applications can also update Counters
using the Reporter.
OutputCollector:
OutputCollector is a generalization of the
facility provided by the MapReduce framework to collect data output by the
Mapper or the Reducer (either the intermediate outputs or the output of the
job). Hadoop MapReduce comes bundled with a library of generally useful
mappers, reducers, and partitioners.

Comments
Post a Comment