免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2786 | 回复: 0
打印 上一主题 下一主题

[Hadoop&HBase] Hadoop MapReduce Tutorial [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-20 09:45 |只看该作者 |倒序浏览
<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Times,serif; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">Hadoop MapReduce Tutorial<p>from<span class="Apple-converted-space">&nbsp;</span><a href="http://hadoop.apache.org/mapreduce/docs/r0.21.0/mapred_tutorial.html" target="_blank" target="_blank">MapReduce Tutorial</a></p><p>Hadoop 0.21 Documentation</p><p>The Hadoop Documentation provides the information you need to get started using Hadoop, the Hadoop Distributed File System (HDFS), and Hadoop on Demand (HOD).</p><p>MapReduce Tutorial</p><div id="outline-container-1" class="outline-2"><a name="o2b:d803c726-c7c5-42e7-b251-a2ba21469f65" id="o2b:d803c726-c7c5-42e7-b251-a2ba21469f65"></a><span class="section-number-2">1</span><span class="Apple-converted-space">&nbsp;</span>Purpose<div class="outline-text-2" id="text-1"><p>This document comprehensively describes all user-facing facets of the Hadoop MapReduce framework and serves as a tutorial.</p></div></div><div id="outline-container-2" class="outline-2"><span class="section-number-2">2</span><span class="Apple-converted-space">&nbsp;</span>Pre-requisites<div class="outline-text-2" id="text-2"><p>Ensure that Hadoop is installed, configured and is running. More details:</p><ul><li><a href="http://hadoop.apache.org/common/docs/r0.20.2/quickstart.html" target="_blank" target="_blank">Hadoop Quick Start</a><span class="Apple-converted-space">&nbsp;</span>for first-time users.</li><li><a href="http://hadoop.apache.org/common/docs/r0.20.2/cluster_setup.html" target="_blank" target="_blank">Hadoop Cluster Setup</a><span class="Apple-converted-space">&nbsp;</span>for large, distributed clusters.</li></ul></div></div><div id="outline-container-3" class="outline-2"><span class="section-number-2">3</span><span class="Apple-converted-space">&nbsp;</span>Overview<div class="outline-text-2" id="text-3"><p><span class="timestamp-wrapper"><span class="timestamp" style="color: rgb(190, 190, 190);">2011-03-03 Thu</span></span><br></p><ul><li>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.</li><li>A MapReduce<span class="Apple-converted-space">&nbsp;</span><i>job</i><span class="Apple-converted-space">&nbsp;</span>usually splits the input data-set into independent chunks which are processed by the<span class="Apple-converted-space">&nbsp;</span><i>map tasks</i><span class="Apple-converted-space">&nbsp;</span>in a completely parallel manner. The framework sorts the outputs of the maps, which are then input to the<span class="Apple-converted-space">&nbsp;</span><i>reduce tasks</i>. 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.</li><li>Typically the compute nodes and the storage nodes are the same, that is, the MapReduce framework and the Hadoop Distributed File System (see<span class="Apple-converted-space">&nbsp;</span><a href="http://hadoop.apache.org/common/docs/r0.20.2/hdfs_design.html" target="_blank" target="_blank">HDFS Architecture</a>) are running on the same set of nodes. This configuration allows the framework to effectively schedule tasks on the nodes where data is already present, resulting in very high aggregate bandwidth across the cluster.</li><li>The MapReduce framework consists of a single master<span class="Apple-converted-space">&nbsp;</span><b>JobTracker</b><span class="Apple-converted-space">&nbsp;</span>and one slave<span class="Apple-converted-space">&nbsp;</span><b>TaskTracker</b><span class="Apple-converted-space">&nbsp;</span>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 salves execute the tasks as directed by the master.</li><li>Minimally, applications specify the input/output locations and supply<span class="Apple-converted-space">&nbsp;</span><i>map</i><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><i>reduce</i><span class="Apple-converted-space">&nbsp;</span>functions via implementations or appropriate interfaces and/or abstract-classes. These, and other job parameters, comprise the<span class="Apple-converted-space">&nbsp;</span><i>job configuration</i>. The Hadoop<span class="Apple-converted-space">&nbsp;</span><i>job client</i><span class="Apple-converted-space">&nbsp;</span>then submits the job (jar/executable etc.) and configuration to the<span class="Apple-converted-space">&nbsp;</span><b>JobTracker</b><span class="Apple-converted-space">&nbsp;</span>which then assumes the responsibility of distributing the software/configuration to the slaves, scheduling tasks and monitoring them, providing status and diagnostic information to the job-client.</li><li>Although the Hadoop framework is implemented in Java<sup>TM</sup>, MapReduce application need not be written in Java.</li></ul></div></div><div id="outline-container-4" class="outline-2"><span class="section-number-2">4</span><span class="Apple-converted-space">&nbsp;</span>Inputs and Outputs<div class="outline-text-2" id="text-4"><ul><li>The MapReduce framework operates exclusively on<span class="Apple-converted-space">&nbsp;</span><b>&lt;key, value&gt;</b><span class="Apple-converted-space">&nbsp;</span>pairs, that is, the framework views the input to the job as a set of<span class="Apple-converted-space">&nbsp;</span><b>&lt;key, value&gt;</b><span class="Apple-converted-space">&nbsp;</span>pairs and produces a set of<span class="Apple-converted-space">&nbsp;</span><b>&lt;key, value&gt;</b><span class="Apple-converted-space">&nbsp;</span>pairs as the output of the job, conceivably of different types.</li><li>The<span class="Apple-converted-space">&nbsp;</span><b>key</b><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><b>value</b><span class="Apple-converted-space">&nbsp;</span>classes have to be serializable by the framework and hence need to implement the<span class="Apple-converted-space">&nbsp;</span><b>Writable</b><span class="Apple-converted-space">&nbsp;</span>interface. Additionally, the<span class="Apple-converted-space">&nbsp;</span><b>key</b><span class="Apple-converted-space">&nbsp;</span>classes have to implement the<span class="Apple-converted-space">&nbsp;</span><b>WritableComparable</b><span class="Apple-converted-space">&nbsp;</span>interface to facilitate sorting by the framework.</li><li>Input and Output types of a MapReduce job:</li></ul>(input) &lt;k1, v1&gt;-&gt; map -&gt; &lt;k2, v2&gt; -&gt; combine *-&gt; &lt;k2, v2&gt; -&gt; reduce -&gt; &lt;k3, v3&gt; (output)<br><p>Note that the combine phrase may run zero or more times in this process.</p></div></div><div id="outline-container-5" class="outline-2"><span class="section-number-2">5</span><span class="Apple-converted-space">&nbsp;</span>Example: WordCount v1.0<div class="outline-text-2" id="text-5"><ul><li>Before we jump into the details, lets walk through an example MapReduce application to get a flavor for how they work.</li><li><b>WordCount</b><span class="Apple-converted-space">&nbsp;</span>is a simple application that counts the number of occurrences of each word in a given input set.</li><li>This example works with a pseudo-distributed (<a href="http://hadoop.apache.org/common/docs/current/single_node_setup.html" target="_blank" target="_blank">Single Node Setup</a>) or fully-distributed (<a href="http://hadoop.apache.org/common/docs/current/cluster_setup.html" target="_blank" target="_blank">Cluster Setup</a>) Hadoop installation.</li></ul></div><div id="outline-container-5_1" class="outline-3"><span class="section-number-3">5.1</span><span class="Apple-converted-space">&nbsp;</span>Source Code<div class="outline-text-3" id="text-5_1"><p><span class="timestamp-wrapper"><span class="timestamp" style="color: rgb(190, 190, 190);">2011-03-04 Fri</span></span><br></p>1.      package org.myorg;<br>2.      <br>3.      import java.io.IOException;<br>4.      import java.util.*;<br>5.      <br>6.      import org.apache.hadoop.fs.Path;<br>7.      import org.apache.hadoop.conf.*;<br>8.      import org.apache.hadoop.io.*;<br>9.      import org.apache.hadoop.mapreduce.*;<br>10.     import org.apache.hadoop.mapreduce.lib.input.*;<br>11.     import org.apache.hadoop.mapreduce.lib.output.*;<br>12.     import org.apache.hadoop.util.*;<br>13.     <br>14.     public class WordCount extends Configured implements Tool {<br>15.     <br>16.        public static class Map<br>17.            extends Mapper&lt;LongWritable, Text, Text, IntWritable&gt; {<br>18.          private final static IntWritable one = new IntWritable(1);<br>19.          private Text word = new Text();<br>20.     <br>21.          public void map(LongWritable key, Text value, Context context)<br>22.              throws IOException, InterruptedException {<br>23.            String line = value.toString();<br>24.            StringTokenizer tokenizer = new StringTokenizer(line);<br>25.            while (tokenizer.hasMoreTokens()) {<br>26.              word.set(tokenizer.nextToken());<br>27.              context.write(word, one);<br>28.            }<br>29.          }<br>30.        }<br>31.     <br>32.        public static class Reduce<br>33.            extends Reducer&lt;Text, IntWritable, Text, IntWritable&gt; {<br>34.          public void reduce(Text key, Iterable&lt;IntWritable&gt; values,<br>35.              Context context) throws IOException, InterruptedException {<br>36.     <br>37.            int sum = 0;<br>38.            for (IntWritable val : values) {<br>39.              sum += val.get();<br>40.            }<br>41.            context.write(key, new IntWritable(sum));<br>42.          }<br>43.        }<br>44.     <br>45.        public int run(String [] args) throws Exception {<br>46.          Job job = new Job(getConf());<br>47.          job.setJarByClass(WordCount.class);<br>48.          job.setJobName("wordcount");<br>49.     <br>50.          job.setOutputKeyClass(Text.class);<br>51.          job.setOutputValueClass(IntWritable.class);<br>52.     <br>53.          job.setMapperClass(Map.class);<br>54.          job.setCombinerClass(Reduce.class);<br>55.          job.setReducerClass(Reduce.class);<br>56.     <br>57.          job.setInputFormatClass(TextInputFormat.class);<br>58.          job.setOutputFormatClass(TextOutputFormat.class);<br>59.     <br>60.          FileInputFormat.setInputPaths(job, new Path(args[0]));<br>61.          FileOutputFormat.setOutputPath(job, new Path(args[1]));<br>62.     <br>63.          boolean success = job.waitForCompletion(true);<br>64.          return success ? 0 : 1;<br>65.        }<br>66.     <br>67.        public static void main(String[] args) throws Exception {<br>68.          int ret = ToolRunner.run(new WordCount(), args);<br>69.          System.exit(ret);<br>70.        }<br>71.     }<br>72.     <br></div></div><div id="outline-container-5_2" class="outline-3"><span class="section-number-3">5.2</span><span class="Apple-converted-space">&nbsp;</span>Usage<div class="outline-text-3" id="text-5_2"><p>(1) Compile<span class="Apple-converted-space">&nbsp;</span><b>WordCount.java</b><span class="Apple-converted-space">&nbsp;</span>and create a jar:</p>$ cat hadoopjar<br># hadoopjar: compile WordCount.java and create a jar<br># written by mht on Mar 4, 2011<br><br>cd hadoop-0.20.2<br><br>mkdir wordcount_classes<br><br>export HADOOP_HOME=~/hadoop-0.20.2<br>export HADOOP_VERSION="0.20.2"<br><br>#echo $HADOOP_VERSION<br><br>javac -classpath ${HADOOP_HOME}/hadoop-${HADOOP_VERSION}-core.jar:<br>${HADOOP_HOME}/hadoop-${HADOOP_VERSION}-mapred.jar:<br>${HADOOP_HOME}/hadoop-${HADOOP_VERSION}-hdfs.jar <br>-d wordcount_classes wordcount_src/WordCount.java<br><br>export JAVA_HOME=/usr/local/jdk1.6.0_17<br>export CLASSPATH=${JAVA_HOME}/lib/tools.jar<br><br>jar -cvf wordcount_jar/wordcount.jar -C wordcount_classes/ .<br>$<br><p>(2) Sample text-files as input:</p>$ ls input<br>file01 file02<br><br>$ cat input/file01<br>Hello World Bye World<br><br>$ cat input/file02<br>Hello Hadoop Goodbye Hadoop<br><p>(3) Run the application:</p>$ bin/hadoop jar wordcount_jar/wordcount.jar org.myorg.WordCount input output<br><p>(4) Output:</p>$ cat output/part-r-00000<br>Bye     1<br>Goodbye 1<br>Hadoop  2<br>Hello   2<br>World   2<br></div></div><div id="outline-container-5_3" class="outline-3"><span class="section-number-3">5.3</span><span class="Apple-converted-space">&nbsp;</span>Bundling a data payload with your application<div class="outline-text-3" id="text-5_3"><ul><li>Applications can specify a comma-separated list of paths which would be present in the current working directory of the task using the option<span class="Apple-converted-space">&nbsp;</span><b>-files</b>. The<span class="Apple-converted-space">&nbsp;</span><b>-libjars</b><span class="Apple-converted-space">&nbsp;</span>option allows applications to add jars to the classpaths of the maps and reduces. The option<span class="Apple-converted-space">&nbsp;</span><b>-archives</b><span class="Apple-converted-space">&nbsp;</span>allows them to pass comma separated list of archives as arguments. These archives are unarchived and a link with name of the archive is created in the current working idrectory of tasks.</li><li>The mechanism that provides this functionality is called the<span class="Apple-converted-space">&nbsp;</span><i>distributed cache</i>. More detains about the command line options surrounding job launching and control of the distributed cache are avaiable at [[][Hadoop Commands Guide]].</li><li>Hadoop ships with some example code in a jar precompiled for you; one of these is (another) wordcount program. Here's an example invocation of the<span class="Apple-converted-space">&nbsp;</span><b>wordcount</b><span class="Apple-converted-space">&nbsp;</span>example with<span class="Apple-converted-space">&nbsp;</span><b>-libjars</b>,<span class="Apple-converted-space">&nbsp;</span><b>-files</b><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><b>-archives</b>;</li></ul>hadoop jar hadoop-examples.jar wordcount -files cachefile.txt <br>-libjars mylib.jar -archives myarchive.zip input output<br><p>Here, myarchive.zip will be placed and unzipped into a directory by the name "myarchive.zip".</p><ul><li>Users can specify a different symbolic name for files and archives passed through -files and -archives option, using #.</li></ul><p>For example,</p>hadoop jar hadoop-examples.jar wordcount <br>-files dir1/dict.txt#dict1, dir2/dict.txt#dict2 <br>-archives mytar.tgz#tgzdir input output<br><p>Here, the files dir1/dict.txt and dir2/dict.txt can be accessed by tasks using the symbolic names dict1 and dict2 respectively. And the archive mytar.tgz will be placed and unarchived into a directory by the name tgzdir.</p></div></div><div id="outline-container-5_4" class="outline-3"><span class="section-number-3">5.4</span><span class="Apple-converted-space">&nbsp;</span>Walk-through<div class="outline-text-3" id="text-5_4"><p><span class="timestamp-wrapper"><span class="timestamp" style="color: rgb(190, 190, 190);">2011-03-06 Sun</span></span><br>This section describes the operation of the<span class="Apple-converted-space">&nbsp;</span><b>WordCount</b><span class="Apple-converted-space">&nbsp;</span>application shown earlier in this tutorial.</p><p>The<span class="Apple-converted-space">&nbsp;</span><b>Mapper</b><span class="Apple-converted-space">&nbsp;</span>implementation (lines 16-30), via the<span class="Apple-converted-space">&nbsp;</span><b>map</b><span class="Apple-converted-space">&nbsp;</span>method (lines 21-29), processes one line at a time, as provided by the specified<span class="Apple-converted-space">&nbsp;</span><b>TextInputFormat</b>. It then splits the line into tokens separated by whitespaces, via the<b>StringTokenizer</b>, and emits a key-value pair of<span class="Apple-converted-space">&nbsp;</span>&lt;&lt;word&gt;, 1&gt;.</p><p>For the given sample input the first map emits:</p>&lt;Hello, 1&gt;<br>&lt;World, 1&gt;<br>&lt;Bye, 1&gt;<br>&lt;World, 1&gt;<br><p>The second map emits:</p>&lt;Hello, 1&gt;<br>&lt;Hadoop, 1&gt;<br>&lt;Goodbye, 1&gt;<br>&lt;Hadoop, 1&gt;<br><p><b>WordCount</b><span class="Apple-converted-space">&nbsp;</span>also specifies a<span class="Apple-converted-space">&nbsp;</span><b>combiner</b><span class="Apple-converted-space">&nbsp;</span>(line 54). Hence, the output of each map is passed through the local combiner (which is same as the<span class="Apple-converted-space">&nbsp;</span><b>Reducer</b><span class="Apple-converted-space">&nbsp;</span>as per the job configuration) for local aggregation, after being sorted on the<span class="Apple-converted-space">&nbsp;</span><i>keyss</i>.</p><p>The output of the first map:</p>&lt;Bye, 1&gt;<br>&lt;Hello, 1&gt;<br>&lt;World, 2&gt;<br><p>The output of the second map:</p>&lt;Goodbye, 1&gt;<br>&lt;Hadoop, 2&gt;<br>&lt;Hello, 1&gt;<br><p>The<span class="Apple-converted-space">&nbsp;</span><b>Reducer</b><span class="Apple-converted-space">&nbsp;</span>implementation (lines 32-43), via the<span class="Apple-converted-space">&nbsp;</span><b>reduce</b><span class="Apple-converted-space">&nbsp;</span>method (lines 34-42) just sums up the values, which are the occurence counts for each key (i.e. words in this example).</p><p>Thus the output of the job is:</p>&lt;Bye, 1&gt;<br>&lt;Goodbye, 1&gt;<br>&lt;Hadoop, 2&gt;<br>&lt;Hello, 2&gt;<br>&lt;World, 2&gt;<br><p>The<span class="Apple-converted-space">&nbsp;</span><b>run</b><span class="Apple-converted-space">&nbsp;</span>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<span class="Apple-converted-space">&nbsp;</span><b>Job</b>. It then calls the<span class="Apple-converted-space">&nbsp;</span><b>Job.waitForCompletion()</b><span class="Apple-converted-space">&nbsp;</span>(line 63) to submit the job to Hadoop and monitor its progress.</p></div></div></div><div id="outline-container-6" class="outline-2"><span class="section-number-2">6</span><span class="Apple-converted-space">&nbsp;</span>MapReduce - User Interfaces<div class="outline-text-2" id="text-6"><p>This section provides a reasonalbe amout of detail on every user-facing aspect of the MapReduce framework. This should help users implement, configure and tune their jobs in a fine-grained manner.</p><p><span class="timestamp-wrapper"><span class="timestamp" style="color: rgb(190, 190, 190);">2011-03-06 Sun</span></span><br></p></div><div id="outline-container-6_1" class="outline-3"><span class="section-number-3">6.1</span><span class="Apple-converted-space">&nbsp;</span>Payload<div class="outline-text-3" id="text-6_1"><p>Applications typically extend the<span class="Apple-converted-space">&nbsp;</span><b>Mapper</b><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><b>Reducer</b><span class="Apple-converted-space">&nbsp;</span>classes to provide the<span class="Apple-converted-space">&nbsp;</span><b>map</b><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><b>reduce</b><span class="Apple-converted-space">&nbsp;</span>methods. Thsese form the core of the job.</p></div><div id="outline-container-6_1_1" class="outline-4"><span class="section-number-4">6.1.1</span><span class="Apple-converted-space">&nbsp;</span>Mapper<div class="outline-text-4" id="text-6_1_1"><p><b>Mapper</b><span class="Apple-converted-space">&nbsp;</span>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 Hadoop MapReduce framewrok spawns one map task for each<b>InputSplit</b><span class="Apple-converted-space">&nbsp;</span>generated by the<span class="Apple-converted-space">&nbsp;</span><b>InputFormat</b><span class="Apple-converted-space">&nbsp;</span>for the job.</p><p>An<span class="Apple-converted-space">&nbsp;</span><b>InputSplit</b><span class="Apple-converted-space">&nbsp;</span>is a logical representation of a unit of input work for a map task; e.g., a filename and a byte range within that file to process. The<span class="Apple-converted-space">&nbsp;</span><b>InputFormat</b><span class="Apple-converted-space">&nbsp;</span>is responsible for enumerating the<span class="Apple-converted-space">&nbsp;</span><b>InputSplits</b>, and producting a<b>RecordReader</b><span class="Apple-converted-space">&nbsp;</span>which will turn those logical work units into actual physical input records.</p><ul><li>Overall,<span class="Apple-converted-space">&nbsp;</span><b>Mapper</b><span class="Apple-converted-space">&nbsp;</span>implementations are specified in the<span class="Apple-converted-space">&nbsp;</span><b>Job</b>, a client-side class that describes the job's configuration and interfaces with the cluster on behalf of the client program. The<span class="Apple-converted-space">&nbsp;</span><b>Mapper</b><span class="Apple-converted-space">&nbsp;</span>itself then is instantiated in the running job, and is passed a<span class="Apple-converted-space">&nbsp;</span><b>MapContext</b><span class="Apple-converted-space">&nbsp;</span>object which it can use to configure itself. The<span class="Apple-converted-space">&nbsp;</span><b>Mapper</b><span class="Apple-converted-space">&nbsp;</span>contains a<span class="Apple-converted-space">&nbsp;</span><b>run()</b><span class="Apple-converted-space">&nbsp;</span>method which calls its<span class="Apple-converted-space">&nbsp;</span><b>setup()</b><span class="Apple-converted-space">&nbsp;</span>method once, its<span class="Apple-converted-space">&nbsp;</span><b>map()</b><span class="Apple-converted-space">&nbsp;</span>method for each input record, and finally its<span class="Apple-converted-space">&nbsp;</span><b>cleanup()</b>method. All of these methods (including<span class="Apple-converted-space">&nbsp;</span><b>run()</b><span class="Apple-converted-space">&nbsp;</span>itself) can be overridden with your own code. If you do not override any methds (leaving even map as-is), it will act as the<span class="Apple-converted-space">&nbsp;</span><i>identity function</i>, emmitting each input record as a separate output.</li></ul><p>(1) The<span class="Apple-converted-space">&nbsp;</span><b>Context</b><span class="Apple-converted-space">&nbsp;</span>object allows the mapper to interact with the rest of the Hadoop system. It includes configuration data for the job, as well as interfaces which allow it to emit output. The<span class="Apple-converted-space">&nbsp;</span><b>getConfiguration()</b><span class="Apple-converted-space">&nbsp;</span>method returns a<b>Configuration</b><span class="Apple-converted-space">&nbsp;</span>which contains configuration data for your program. You can set arbitrary (key, value) pairs of configuration data in your<span class="Apple-converted-space">&nbsp;</span><b>Job</b>, e.g. with<span class="Apple-converted-space">&nbsp;</span><b>Job.getConfiguration().set("myKey", "myval")</b>, and then retrieve this data in your mapper with<span class="Apple-converted-space">&nbsp;</span><b>Context.getConfiguration().get("myKey")</b>. This sort of functionality is typically done in the Mapper's<span class="Apple-converted-space">&nbsp;</span><b>setup()</b><span class="Apple-converted-space">&nbsp;</span>method.</p><p>(2) The<span class="Apple-converted-space">&nbsp;</span><b>Mapper.run()</b><span class="Apple-converted-space">&nbsp;</span>method then calls<span class="Apple-converted-space">&nbsp;</span><b>map(KeyInType, ValInType, Context)</b><span class="Apple-converted-space">&nbsp;</span>for each key/value pair in the<span class="Apple-converted-space">&nbsp;</span><b>InputSplit</b><span class="Apple-converted-space">&nbsp;</span>for that task. Note that in the WordCount program's map() method, we then emit our output data via the<b>Context</b><span class="Apple-converted-space">&nbsp;</span>argument, using its<span class="Apple-converted-space">&nbsp;</span><b>write()</b><span class="Apple-converted-space">&nbsp;</span>method.</p><p>(3) Applications can then override the Mapper's<span class="Apple-converted-space">&nbsp;</span><b>Cleanup()</b><span class="Apple-converted-space">&nbsp;</span>method to perform any required teardown operations.</p><p>(4) Output pairs are collected with calls to<span class="Apple-converted-space">&nbsp;</span><b>Context.write(KeyOutType, ValOutType)</b>.</p><p>(5) Applications can also use the<span class="Apple-converted-space">&nbsp;</span><b>Context</b><span class="Apple-converted-space">&nbsp;</span>to report progress, set application-level status messages and update<span class="Apple-converted-space">&nbsp;</span><b>Counters</b>, or just indicate that they are alive.</p><p>(6) All intermediate values associated with a given output key are subsequently grouped by the framework, and passed to the<span class="Apple-converted-space">&nbsp;</span><b>Reducer(s)</b><span class="Apple-converted-space">&nbsp;</span>to determine the final output. Users can control the grouping by specifying a<span class="Apple-converted-space">&nbsp;</span><b>Comparator</b><span class="Apple-converted-space">&nbsp;</span>via<b>Job.setGroupingComparatorClass(class)</b>.</p><p>(7) The<span class="Apple-converted-space">&nbsp;</span><b>Mapper</b><span class="Apple-converted-space">&nbsp;</span>outputs are sorted and partitioned per<span class="Apple-converted-space">&nbsp;</span><b>Reducer</b>. 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<span class="Apple-converted-space">&nbsp;</span><b>Reducer</b><span class="Apple-converted-space">&nbsp;</span>by implementing a custom<span class="Apple-converted-space">&nbsp;</span><b>Partitioner</b>.</p><p>(8) Users can optionally specify a<span class="Apple-converted-space">&nbsp;</span><b>combiner</b>, via<span class="Apple-converted-space">&nbsp;</span><b>Job.setCombinerClass(Class)</b>, to perform local aggregation of the intermediate outputs, which helps to cut down the amount of data transferred from the<span class="Apple-converted-space">&nbsp;</span><b>Mapper</b><span class="Apple-converted-space">&nbsp;</span>to the<span class="Apple-converted-space">&nbsp;</span><b>Reducer</b>.</p><p>(9) The intermediate, sorted outputs are always stored in a simle (key-len, key, value-len, value) format. Applications can control if, and how, the intermediate outputs are to be compressed and the<span class="Apple-converted-space">&nbsp;</span><b>CompressionCodec</b><span class="Apple-converted-space">&nbsp;</span>to be used via the<span class="Apple-converted-space">&nbsp;</span><b>Job</b>.</p><p><b>How Many Maps?</b></p><ul><li>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.</li><li>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.</li><li>Thus, if you expect 10TB of input data and have a blocksize of 128MB, you'll end up with 82,000 maps, unless the<span class="Apple-converted-space">&nbsp;</span><b>mapreduce.job.maps</b><span class="Apple-converted-space">&nbsp;</span>parameter (which only provides a hint to the framework) is used to set it even higher. Ultimately, the number of tasks is controlled by the number of splits returned by the<span class="Apple-converted-space">&nbsp;</span><b>InputFormat.getSplits()</b><span class="Apple-converted-space">&nbsp;</span>method.</li></ul></div></div><div id="outline-container-6_1_2" class="outline-4"><span class="section-number-4">6.1.2</span><span class="Apple-converted-space">&nbsp;</span>Reducer<div class="outline-text-4" id="text-6_1_2"><p><span class="timestamp-wrapper"><span class="timestamp" style="color: rgb(190, 190, 190);">2011-03-07 Mon</span></span><br></p><p><b>Reducer</b><span class="Apple-converted-space">&nbsp;</span>reduces a set of intermediate values which share a key to a (usually smaller) set of values. The number of reduces for the job is set by the user via<span class="Apple-converted-space">&nbsp;</span><b>Job.setNumReduceTasks(int)</b>.</p><p>The API of<span class="Apple-converted-space">&nbsp;</span><b>Reducer</b><span class="Apple-converted-space">&nbsp;</span>is very similar to that of<span class="Apple-converted-space">&nbsp;</span><b>Mapper</b>; there's a<span class="Apple-converted-space">&nbsp;</span><b>run()</b><span class="Apple-converted-space">&nbsp;</span>method that receives a<span class="Apple-converted-space">&nbsp;</span><b>Context</b><span class="Apple-converted-space">&nbsp;</span>containing the job's configuration as well as interfacing methods that return data from the reducer itself back to the framework. The<b>run()</b><span class="Apple-converted-space">&nbsp;</span>method calls<span class="Apple-converted-space">&nbsp;</span><b>setup()</b><span class="Apple-converted-space">&nbsp;</span>once,<span class="Apple-converted-space">&nbsp;</span><b>reduce()</b><span class="Apple-converted-space">&nbsp;</span>once for each key associated with the reduce task, and<span class="Apple-converted-space">&nbsp;</span><b>cleanup()</b><span class="Apple-converted-space">&nbsp;</span>once at the end. Each of these methods can access the job's configuration data by using<span class="Apple-converted-space">&nbsp;</span><b>Context.getconfiguration()</b>.</p><p>The heart of<span class="Apple-converted-space">&nbsp;</span><b>Reducer</b><span class="Apple-converted-space">&nbsp;</span>is its<span class="Apple-converted-space">&nbsp;</span><b>reduce()</b><span class="Apple-converted-space">&nbsp;</span>method. This is called once per key; they second argument is an<span class="Apple-converted-space">&nbsp;</span><b>Iterable</b><span class="Apple-converted-space">&nbsp;</span>which returns all the values associated with that key. The Reducer should emit its final output (key, value) pairs with the<b>Context.write()</b><span class="Apple-converted-space">&nbsp;</span>method. It may emit 0, 1, or more (key, value) pairs for each input.</p><p><b>Reducer</b><span class="Apple-converted-space">&nbsp;</span>has 3 primary phases: shuffle, sort and reduce.</p><ul><li id="sec-6_1_2_1">Shuffle<span class="Apple-converted-space">&nbsp;</span><br>Input to the<span class="Apple-converted-space">&nbsp;</span><b>Reducer</b><span class="Apple-converted-space">&nbsp;</span>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.</li></ul><ul><li id="sec-6_1_2_2">Sort<span class="Apple-converted-space">&nbsp;</span><br>The framework groups<span class="Apple-converted-space">&nbsp;</span><b>Reducer</b><span class="Apple-converted-space">&nbsp;</span>inputs by keys (since different mappers may have output the same key) in this stage.<p>The shuffle and sort phase occur simultaneously; while map-outputs are being fetched they are merged.</p></li></ul><ul><li id="sec-6_1_2_3">Secondary Sort<span class="Apple-converted-space">&nbsp;</span><br><p>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<span class="Apple-converted-space">&nbsp;</span><b>Comarator</b><span class="Apple-converted-space">&nbsp;</span>via<span class="Apple-converted-space">&nbsp;</span><b>Job.setGroupingComparatorClass(Class)</b>. Since this can be used to control how intermediate keys are grouped, these can be used in conjunction to simulate<span class="Apple-converted-space">&nbsp;</span><i>secondary sort on values</i>.</p></li></ul><ul><li id="sec-6_1_2_4">Reduce<span class="Apple-converted-space">&nbsp;</span><br>In this phase the<span class="Apple-converted-space">&nbsp;</span><b>reduce(MapOutKeyType, Iterable &lt;MapOutValType&gt;, Context)</b><span class="Apple-converted-space">&nbsp;</span>method is called for each<span class="Apple-converted-space">&nbsp;</span><b>&lt;key, (list of values)&gt;</b><span class="Apple-converted-space">&nbsp;</span>pair in the grouped inputs.<p>The output of the reduce task is typically written to the<span class="Apple-converted-space">&nbsp;</span><b>FileSystem</b><span class="Apple-converted-space">&nbsp;</span>via<span class="Apple-converted-space">&nbsp;</span><b>Context.write(ReduceKeyType, ReduceOutValType)</b>.</p><p>Applications can use the<span class="Apple-converted-space">&nbsp;</span><b>Context</b><span class="Apple-converted-space">&nbsp;</span>to report progress, set application-level status messages and update<span class="Apple-converted-space">&nbsp;</span><b>counters</b>, or just indicate that they are alive.</p><p>The output of the<span class="Apple-converted-space">&nbsp;</span><b>Reducer</b><span class="Apple-converted-space">&nbsp;</span>is<span class="Apple-converted-space">&nbsp;</span><i>not sorted</i>.</p></li></ul></div></div><div id="outline-container-6_1_3" class="outline-4"><span class="section-number-4">6.1.3</span><span class="Apple-converted-space">&nbsp;</span>How Many Reduces?<div class="outline-text-4" id="text-6_1_3"><p>The right number of reduces seems to be<span class="Apple-converted-space">&nbsp;</span><b>0.95</b><span class="Apple-converted-space">&nbsp;</span>or<span class="Apple-converted-space">&nbsp;</span><b>1.75</b><span class="Apple-converted-space">&nbsp;</span>multiplied by (<i>&lt;no. of nodes&gt;</i><span class="Apple-converted-space">&nbsp;</span>* mapreduce.tasktracker.reduce.tasks.maximum*).</p><p>With<span class="Apple-converted-space">&nbsp;</span><b>0.95</b><span class="Apple-converted-space">&nbsp;</span>all of the reduces can launch immediately and start transfering map outputs as the maps finish. With<span class="Apple-converted-space">&nbsp;</span><b>1.75</b><span class="Apple-converted-space">&nbsp;</span>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.</p><p>Increasing the number of reduces increases the framework overhead, but increases load balancing and lowers the cost of failures.</p><p>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.</p></div></div><div id="outline-container-6_1_4" class="outline-4"><span class="section-number-4">6.1.4</span><span class="Apple-converted-space">&nbsp;</span>Reducer NONE<div class="outline-text-4" id="text-6_1_4"><p>It is legal to set the number of reduce-tasks to<span class="Apple-converted-space">&nbsp;</span><i>zero</i><span class="Apple-converted-space">&nbsp;</span>if no reduction is desired.</p><p>In this case the outputs of the map-tasks go directly to the<span class="Apple-converted-space">&nbsp;</span><b>FileSystem</b>, into the output path set by<span class="Apple-converted-space">&nbsp;</span><b>setOutputPath(Path)</b>. The framework does not sort the map-outputs before writing them out to the<span class="Apple-converted-space">&nbsp;</span><b>FileSystem</b>..</p></div></div><div id="outline-container-6_1_5" class="outline-4"><span class="section-number-4">6.1.5</span><span class="Apple-converted-space">&nbsp;</span>Mark-Reset<div class="outline-text-4" id="text-6_1_5"><p>While applications iterate through the values for a given key, it is possible to mark the current position and later reset the iterator to this position and continue the iteration process. The corresponding methods are<span class="Apple-converted-space">&nbsp;</span><b>mark()</b><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><b>reset()</b>.</p><p><b>mark()</b><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><b>reset()</b><span class="Apple-converted-space">&nbsp;</span>can be called any number of times during the iteration cycle. The<span class="Apple-converted-space">&nbsp;</span><b>reset()</b><span class="Apple-converted-space">&nbsp;</span>method will reset the iterator to the last record before a call to the previous<span class="Apple-converted-space">&nbsp;</span><b>mark()</b>.</p><p>This functionality is avaiable only with the new context based reduce iterator.</p></div></div><div id="outline-container-6_1_6" class="outline-4"><span class="section-number-4">6.1.6</span><span class="Apple-converted-space">&nbsp;</span>Partitioner<div class="outline-text-4" id="text-6_1_6"><p><b>Partitioner</b><span class="Apple-converted-space">&nbsp;</span>partitions the key space.</p><p>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<span class="Apple-converted-space">&nbsp;</span><i>hash function</i>. The total number of partitions is the same as the number of reduce tasks for the job. Hence this controls which of the<span class="Apple-converted-space">&nbsp;</span><b>m</b><span class="Apple-converted-space">&nbsp;</span>reduce tasks the intermediate key (and hence the record) is sent to for the reduction.</p><p><b>HashPatitioner</b><span class="Apple-converted-space">&nbsp;</span>is the default<span class="Apple-converted-space">&nbsp;</span><b>Partitioner</b>.</p></div></div></div><div id="outline-container-6_2" class="outline-3"><span class="section-number-3">6.2</span><span class="Apple-converted-space">&nbsp;</span>Reporting Progress<div class="outline-text-3" id="text-6_2"><p>Via the mapper or reducer's Context, MapReduce applications can report progress, set application-level status messages and update<span class="Apple-converted-space">&nbsp;</span><b>Counters</b>.</p><p><b>Mapper</b><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><b>Reducer</b><span class="Apple-converted-space">&nbsp;</span>implementations can use the<span class="Apple-converted-space">&nbsp;</span><b>Context</b><span class="Apple-converted-space">&nbsp;</span>to report progress or just indicate that they are alive.</p><p>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 time-out and kill that task. Another way to avoid this is to set the configuration parameter<span class="Apple-converted-space">&nbsp;</span><b>mapreduce.task.timeout</b><span class="Apple-converted-space">&nbsp;</span>to a high-enough value (or even set it to<span class="Apple-converted-space">&nbsp;</span><i>zero</i><span class="Apple-converted-space">&nbsp;</span>for no time-outs).</p><p>Applications can also update<span class="Apple-converted-space">&nbsp;</span><b>Counters</b><span class="Apple-converted-space">&nbsp;</span>using the<span class="Apple-converted-space">&nbsp;</span><b>Context</b>.</p><p>Hadoop MapReduce comes bundled with a library of generally useful mappers, reducers, and partitioners in the<span class="Apple-converted-space">&nbsp;</span><b>org.apache.hadoop.mapreduce.lib</b><span class="Apple-converted-space">&nbsp;</span>package.</p></div></div><div id="outline-container-6_3" class="outline-3"><span class="section-number-3">6.3</span><span class="Apple-converted-space">&nbsp;</span>Job configuration<div class="outline-text-3" id="text-6_3"><p><span class="timestamp-wrapper"><span class="timestamp" style="color: rgb(190, 190, 190);">2011-03-07 Mon</span></span><br>The<span class="Apple-converted-space">&nbsp;</span><b>Job</b><span class="Apple-converted-space">&nbsp;</span>represents a MapReduce job configuration. The actual state for this object is written to an underlying instance of<span class="Apple-converted-space">&nbsp;</span><b>Configuration</b>.</p><p><b>Job</b><span class="Apple-converted-space">&nbsp;</span>is the primary interface for a user to describe a MapReduce job to the Hadoop framework for execution.</p><p>The<span class="Apple-converted-space">&nbsp;</span><b>Job</b><span class="Apple-converted-space">&nbsp;</span>is typically used to specify the<span class="Apple-converted-space">&nbsp;</span><b>Mapper</b>, combiner (if any),<span class="Apple-converted-space">&nbsp;</span><b>Partitioner</b>,<span class="Apple-converted-space">&nbsp;</span><b>Reducer</b>,<span class="Apple-converted-space">&nbsp;</span><b>InputFormat</b>,<span class="Apple-converted-space">&nbsp;</span><b>OutputFormat</b><span class="Apple-converted-space">&nbsp;</span>and<span class="Apple-converted-space">&nbsp;</span><b>OutputCommitter</b><span class="Apple-converted-space">&nbsp;</span>implementations.<span class="Apple-converted-space">&nbsp;</span><b>Job</b><span class="Apple-converted-space">&nbsp;</span>also indicates the set of input files (<b>setInputPaths(Job, Path…)</b>)/*addInputPath(Job,Path))* and<span class="Apple-converted-space">&nbsp;</span><b>(setInputPaths(Job, String)*/*(addInputPaths(Job,String))</b><span class="Apple-converted-space">&nbsp;</span>and where the output files should be written (<b>setOutputPath(Path)</b>).</p><p>Of course, users can use<span class="Apple-converted-space">&nbsp;</span><b>Job.getConfiguration()</b><span class="Apple-converted-space">&nbsp;</span>to get access to the underlying configuration state, and can then use<span class="Apple-converted-space">&nbsp;</span><b>set(String, String)/get(String,String)</b><span class="Apple-converted-space">&nbsp;</span>to set/get arbitrary parameters needed by applications. However, use the<b>DistributedCache</b><span class="Apple-converted-space">&nbsp;</span>for large amounts of (read-only) data.</p></div></div><div id="outline-container-6_4" class="outline-3"><span class="section-number-3">6.4</span><span class="Apple-converted-space">&nbsp;</span>Task Execution &amp; Environment<div class="outline-text-3" id="text-6_4"><p><span class="timestamp-wrapper"><span class="timestamp" style="color: rgb(190, 190, 190);">2011-03-07 Mon</span></span><br></p><p>The<span class="Apple-converted-space">&nbsp;</span><b>TaskTracker</b><span class="Apple-converted-space">&nbsp;</span>executes the<span class="Apple-converted-space">&nbsp;</span><b>Mapper/Reducer</b><span class="Apple-converted-space">&nbsp;</span><i>task</i><span class="Apple-converted-space">&nbsp;</span>as a child process in a separate jvm.</p><p>The child-task inherits the environment of the parent<span class="Apple-converted-space">&nbsp;</span><b>TaskTracker</b>. The user can specify additional</p></div><div id="outline-container-6_4_1" class="outline-4"><span class="section-number-4">6.4.1</span><span class="Apple-converted-space">&nbsp;</span>Configuring Memory Requirements For A Job</div><div id="outline-container-6_4_2" class="outline-4"><span class="section-number-4">6.4.2</span><span class="Apple-converted-space">&nbsp;</span>Map Parameters</div><div id="outline-container-6_4_3" class="outline-4"><span class="section-number-4">6.4.3</span><span class="Apple-converted-space">&nbsp;</span>Shuffle/Reduce Parameters</div><div id="outline-container-6_4_4" class="outline-4"><span class="section-number-4">6.4.4</span><span class="Apple-converted-space">&nbsp;</span>Directory Structure</div><div id="outline-container-6_4_5" class="outline-4"><span class="section-number-4">6.4.5</span><span class="Apple-converted-space">&nbsp;</span>Task JVM Reuse</div><div id="outline-container-6_4_6" class="outline-4"><span class="section-number-4">6.4.6</span><span class="Apple-converted-space">&nbsp;</span>Task Logs</div><div id="outline-container-6_4_7" class="outline-4"><span class="section-number-4">6.4.7</span><span class="Apple-converted-space">&nbsp;</span>Distributing Libraries</div></div><div id="outline-container-6_5" class="outline-3"><span class="section-number-3">6.5</span><span class="Apple-converted-space">&nbsp;</span>Job Submission and Monitoring<div class="outline-text-3" id="text-6_5"><p><span class="timestamp-wrapper"><span class="timestamp" style="color: rgb(190, 190, 190);">2011-03-07 Mon</span></span><br></p><p>The<span class="Apple-converted-space">&nbsp;</span><b>Job</b><span class="Apple-converted-space">&nbsp;</span>is the primary interface by which user-job interacts with the<span class="Apple-converted-space">&nbsp;</span><b>JobTracker</b>.</p><p><b>Job</b><span class="Apple-converted-space">&nbsp;</span>provides facilities to submit jobs, track their progress, access component-tasks' reports and logs, get the MapReduce cluster's status information and so on.</p><p>The job submission process involves:</p><ol><li>Checking the input and output specifications of the job.</li><li>Computing the<span class="Apple-converted-space">&nbsp;</span><b>InputSplit</b><span class="Apple-converted-space">&nbsp;</span>values for the job.</li><li>Setting up the requisite accounting information for the<span class="Apple-converted-space">&nbsp;</span><b>DistributedCache</b><span class="Apple-converted-space">&nbsp;</span>of the job, if necessary.</li><li>Copying the job's jar and configuration to the MapReduce sstem directory on the<span class="Apple-converted-space">&nbsp;</span><b>FileSystem</b>.</li><li>Submitting the job to the<span class="Apple-converted-space">&nbsp;</span><b>JobTracker</b><span class="Apple-converted-space">&nbsp;</span>and optionally monitoring it's status.</li></ol><p>Normally the user creates the application, describes various facets of the job via<span class="Apple-converted-space">&nbsp;</span><b>Job</b>, and then uses the<span class="Apple-converted-space">&nbsp;</span><b>waitForCompletion()</b><span class="Apple-converted-space">&nbsp;</span>method to submit the job and monitor its progress.</p></div><div id="outline-container-6_5_1" class="outline-4"><span class="section-number-4">6.5.1</span><span class="Apple-converted-space">&nbsp;</span>Job Control</div><div id="outline-container-6_5_2" class="outline-4"><span class="section-number-4">6.5.2</span><span class="Apple-converted-space">&nbsp;</span>Job Authorization</div></div><div id="outline-container-6_6" class="outline-3"><span class="section-number-3">6.6</span><span class="Apple-converted-space">&nbsp;</span>Job Input<div class="outline-text-3" id="text-6_6"><p><span class="timestamp-wrapper"><span class="timestamp" style="color: rgb(190, 190, 190);">2011-03-07 Mon</span></span><br></p><p><b>InputFormat</b><span class="Apple-converted-space">&nbsp;</span>describes the input-specification for a MapReduce job.</p><p>The MapReduce framework relies on the<span class="Apple-converted-space">&nbsp;</span><b>InputFormat</b><span class="Apple-converted-space">&nbsp;</span>of the job to:</p><ol><li>Validate the input-specification of the job.</li><li>Split-up the input file(s) into logical<span class="Apple-converted-space">&nbsp;</span><b>InputSplit</b><span class="Apple-converted-space">&nbsp;</span>instances, each of which is then assigned to an individual<span class="Apple-converted-space">&nbsp;</span><b>Mapper</b>.</li><li>Provide the<span class="Apple-converted-space">&nbsp;</span><b>RecordReader</b><span class="Apple-converted-space">&nbsp;</span>implementation used to glean input records from the logical<span class="Apple-converted-space">&nbsp;</span><b>InputSplit</b><span class="Apple-converted-space">&nbsp;</span>for processing by the<span class="Apple-converted-space">&nbsp;</span><b>Mapper</b>.</li></ol><p>The default behavior of file-based<span class="Apple-converted-space">&nbsp;</span><b>InputFormat</b><span class="Apple-converted-space">&nbsp;</span>implementations, typically sub-classes of FileInputFormat, is to split the input into<span class="Apple-converted-space">&nbsp;</span><i>logical</i><span class="Apple-converted-space">&nbsp;</span><b>InputSplit</b><span class="Apple-converted-space">&nbsp;</span>instances based on the total size, in bytes, of the input files. However, the<b>FileSystem</b><span class="Apple-converted-space">&nbsp;</span>blocksize of the input files is treated as an upper bound for input splits. A lower bound on the split size can be set via<span class="Apple-converted-space">&nbsp;</span><b>mapreduce.input.fileinputformat.split.minsize</b>.</p><p>TextInputFormat is the default<span class="Apple-converted-space">&nbsp;</span><b>InputFormat</b>.</p><p>If<span class="Apple-converted-space">&nbsp;</span><b>TextInputFormat</b><span class="Apple-converted-space">&nbsp;</span>is the<span class="Apple-converted-space">&nbsp;</span><b>InputFormat</b><span class="Apple-converted-space">&nbsp;</span>for a given job, the framework detects input-files with the .gz extensions and automatically decompresses them using the appropriate<span class="Apple-converted-space">&nbsp;</span><b>CompressionCodec</b>. However, it must be noted that compressed files with the above extensions cannot be split and each compressed file is processed in its entirety by a single mapper.</p></div><div id="outline-container-6_6_1" class="outline-4"><span class="section-number-4">6.6.1</span><span class="Apple-converted-space">&nbsp;</span>InputSplit<div class="outline-text-4" id="text-6_6_1"><p><b>InputSplit</b><span class="Apple-converted-space">&nbsp;</span>represents the data to be processed by an individual<span class="Apple-converted-space">&nbsp;</span><b>Mapper</b>.</p><p>Typically<span class="Apple-converted-space">&nbsp;</span><b>InputSplit</b><span class="Apple-converted-space">&nbsp;</span>presents a byte-oriented view of the input, and it is the responsibility of<span class="Apple-converted-space">&nbsp;</span><b>RecordReader</b><span class="Apple-converted-space">&nbsp;</span>to process and present a record-oriented view.</p><p>FileSplit is the default<span class="Apple-converted-space">&nbsp;</span><b>InputSplit</b>. It sets<span class="Apple-converted-space">&nbsp;</span><b>mapreduce.map.input.file</b><span class="Apple-converted-space">&nbsp;</span>to the path of the input file for the logical split.</p></div></div><div id="outline-container-6_6_2" class="outline-4"><span class="section-number-4">6.6.2</span><span class="Apple-converted-space">&nbsp;</span>RecordReader<div class="outline-text-4" id="text-6_6_2"><p><b>RecordReader</b><span class="Apple-converted-space">&nbsp;</span>reads<span class="Apple-converted-space">&nbsp;</span><b>&lt;key, value&gt;</b><span class="Apple-converted-space">&nbsp;</span>pairs from an<span class="Apple-converted-space">&nbsp;</span><b>InputSplit</b>.</p><p>Typically the<span class="Apple-converted-space">&nbsp;</span><b>RecordReader</b><span class="Apple-converted-space">&nbsp;</span>converts the byte-oriented view of the input, provided by the<span class="Apple-converted-space">&nbsp;</span><b>InputSplit</b>, and presents a record-oriented to the<span class="Apple-converted-space">&nbsp;</span><b>Mapper</b><span class="Apple-converted-space">&nbsp;</span>implementations for processing.<span class="Apple-converted-space">&nbsp;</span><b>RecordReader</b><span class="Apple-converted-space">&nbsp;</span>thus assumes the responsibility of processing record boundaries and presents the tasks with keys and values.</p></div></div></div><div id="outline-container-6_7" class="outline-3"><span class="section-number-3">6.7</span><span class="Apple-converted-space">&nbsp;</span>Job Output<div class="outline-text-3" id="text-6_7"><p><span class="timestamp-wrapper"><span class="timestamp" style="color: rgb(190, 190, 190);">2011-03-09 Wed</span></span><br></p><p><b>OutputFormat</b><span class="Apple-converted-space">&nbsp;</span>describes the output-specification for a MapReduce job.</p><p>The MapReduce framework relies on the<span class="Apple-converted-space">&nbsp;</span><b>OutputFormat</b><span class="Apple-converted-space">&nbsp;</span>of the job to:</p><ol><li>Validate the output-specification of the job; for example, check that the output directory doesn't already exist.</li><li>Provide the<span class="Apple-converted-space">&nbsp;</span><b>RecordWriter</b><span class="Apple-converted-space">&nbsp;</span>implementation used to write the output files of the job. Output files are stored in a<span class="Apple-converted-space">&nbsp;</span><b>FileSystem</b>.</li></ol><p><b>TextOutputFormat</b><span class="Apple-converted-space">&nbsp;</span>is the default<span class="Apple-converted-space">&nbsp;</span><b>OutputFormat</b>.</p><p><b>RecodWriter</b><span class="Apple-converted-space">&nbsp;</span>writes the output<span class="Apple-converted-space">&nbsp;</span><b>&lt;key, value&gt;</b><span class="Apple-converted-space">&nbsp;</span>pairs to an output file. RecordWriter implementations write the job outputs to the<span class="Apple-converted-space">&nbsp;</span><b>FileSystem</b>.</p></div></div><div id="outline-container-6_8" class="outline-3"><span class="section-number-3">6.8</span><span class="Apple-converted-space">&nbsp;</span>Other Useful Features<div class="outline-text-3" id="text-6_8"><p><span class="timestamp-wrapper"><span class="timestamp" style="color: rgb(190, 190, 190);">2011-03-09 Wed</span></span><br></p></div><div id="outline-container-6_8_1" class="outline-4"><span class="section-number-4">6.8.1</span><span class="Apple-converted-space">&nbsp;</span>Submitting Jobs to Queues</div><div id="outline-container-6_8_2" class="outline-4"><span class="section-number-4">6.8.2</span><span class="Apple-converted-space">&nbsp;</span>Counters</div><div id="outline-container-6_8_3" class="outline-4"><span class="section-number-4">6.8.3</span><span class="Apple-converted-space">&nbsp;</span>DistributedCache</div><div id="outline-container-6_8_4" class="outline-4"><span class="section-number-4">6.8.4</span><span class="Apple-converted-space">&nbsp;</span>Tool<div class="outline-text-4" id="text-6_8_4"><p>The<span class="Apple-converted-space">&nbsp;</span><b>Tool</b><span class="Apple-converted-space">&nbsp;</span>interface supports the handling of generaic Hadoop command-line options.</p></div></div><div id="outline-container-6_8_5" class="outline-4"><span class="section-number-4">6.8.5</span><span class="Apple-converted-space">&nbsp;</span>IsolationRunner<div class="outline-text-4" id="text-6_8_5"><p><b>IsolationRunner</b><span class="Apple-converted-space">&nbsp;</span>is a utility to help debug MapReduce programs.</p></div></div><div id="outline-container-6_8_6" class="outline-4"><span class="section-number-4">6.8.6</span><span class="Apple-converted-space">&nbsp;</span>Profiling<div class="outline-text-4" id="text-6_8_6"><p>Profiling is a utility to get a representative (2 or 3) sample of build-in java profiler for a sample of maps and reduces.</p></div></div><div id="outline-container-6_8_7" class="outline-4"><span class="section-number-4">6.8.7</span><span class="Apple-converted-space">&nbsp;</span>Debugging</div><div id="outline-container-6_8_8" class="outline-4"><span class="section-number-4">6.8.8</span><span class="Apple-converted-space">&nbsp;</span>JobControl</div><div id="outline-container-6_8_9" class="outline-4"><span class="section-number-4">6.8.9</span><span class="Apple-converted-space">&nbsp;</span>Data Compression</div><div id="outline-container-6_8_10" class="outline-4"><span class="section-number-4">6.8.10</span><span class="Apple-converted-space">&nbsp;</span>Skipping Bad Records</div></div></div><div id="outline-container-7" class="outline-2"><span class="section-number-2">7</span><span class="Apple-converted-space">&nbsp;</span>Example: WordCount v2.0<div class="outline-text-2" id="text-7"><p><span class="timestamp-wrapper"><span class="timestamp" style="color: rgb(190, 190, 190);">2011-03-09 Wed</span></span><br>Here is a more complete<span class="Apple-converted-space">&nbsp;</span><b>WordCount</b><span class="Apple-converted-space">&nbsp;</span>which uses many of the features provided by the MapReduce framework we dicussed so far.</p><p>This example needs the HDFS to be up and running, especially for the<span class="Apple-converted-space">&nbsp;</span><b>DistributedCache</b>-related features. Hence it only works with a pseudo-distributed (Single Node Setup) or fully-distributed (Cluster Setup) Hadoop installtation.</p></div><div id="outline-container-7_1" class="outline-3"><span class="section-number-3">7.1</span><span class="Apple-converted-space">&nbsp;</span>Source Code<div class="outline-text-3" id="text-7_1"><p><span class="timestamp-wrapper"><span class="timestamp" style="color: rgb(190, 190, 190);">2011-03-09 Wed</span></span><br></p>1.      package org.myorg;<br>2.      <br>3.      import java.io.*;<br>4.      import java.util.*;<br>5.      <br>6.      import org.apache.hadoop.fs.Path;<br>7.      import org.apache.hadoop.filecache.DistributedCache;<br>8.      import org.apache.hadoop.conf.*;<br>9.      import org.apache.hadoop.io.*;<br>10.     import org.apache.hadoop.mapreduce.*;<br>11.     import org.apache.hadoop.mapreduce.lib.input.*;<br>12.     import org.apache.hadoop.mapreduce.lib.output.*;<br>13.     import org.apache.hadoop.util.*;<br>14.     <br>15.     public class WordCount2 extends Configured implements Tool {<br>16.     <br>17.        public static class Map<br>18.            extends Mapper&lt;LongWritable, Text, Text, IntWritable&gt; {<br>19.     <br>20.          static enum Counters { INPUT_WORDS }<br>21.     <br>22.          private final static IntWritable one = new IntWritable(1);<br>23.          private Text word = new Text();<br>24.     <br>25.          private boolean caseSensitive = true;<br>26.          private Set&lt;String&gt; patternsToSkip = new HashSet&lt;String&gt;();<br>27.     <br>28.          private long numRecords = 0;<br>29.          private String inputFile;<br>30.     <br>31.          public void setup(Context context) {<br>32.            Configuration conf = context.getConfiguration();<br>33.            caseSensitive = conf.getBoolean("wordcount.case.sensitive", true);<br>34.            inputFile = conf.get("mapreduce.map.input.file");<br>35.     <br>36.            if (conf.getBoolean("wordcount.skip.patterns", false)) {<br>37.              Path[] patternsFiles = new Path[0];<br>38.              try {<br>39.                patternsFiles = DistributedCache.getLocalCacheFiles(conf);<br>40.              } catch (IOException ioe) {<br>41.                System.err.println("Caught exception while getting cached files: "<br>42.                    + StringUtils.stringifyException(ioe));<br>43.              }<br>44.              for (Path patternsFile : patternsFiles) {<br>45.                parseSkipFile(patternsFile);<br>46.              }<br>47.            }<br>48.          }<br>49.     <br>50.          private void parseSkipFile(Path patternsFile) {<br>51.            try {<br>52.              BufferedReader fis = new BufferedReader(new FileReader(<br>53.                  patternsFile.toString()));<br>54.              String pattern = null;<br>55.              while ((pattern = fis.readLine()) != null) {<br>56.                patternsToSkip.add(pattern);<br>57.              }<br>58.            } catch (IOException ioe) {<br>59.              System.err.println("Caught exception while parsing the cached file '"<br>60.                  + patternsFile + "' : " + StringUtils.stringifyException(ioe));<br>61.            }<br>62.          }<br>63.     <br>64.          public void map(LongWritable key, Text value, Context context)<br>65.              throws IOException, InterruptedException {<br>66.            String line = (caseSensitive) ?<br>67.                value.toString() : value.toString().toLowerCase();<br>68.     <br>69.            for (String pattern : patternsToSkip) {<br>70.              line = line.replaceAll(pattern, "");<br>71.            }<br>72.     <br>73.            StringTokenizer tokenizer = new StringTokenizer(line);<br>74.            while (tokenizer.hasMoreTokens()) {<br>75.              word.set(tokenizer.nextToken());<br>76.              context.write(word, one);<br>77.              context.getCounter(Counters.INPUT_WORDS).increment(1);<br>78.            }<br>79.     <br>80.            if ((++numRecords % 100) == 0) {<br>81.              context.setStatus("Finished processing " + numRecords<br>82.                  + " records " + "from the input file: " + inputFile);<br>83.            }<br>84.          }<br>85.        }<br>86.     <br>87.        public static class Reduce<br>88.            extends Reducer&lt;Text, IntWritable, Text, IntWritable&gt; {<br>89.          public void reduce(Text key, Iterable&lt;IntWritable&gt; values,<br>90.              Context context) throws IOException, InterruptedException {<br>91.     <br>92.            int sum = 0;<br>93.            for (IntWritable val : values) {<br>94.              sum += val.get();<br>95.            }<br>96.            context.write(key, new IntWritable(sum));<br>97.          }<br>98.        }<br>99.     <br>100.       public int run(String[] args) throws Exception {<br>101.         Job job = new Job(getConf());<br>102.         job.setJarByClass(WordCount2.class);<br>103.         job.setJobName("wordcount2.0");<br>104.    <br>105.         job.setOutputKeyClass(Text.class);<br>106.         job.setOutputValueClass(IntWritable.class);<br>107.    <br>108.         job.setMapperClass(Map.class);<br>109.         job.setCombinerClass(Reduce.class);<br>110.         job.setReducerClass(Reduce.class);<br>111.    <br>112.         // Note that these are the default.<br>113.         job.setInputFormatClass(TextInputFormat.class);<br>114.         job.setOutputFormatClass(TextOutputFormat.class);<br>115.    <br>116.         List&lt;String&gt; other_args = new ArrayList&lt;String&gt;();<br>117.         for (int i=0; i &lt; args.length; ++i) {<br>118.           if ("-skip".equals(args[i])) {<br>119.             DistributedCache.addCacheFile(new Path(args[++i]).toUri(),<br>120.                 job.getConfiguration());<br>121.             job.getConfiguration().setBoolean("wordcount.skip.patterns", true);<br>122.           } else {<br>123.             other_args.add(args[i]);<br>124.           }<br>125.         }<br>126.    <br>127.         FileInputFormat.setInputPaths(job, new Path(other_args.get(0)));<br>128.         FileOutputFormat.setOutputPath(job, new Path(other_args.get(1)));<br>129.    <br>130.         boolean success = job.waitForCompletion(true);<br>131.         return success ? 0 : 1;<br>132.       }<br>133.    <br>134.       public static void main(String[] args) throws Exception {<br>135.         int res = ToolRunner.run(new Configuration(), new WordCount2(), args);<br>136.         System.exit(res);<br>137.       }<br>138.    }<br></div></div><div id="outline-container-7_2" class="outline-3"><span class="section-number-3">7.2</span><span class="Apple-converted-space">&nbsp;</span>Sample Runs<div class="outline-text-3" id="text-7_2"><p><span class="timestamp-wrapper"><span class="timestamp" style="color: rgb(190, 190, 190);">2011-03-10 Thu</span></span><br></p><p>Sample text-files as input:</p>$ bin/hadoop fs -ls /user/joe/wordcount/input/ <br>/user/joe/wordcount/input/file01 <br>/user/joe/wordcount/input/file02 <br><br>$ bin/hadoop fs -cat /user/joe/wordcount/input/file01 <br>Hello World, Bye World! <br><br>$ bin/hadoop fs -cat /user/joe/wordcount/input/file02 <br>Hello Hadoop, Goodbye to hadoop.<br><p>Run the application:</p>$ bin/hadoop jar /user/joe/wordcount.jar org.myorg.WordCount2 /user/joe/wordcount/input /user/joe/wordcount/output<br><p>Output:</p>$ bin/hadoop fs -cat /user/joe/wordcount/output/part-r-00000 <br>Bye 1 <br>Goodbye 1 <br>Hadoop, 1 <br>Hello 2 <br>World! 1 <br>World, 1 <br>hadoop. 1 <br>to 1 <br><p>Notice that the inputs differ from the first version we looked at, and how they affect the outputs.</p><p>Now, lets plug-in a pattern-file which lists the word-patterns to be ignored, via the DistributedCache.</p>$ hadoop fs -cat /user/joe/wordcount/patterns.txt <br>\. <br>\, <br>\! <br>to<br><p>Run it again, this time with more options:</p>$ bin/hadoop jar /user/joe/wordcount.jar org.myorg.WordCount2 -Dwordcount.case.sensitive=true /user/joe/wordcount/input /user/joe/wordcount/output -skip /user/joe/wordcount/patterns.txt<br><p>As expected, the output:</p>$ bin/hadoop fs -cat /user/joe/wordcount/output/part-r-00000 <br>Bye 1 <br>Goodbye 1 <br>Hadoop 1 <br>Hello 2 <br>World 2 <br>hadoop 1 <br><p>Run it once more, this time switch-off case-sensitivity:</p>$ bin/hadoop jar /user/joe/wordcount.jar org.myorg.WordCount2 -Dwordcount.case.sensitive=false /user/joe/wordcount/input /user/joe/wordcount/output -skip /user/joe/wordcount/patterns.txt<br><p>Sure enough, the output:</p>$ bin/hadoop fs -cat /user/joe/wordcount/output/part-r-00000 <br>bye 1 <br>goodbye 1 <br>hadoop 2 <br>hello 2 <br>world 2 <br></div></div><div id="outline-container-7_3" class="outline-3"><span class="section-number-3">7.3</span><span class="Apple-converted-space">&nbsp;</span>Highlights<div class="outline-text-3" id="text-7_3"><p><span class="timestamp-wrapper"><span class="timestamp" style="color: rgb(190, 190, 190);">2011-03-10 Thu</span></span><br>The second version of<span class="Apple-converted-space">&nbsp;</span><b>WordCount</b><span class="Apple-converted-space">&nbsp;</span>improves upon the previous one by using some features offered by the MapReduce framework:</p><ul><li>Demonstrates how applications can access configuration parameters in the<span class="Apple-converted-space">&nbsp;</span><b>setup</b><span class="Apple-converted-space">&nbsp;</span>method of the<span class="Apple-converted-space">&nbsp;</span><b>Mapper</b><span class="Apple-converted-space">&nbsp;</span>(and<span class="Apple-converted-space">&nbsp;</span><b>Reducer</b>) implementations (lines 31-48).</li></ul><p>-Demonstrates how the<span class="Apple-converted-space">&nbsp;</span><b>DistributedCache</b><span class="Apple-converted-space">&nbsp;</span>can be used to distribute read-only data needed by the jobs. Here it allows the user to specify word-patterns to skip while counting (line 119).</p><ul><li>Demonstrates the utility of the<span class="Apple-converted-space">&nbsp;</span><b>Tool</b><span class="Apple-converted-space">&nbsp;</span>interface and the<span class="Apple-converted-space">&nbsp;</span><b>GenericOptionsParser</b><span class="Apple-converted-space">&nbsp;</span>to handle generic Hadoop command-line options (line 135).</li><li>Demonstrates how applications can use<span class="Apple-converted-space">&nbsp;</span><b>Counters</b><span class="Apple-converted-space">&nbsp;</span>(line 77) and how they can set application-specific status information via the<span class="Apple-converted-space">&nbsp;</span><b>Context</b><span class="Apple-converted-space">&nbsp;</span>instance passed to the<span class="Apple-converted-space">&nbsp;</span><b>map</b><span class="Apple-converted-space">&nbsp;</span>(and<span class="Apple-converted-space">&nbsp;</span><b>reduce</b>) method (line 81).</li></ul></div></div></div><div id="postamble"><p class="author">Author: mahaitao</p><p class="date">Date: 2011-03-10 14:55:39 EST</p><p class="creator">HTML generated by org-mode 7.4 in emacs 23</p></div></span>
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP