mapred_tutorial.html 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <meta content="Apache Forrest" name="Generator">
  6. <meta name="Forrest-version" content="0.8">
  7. <meta name="Forrest-skin-name" content="pelt">
  8. <title>Hadoop Map-Reduce Tutorial</title>
  9. <link type="text/css" href="skin/basic.css" rel="stylesheet">
  10. <link media="screen" type="text/css" href="skin/screen.css" rel="stylesheet">
  11. <link media="print" type="text/css" href="skin/print.css" rel="stylesheet">
  12. <link type="text/css" href="skin/profile.css" rel="stylesheet">
  13. <script src="skin/getBlank.js" language="javascript" type="text/javascript"></script><script src="skin/getMenu.js" language="javascript" type="text/javascript"></script><script src="skin/fontsize.js" language="javascript" type="text/javascript"></script>
  14. <link rel="shortcut icon" href="images/favicon.ico">
  15. </head>
  16. <body onload="init()">
  17. <script type="text/javascript">ndeSetTextSize();</script>
  18. <div id="top">
  19. <!--+
  20. |breadtrail
  21. +-->
  22. <div class="breadtrail">
  23. <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://lucene.apache.org/">Lucene</a> &gt; <a href="http://lucene.apache.org/hadoop/">Hadoop</a><script src="skin/breadcrumbs.js" language="JavaScript" type="text/javascript"></script>
  24. </div>
  25. <!--+
  26. |header
  27. +-->
  28. <div class="header">
  29. <!--+
  30. |start group logo
  31. +-->
  32. <div class="grouplogo">
  33. <a href="http://lucene.apache.org/"><img class="logoImage" alt="Lucene" src="images/lucene_green_150.gif" title="Apache Lucene"></a>
  34. </div>
  35. <!--+
  36. |end group logo
  37. +-->
  38. <!--+
  39. |start Project Logo
  40. +-->
  41. <div class="projectlogo">
  42. <a href="http://lucene.apache.org/hadoop/"><img class="logoImage" alt="Hadoop" src="images/hadoop-logo.jpg" title="Scalable Computing Platform"></a>
  43. </div>
  44. <!--+
  45. |end Project Logo
  46. +-->
  47. <!--+
  48. |start Search
  49. +-->
  50. <div class="searchbox">
  51. <form action="http://www.google.com/search" method="get" class="roundtopsmall">
  52. <input value="lucene.apache.org" name="sitesearch" type="hidden"><input onFocus="getBlank (this, 'Search the site with google');" size="25" name="q" id="query" type="text" value="Search the site with google">&nbsp;
  53. <input name="Search" value="Search" type="submit">
  54. </form>
  55. </div>
  56. <!--+
  57. |end search
  58. +-->
  59. <!--+
  60. |start Tabs
  61. +-->
  62. <ul id="tabs">
  63. <li>
  64. <a class="unselected" href="http://lucene.apache.org/hadoop/">Project</a>
  65. </li>
  66. <li>
  67. <a class="unselected" href="http://wiki.apache.org/lucene-hadoop">Wiki</a>
  68. </li>
  69. <li class="current">
  70. <a class="selected" href="index.html">Hadoop 0.15 Documentation</a>
  71. </li>
  72. </ul>
  73. <!--+
  74. |end Tabs
  75. +-->
  76. </div>
  77. </div>
  78. <div id="main">
  79. <div id="publishedStrip">
  80. <!--+
  81. |start Subtabs
  82. +-->
  83. <div id="level2tabs"></div>
  84. <!--+
  85. |end Endtabs
  86. +-->
  87. <script type="text/javascript"><!--
  88. document.write("Last Published: " + document.lastModified);
  89. // --></script>
  90. </div>
  91. <!--+
  92. |breadtrail
  93. +-->
  94. <div class="breadtrail">
  95. &nbsp;
  96. </div>
  97. <!--+
  98. |start Menu, mainarea
  99. +-->
  100. <!--+
  101. |start Menu
  102. +-->
  103. <div id="menu">
  104. <div onclick="SwitchMenu('menu_selected_1.1', 'skin/')" id="menu_selected_1.1Title" class="menutitle" style="background-image: url('skin/images/chapter_open.gif');">Documentation</div>
  105. <div id="menu_selected_1.1" class="selectedmenuitemgroup" style="display: block;">
  106. <div class="menuitem">
  107. <a href="index.html">Overview</a>
  108. </div>
  109. <div class="menuitem">
  110. <a href="quickstart.html">Quickstart</a>
  111. </div>
  112. <div class="menuitem">
  113. <a href="cluster_setup.html">Cluster Setup</a>
  114. </div>
  115. <div class="menuitem">
  116. <a href="hdfs_design.html">HDFS Architecture</a>
  117. </div>
  118. <div class="menupage">
  119. <div class="menupagetitle">Map-Reduce Tutorial</div>
  120. </div>
  121. <div class="menuitem">
  122. <a href="streaming.html">Streaming</a>
  123. </div>
  124. <div class="menuitem">
  125. <a href="api/index.html">API Docs</a>
  126. </div>
  127. <div class="menuitem">
  128. <a href="http://wiki.apache.org/lucene-hadoop/">Wiki</a>
  129. </div>
  130. <div class="menuitem">
  131. <a href="http://wiki.apache.org/lucene-hadoop/FAQ">FAQ</a>
  132. </div>
  133. <div class="menuitem">
  134. <a href="http://lucene.apache.org/hadoop/mailing_lists.html">Mailing Lists</a>
  135. </div>
  136. </div>
  137. <div id="credit"></div>
  138. <div id="roundbottom">
  139. <img style="display: none" class="corner" height="15" width="15" alt="" src="skin/images/rc-b-l-15-1body-2menu-3menu.png"></div>
  140. <!--+
  141. |alternative credits
  142. +-->
  143. <div id="credit2"></div>
  144. </div>
  145. <!--+
  146. |end Menu
  147. +-->
  148. <!--+
  149. |start content
  150. +-->
  151. <div id="content">
  152. <div title="Portable Document Format" class="pdflink">
  153. <a class="dida" href="mapred_tutorial.pdf"><img alt="PDF -icon" src="skin/images/pdfdoc.gif" class="skin"><br>
  154. PDF</a>
  155. </div>
  156. <h1>Hadoop Map-Reduce Tutorial</h1>
  157. <div id="minitoc-area">
  158. <ul class="minitoc">
  159. <li>
  160. <a href="#Purpose">Purpose</a>
  161. </li>
  162. <li>
  163. <a href="#Pre-requisites">Pre-requisites</a>
  164. </li>
  165. <li>
  166. <a href="#Overview">Overview</a>
  167. </li>
  168. <li>
  169. <a href="#Inputs+and+Outputs">Inputs and Outputs</a>
  170. </li>
  171. <li>
  172. <a href="#Example%3A+WordCount+v1.0">Example: WordCount v1.0</a>
  173. <ul class="minitoc">
  174. <li>
  175. <a href="#Source+Code">Source Code</a>
  176. </li>
  177. <li>
  178. <a href="#Usage">Usage</a>
  179. </li>
  180. <li>
  181. <a href="#Walk-through">Walk-through</a>
  182. </li>
  183. </ul>
  184. </li>
  185. <li>
  186. <a href="#Map-Reduce+-+User+Interfaces">Map-Reduce - User Interfaces</a>
  187. <ul class="minitoc">
  188. <li>
  189. <a href="#Payload">Payload</a>
  190. <ul class="minitoc">
  191. <li>
  192. <a href="#Mapper">Mapper</a>
  193. </li>
  194. <li>
  195. <a href="#Reducer">Reducer</a>
  196. </li>
  197. <li>
  198. <a href="#Partitioner">Partitioner</a>
  199. </li>
  200. <li>
  201. <a href="#Reporter">Reporter</a>
  202. </li>
  203. <li>
  204. <a href="#OutputCollector">OutputCollector</a>
  205. </li>
  206. </ul>
  207. </li>
  208. <li>
  209. <a href="#Job+Configuration">Job Configuration</a>
  210. </li>
  211. <li>
  212. <a href="#Job+Submission+and+Monitoring">Job Submission and Monitoring</a>
  213. <ul class="minitoc">
  214. <li>
  215. <a href="#Job+Control">Job Control</a>
  216. </li>
  217. </ul>
  218. </li>
  219. <li>
  220. <a href="#Job+Input">Job Input</a>
  221. <ul class="minitoc">
  222. <li>
  223. <a href="#InputSplit">InputSplit</a>
  224. </li>
  225. <li>
  226. <a href="#RecordReader">RecordReader</a>
  227. </li>
  228. </ul>
  229. </li>
  230. <li>
  231. <a href="#Job+Output">Job Output</a>
  232. <ul class="minitoc">
  233. <li>
  234. <a href="#Task+Side-Effect+Files">Task Side-Effect Files</a>
  235. </li>
  236. <li>
  237. <a href="#RecordWriter">RecordWriter</a>
  238. </li>
  239. </ul>
  240. </li>
  241. <li>
  242. <a href="#Other+Useful+Features">Other Useful Features</a>
  243. <ul class="minitoc">
  244. <li>
  245. <a href="#Counters">Counters</a>
  246. </li>
  247. <li>
  248. <a href="#DistributedCache">DistributedCache</a>
  249. </li>
  250. <li>
  251. <a href="#Tool">Tool</a>
  252. </li>
  253. <li>
  254. <a href="#IsolationRunner">IsolationRunner</a>
  255. </li>
  256. <li>
  257. <a href="#JobControl">JobControl</a>
  258. </li>
  259. </ul>
  260. </li>
  261. </ul>
  262. </li>
  263. <li>
  264. <a href="#Example%3A+WordCount+v2.0">Example: WordCount v2.0</a>
  265. <ul class="minitoc">
  266. <li>
  267. <a href="#Source+Code-N10AB2">Source Code</a>
  268. </li>
  269. <li>
  270. <a href="#Sample+Runs">Sample Runs</a>
  271. </li>
  272. <li>
  273. <a href="#Highlights">Highlights</a>
  274. </li>
  275. </ul>
  276. </li>
  277. </ul>
  278. </div>
  279. <a name="N1000C"></a><a name="Purpose"></a>
  280. <h2 class="h3">Purpose</h2>
  281. <div class="section">
  282. <p>This document comprehensively describes all user-facing facets of the
  283. Hadoop Map-Reduce framework and serves as a tutorial.
  284. </p>
  285. </div>
  286. <a name="N10016"></a><a name="Pre-requisites"></a>
  287. <h2 class="h3">Pre-requisites</h2>
  288. <div class="section">
  289. <p>Ensure that Hadoop is installed, configured and is running. More
  290. details:</p>
  291. <ul>
  292. <li>
  293. Hadoop <a href="quickstart.html">Quickstart</a> for first-time users.
  294. </li>
  295. <li>
  296. Hadoop <a href="cluster_setup.html">Cluster Setup</a> for large,
  297. distributed clusters.
  298. </li>
  299. </ul>
  300. </div>
  301. <a name="N10031"></a><a name="Overview"></a>
  302. <h2 class="h3">Overview</h2>
  303. <div class="section">
  304. <p>Hadoop Map-Reduce is a software framework for easily writing
  305. applications which process vast amounts of data (multi-terabyte data-sets)
  306. in-parallel on large clusters (thousands of nodes) of commodity
  307. hardware in a reliable, fault-tolerant manner.</p>
  308. <p>A Map-Reduce <em>job</em> usually splits the input data-set into
  309. independent chunks which are processed by the <em>map tasks</em> in a
  310. completely parallel manner. The framework sorts the outputs of the maps,
  311. which are then input to the <em>reduce tasks</em>. Typically both the
  312. input and the output of the job are stored in a file-system. The framework
  313. takes care of scheduling tasks, monitoring them and re-executes the failed
  314. tasks.</p>
  315. <p>Typically the compute nodes and the storage nodes are the same, that is,
  316. the Map-Reduce framework and the <a href="hdfs_design.html">Distributed
  317. FileSystem</a> are running on the same set of nodes. This configuration
  318. allows the framework to effectively schedule tasks on the nodes where data
  319. is already present, resulting in very high aggregate bandwidth across the
  320. cluster.</p>
  321. <p>The Map-Reduce framework consists of a single master
  322. <span class="codefrag">JobTracker</span> and one slave <span class="codefrag">TaskTracker</span> per
  323. cluster-node. The master is responsible for scheduling the jobs' component
  324. tasks on the slaves, monitoring them and re-executing the failed tasks. The
  325. slaves execute the tasks as directed by the master.</p>
  326. <p>Minimally, applications specify the input/output locations and supply
  327. <em>map</em> and <em>reduce</em> functions via implementations of
  328. appropriate interfaces and/or abstract-classes. These, and other job
  329. parameters, comprise the <em>job configuration</em>. The Hadoop
  330. <em>job client</em> then submits the job (jar/executable etc.) and
  331. configuration to the <span class="codefrag">JobTracker</span> which then assumes the
  332. responsibility of distributing the software/configuration to the slaves,
  333. scheduling tasks and monitoring them, providing status and diagnostic
  334. information to the job-client.</p>
  335. <p>Although the Hadoop framework is implemented in Java<sup>TM</sup>,
  336. Map-Reduce applications need not be written in Java.</p>
  337. <ul>
  338. <li>
  339. <a href="api/org/apache/hadoop/streaming/package-summary.html">
  340. Hadoop Streaming</a> is a utility which allows users to create and run
  341. jobs with any executables (e.g. shell utilities) as the mapper and/or
  342. the reducer.
  343. </li>
  344. <li>
  345. <a href="api/org/apache/hadoop/mapred/pipes/package-summary.html">
  346. Hadoop Pipes</a> is a <a href="http://www.swig.org/">SWIG</a>-
  347. compatible <em>C++ API</em> to implement Map-Reduce applications (non
  348. JNI<sup>TM</sup> based).
  349. </li>
  350. </ul>
  351. </div>
  352. <a name="N1008A"></a><a name="Inputs+and+Outputs"></a>
  353. <h2 class="h3">Inputs and Outputs</h2>
  354. <div class="section">
  355. <p>The Map-Reduce framework operates exclusively on
  356. <span class="codefrag">&lt;key, value&gt;</span> pairs, that is, the framework views the
  357. input to the job as a set of <span class="codefrag">&lt;key, value&gt;</span> pairs and
  358. produces a set of <span class="codefrag">&lt;key, value&gt;</span> pairs as the output of
  359. the job, conceivably of different types.</p>
  360. <p>The <span class="codefrag">key</span> and <span class="codefrag">value</span> classes have to be
  361. serializable by the framework and hence need to implement the
  362. <a href="api/org/apache/hadoop/io/Writable.html">Writable</a>
  363. interface. Additionally, the <span class="codefrag">key</span> classes have to implement the
  364. <a href="api/org/apache/hadoop/io/WritableComparable.html">
  365. WritableComparable</a> interface to facilitate sorting by the framework.
  366. </p>
  367. <p>Input and Output types of a Map-Reduce job:</p>
  368. <p>
  369. (input) <span class="codefrag">&lt;k1, v1&gt;</span>
  370. -&gt;
  371. <strong>map</strong>
  372. -&gt;
  373. <span class="codefrag">&lt;k2, v2&gt;</span>
  374. -&gt;
  375. <strong>combine</strong>
  376. -&gt;
  377. <span class="codefrag">&lt;k2, v2&gt;</span>
  378. -&gt;
  379. <strong>reduce</strong>
  380. -&gt;
  381. <span class="codefrag">&lt;k3, v3&gt;</span> (output)
  382. </p>
  383. </div>
  384. <a name="N100CC"></a><a name="Example%3A+WordCount+v1.0"></a>
  385. <h2 class="h3">Example: WordCount v1.0</h2>
  386. <div class="section">
  387. <p>Before we jump into the details, lets walk through an example Map-Reduce
  388. application to get a flavour for how they work.</p>
  389. <p>
  390. <span class="codefrag">WordCount</span> is a simple application that counts the number of
  391. occurences of each word in a given input set.</p>
  392. <p>This works with a
  393. <a href="quickstart.html#Standalone+Operation">local-standalone</a>,
  394. <a href="quickstart.html#SingleNodeSetup">pseudo-distributed</a> or
  395. <a href="quickstart.html#Fully-Distributed+Operation">fully-distributed</a>
  396. Hadoop installation.</p>
  397. <a name="N100E9"></a><a name="Source+Code"></a>
  398. <h3 class="h4">Source Code</h3>
  399. <table class="ForrestTable" cellspacing="1" cellpadding="4">
  400. <tr>
  401. <th colspan="1" rowspan="1"></th>
  402. <th colspan="1" rowspan="1">WordCount.java</th>
  403. </tr>
  404. <tr>
  405. <td colspan="1" rowspan="1">1.</td>
  406. <td colspan="1" rowspan="1">
  407. <span class="codefrag">package org.myorg;</span>
  408. </td>
  409. </tr>
  410. <tr>
  411. <td colspan="1" rowspan="1">2.</td>
  412. <td colspan="1" rowspan="1"></td>
  413. </tr>
  414. <tr>
  415. <td colspan="1" rowspan="1">3.</td>
  416. <td colspan="1" rowspan="1">
  417. <span class="codefrag">import java.io.IOException;</span>
  418. </td>
  419. </tr>
  420. <tr>
  421. <td colspan="1" rowspan="1">4.</td>
  422. <td colspan="1" rowspan="1">
  423. <span class="codefrag">import java.util.*;</span>
  424. </td>
  425. </tr>
  426. <tr>
  427. <td colspan="1" rowspan="1">5.</td>
  428. <td colspan="1" rowspan="1"></td>
  429. </tr>
  430. <tr>
  431. <td colspan="1" rowspan="1">6.</td>
  432. <td colspan="1" rowspan="1">
  433. <span class="codefrag">import org.apache.hadoop.fs.Path;</span>
  434. </td>
  435. </tr>
  436. <tr>
  437. <td colspan="1" rowspan="1">7.</td>
  438. <td colspan="1" rowspan="1">
  439. <span class="codefrag">import org.apache.hadoop.conf.*;</span>
  440. </td>
  441. </tr>
  442. <tr>
  443. <td colspan="1" rowspan="1">8.</td>
  444. <td colspan="1" rowspan="1">
  445. <span class="codefrag">import org.apache.hadoop.io.*;</span>
  446. </td>
  447. </tr>
  448. <tr>
  449. <td colspan="1" rowspan="1">9.</td>
  450. <td colspan="1" rowspan="1">
  451. <span class="codefrag">import org.apache.hadoop.mapred.*;</span>
  452. </td>
  453. </tr>
  454. <tr>
  455. <td colspan="1" rowspan="1">10.</td>
  456. <td colspan="1" rowspan="1">
  457. <span class="codefrag">import org.apache.hadoop.util.*;</span>
  458. </td>
  459. </tr>
  460. <tr>
  461. <td colspan="1" rowspan="1">11.</td>
  462. <td colspan="1" rowspan="1"></td>
  463. </tr>
  464. <tr>
  465. <td colspan="1" rowspan="1">12.</td>
  466. <td colspan="1" rowspan="1">
  467. <span class="codefrag">public class WordCount {</span>
  468. </td>
  469. </tr>
  470. <tr>
  471. <td colspan="1" rowspan="1">13.</td>
  472. <td colspan="1" rowspan="1"></td>
  473. </tr>
  474. <tr>
  475. <td colspan="1" rowspan="1">14.</td>
  476. <td colspan="1" rowspan="1">
  477. &nbsp;&nbsp;
  478. <span class="codefrag">
  479. public static class Map extends MapReduceBase
  480. implements Mapper&lt;LongWritable, Text, Text, IntWritable&gt; {
  481. </span>
  482. </td>
  483. </tr>
  484. <tr>
  485. <td colspan="1" rowspan="1">15.</td>
  486. <td colspan="1" rowspan="1">
  487. &nbsp;&nbsp;&nbsp;&nbsp;
  488. <span class="codefrag">
  489. private final static IntWritable one = new IntWritable(1);
  490. </span>
  491. </td>
  492. </tr>
  493. <tr>
  494. <td colspan="1" rowspan="1">16.</td>
  495. <td colspan="1" rowspan="1">
  496. &nbsp;&nbsp;&nbsp;&nbsp;
  497. <span class="codefrag">private Text word = new Text();</span>
  498. </td>
  499. </tr>
  500. <tr>
  501. <td colspan="1" rowspan="1">17.</td>
  502. <td colspan="1" rowspan="1"></td>
  503. </tr>
  504. <tr>
  505. <td colspan="1" rowspan="1">18.</td>
  506. <td colspan="1" rowspan="1">
  507. &nbsp;&nbsp;&nbsp;&nbsp;
  508. <span class="codefrag">
  509. public void map(LongWritable key, Text value,
  510. OutputCollector&lt;Text, IntWritable&gt; output,
  511. Reporter reporter) throws IOException {
  512. </span>
  513. </td>
  514. </tr>
  515. <tr>
  516. <td colspan="1" rowspan="1">19.</td>
  517. <td colspan="1" rowspan="1">
  518. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  519. <span class="codefrag">String line = value.toString();</span>
  520. </td>
  521. </tr>
  522. <tr>
  523. <td colspan="1" rowspan="1">20.</td>
  524. <td colspan="1" rowspan="1">
  525. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  526. <span class="codefrag">StringTokenizer tokenizer = new StringTokenizer(line);</span>
  527. </td>
  528. </tr>
  529. <tr>
  530. <td colspan="1" rowspan="1">21.</td>
  531. <td colspan="1" rowspan="1">
  532. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  533. <span class="codefrag">while (tokenizer.hasMoreTokens()) {</span>
  534. </td>
  535. </tr>
  536. <tr>
  537. <td colspan="1" rowspan="1">22.</td>
  538. <td colspan="1" rowspan="1">
  539. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  540. <span class="codefrag">word.set(tokenizer.nextToken());</span>
  541. </td>
  542. </tr>
  543. <tr>
  544. <td colspan="1" rowspan="1">23.</td>
  545. <td colspan="1" rowspan="1">
  546. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  547. <span class="codefrag">output.collect(word, one);</span>
  548. </td>
  549. </tr>
  550. <tr>
  551. <td colspan="1" rowspan="1">24.</td>
  552. <td colspan="1" rowspan="1">
  553. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  554. <span class="codefrag">}</span>
  555. </td>
  556. </tr>
  557. <tr>
  558. <td colspan="1" rowspan="1">25.</td>
  559. <td colspan="1" rowspan="1">
  560. &nbsp;&nbsp;&nbsp;&nbsp;
  561. <span class="codefrag">}</span>
  562. </td>
  563. </tr>
  564. <tr>
  565. <td colspan="1" rowspan="1">26.</td>
  566. <td colspan="1" rowspan="1">
  567. &nbsp;&nbsp;
  568. <span class="codefrag">}</span>
  569. </td>
  570. </tr>
  571. <tr>
  572. <td colspan="1" rowspan="1">27.</td>
  573. <td colspan="1" rowspan="1"></td>
  574. </tr>
  575. <tr>
  576. <td colspan="1" rowspan="1">28.</td>
  577. <td colspan="1" rowspan="1">
  578. &nbsp;&nbsp;
  579. <span class="codefrag">
  580. public static class Reduce extends MapReduceBase implements
  581. Reducer&lt;Text, IntWritable, Text, IntWritable&gt; {
  582. </span>
  583. </td>
  584. </tr>
  585. <tr>
  586. <td colspan="1" rowspan="1">29.</td>
  587. <td colspan="1" rowspan="1">
  588. &nbsp;&nbsp;&nbsp;&nbsp;
  589. <span class="codefrag">
  590. public void reduce(Text key, Iterator&lt;IntWritable&gt; values,
  591. OutputCollector&lt;Text, IntWritable&gt; output,
  592. Reporter reporter) throws IOException {
  593. </span>
  594. </td>
  595. </tr>
  596. <tr>
  597. <td colspan="1" rowspan="1">30.</td>
  598. <td colspan="1" rowspan="1">
  599. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  600. <span class="codefrag">int sum = 0;</span>
  601. </td>
  602. </tr>
  603. <tr>
  604. <td colspan="1" rowspan="1">31.</td>
  605. <td colspan="1" rowspan="1">
  606. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  607. <span class="codefrag">while (values.hasNext()) {</span>
  608. </td>
  609. </tr>
  610. <tr>
  611. <td colspan="1" rowspan="1">32.</td>
  612. <td colspan="1" rowspan="1">
  613. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  614. <span class="codefrag">sum += values.next().get();</span>
  615. </td>
  616. </tr>
  617. <tr>
  618. <td colspan="1" rowspan="1">33.</td>
  619. <td colspan="1" rowspan="1">
  620. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  621. <span class="codefrag">}</span>
  622. </td>
  623. </tr>
  624. <tr>
  625. <td colspan="1" rowspan="1">34.</td>
  626. <td colspan="1" rowspan="1">
  627. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  628. <span class="codefrag">output.collect(key, new IntWritable(sum));</span>
  629. </td>
  630. </tr>
  631. <tr>
  632. <td colspan="1" rowspan="1">35.</td>
  633. <td colspan="1" rowspan="1">
  634. &nbsp;&nbsp;&nbsp;&nbsp;
  635. <span class="codefrag">}</span>
  636. </td>
  637. </tr>
  638. <tr>
  639. <td colspan="1" rowspan="1">36.</td>
  640. <td colspan="1" rowspan="1">
  641. &nbsp;&nbsp;
  642. <span class="codefrag">}</span>
  643. </td>
  644. </tr>
  645. <tr>
  646. <td colspan="1" rowspan="1">37.</td>
  647. <td colspan="1" rowspan="1"></td>
  648. </tr>
  649. <tr>
  650. <td colspan="1" rowspan="1">38.</td>
  651. <td colspan="1" rowspan="1">
  652. &nbsp;&nbsp;
  653. <span class="codefrag">
  654. public static void main(String[] args) throws Exception {
  655. </span>
  656. </td>
  657. </tr>
  658. <tr>
  659. <td colspan="1" rowspan="1">39.</td>
  660. <td colspan="1" rowspan="1">
  661. &nbsp;&nbsp;&nbsp;&nbsp;
  662. <span class="codefrag">
  663. JobConf conf = new JobConf(WordCount.class);
  664. </span>
  665. </td>
  666. </tr>
  667. <tr>
  668. <td colspan="1" rowspan="1">40.</td>
  669. <td colspan="1" rowspan="1">
  670. &nbsp;&nbsp;&nbsp;&nbsp;
  671. <span class="codefrag">conf.setJobName("wordcount");</span>
  672. </td>
  673. </tr>
  674. <tr>
  675. <td colspan="1" rowspan="1">41.</td>
  676. <td colspan="1" rowspan="1"></td>
  677. </tr>
  678. <tr>
  679. <td colspan="1" rowspan="1">42.</td>
  680. <td colspan="1" rowspan="1">
  681. &nbsp;&nbsp;&nbsp;&nbsp;
  682. <span class="codefrag">conf.setOutputKeyClass(Text.class);</span>
  683. </td>
  684. </tr>
  685. <tr>
  686. <td colspan="1" rowspan="1">43.</td>
  687. <td colspan="1" rowspan="1">
  688. &nbsp;&nbsp;&nbsp;&nbsp;
  689. <span class="codefrag">conf.setOutputValueClass(IntWritable.class);</span>
  690. </td>
  691. </tr>
  692. <tr>
  693. <td colspan="1" rowspan="1">44.</td>
  694. <td colspan="1" rowspan="1"></td>
  695. </tr>
  696. <tr>
  697. <td colspan="1" rowspan="1">45.</td>
  698. <td colspan="1" rowspan="1">
  699. &nbsp;&nbsp;&nbsp;&nbsp;
  700. <span class="codefrag">conf.setMapperClass(Map.class);</span>
  701. </td>
  702. </tr>
  703. <tr>
  704. <td colspan="1" rowspan="1">46.</td>
  705. <td colspan="1" rowspan="1">
  706. &nbsp;&nbsp;&nbsp;&nbsp;
  707. <span class="codefrag">conf.setCombinerClass(Reduce.class);</span>
  708. </td>
  709. </tr>
  710. <tr>
  711. <td colspan="1" rowspan="1">47.</td>
  712. <td colspan="1" rowspan="1">
  713. &nbsp;&nbsp;&nbsp;&nbsp;
  714. <span class="codefrag">conf.setReducerClass(Reduce.class);</span>
  715. </td>
  716. </tr>
  717. <tr>
  718. <td colspan="1" rowspan="1">48.</td>
  719. <td colspan="1" rowspan="1"></td>
  720. </tr>
  721. <tr>
  722. <td colspan="1" rowspan="1">49.</td>
  723. <td colspan="1" rowspan="1">
  724. &nbsp;&nbsp;&nbsp;&nbsp;
  725. <span class="codefrag">conf.setInputFormat(TextInputFormat.class);</span>
  726. </td>
  727. </tr>
  728. <tr>
  729. <td colspan="1" rowspan="1">50.</td>
  730. <td colspan="1" rowspan="1">
  731. &nbsp;&nbsp;&nbsp;&nbsp;
  732. <span class="codefrag">conf.setOutputFormat(TextOutputFormat.class);</span>
  733. </td>
  734. </tr>
  735. <tr>
  736. <td colspan="1" rowspan="1">51.</td>
  737. <td colspan="1" rowspan="1"></td>
  738. </tr>
  739. <tr>
  740. <td colspan="1" rowspan="1">52.</td>
  741. <td colspan="1" rowspan="1">
  742. &nbsp;&nbsp;&nbsp;&nbsp;
  743. <span class="codefrag">conf.setInputPath(new Path(args[0]));</span>
  744. </td>
  745. </tr>
  746. <tr>
  747. <td colspan="1" rowspan="1">53.</td>
  748. <td colspan="1" rowspan="1">
  749. &nbsp;&nbsp;&nbsp;&nbsp;
  750. <span class="codefrag">conf.setOutputPath(new Path(args[1]));</span>
  751. </td>
  752. </tr>
  753. <tr>
  754. <td colspan="1" rowspan="1">54.</td>
  755. <td colspan="1" rowspan="1"></td>
  756. </tr>
  757. <tr>
  758. <td colspan="1" rowspan="1">55.</td>
  759. <td colspan="1" rowspan="1">
  760. &nbsp;&nbsp;&nbsp;&nbsp;
  761. <span class="codefrag">JobClient.runJob(conf);</span>
  762. </td>
  763. </tr>
  764. <tr>
  765. <td colspan="1" rowspan="1">57.</td>
  766. <td colspan="1" rowspan="1">
  767. &nbsp;&nbsp;
  768. <span class="codefrag">}</span>
  769. </td>
  770. </tr>
  771. <tr>
  772. <td colspan="1" rowspan="1">58.</td>
  773. <td colspan="1" rowspan="1">
  774. <span class="codefrag">}</span>
  775. </td>
  776. </tr>
  777. <tr>
  778. <td colspan="1" rowspan="1">59.</td>
  779. <td colspan="1" rowspan="1"></td>
  780. </tr>
  781. </table>
  782. <a name="N1046B"></a><a name="Usage"></a>
  783. <h3 class="h4">Usage</h3>
  784. <p>Assuming <span class="codefrag">HADOOP_HOME</span> is the root of the installation and
  785. <span class="codefrag">HADOOP_VERSION</span> is the Hadoop version installed, compile
  786. <span class="codefrag">WordCount.java</span> and create a jar:</p>
  787. <p>
  788. <span class="codefrag">$ mkdir wordcount_classes</span>
  789. <br>
  790. <span class="codefrag">
  791. $ javac -classpath ${HADOOP_HOME}/hadoop-${HADOOP_VERSION}-core.jar
  792. -d wordcount_classes WordCount.java
  793. </span>
  794. <br>
  795. <span class="codefrag">$ jar -cvf /usr/joe/wordcount.jar -C wordcount_classes/ .</span>
  796. </p>
  797. <p>Assuming that:</p>
  798. <ul>
  799. <li>
  800. <span class="codefrag">/usr/joe/wordcount/input</span> - input directory in HDFS
  801. </li>
  802. <li>
  803. <span class="codefrag">/usr/joe/wordcount/output</span> - output directory in HDFS
  804. </li>
  805. </ul>
  806. <p>Sample text-files as input:</p>
  807. <p>
  808. <span class="codefrag">$ bin/hadoop dfs -ls /usr/joe/wordcount/input/</span>
  809. <br>
  810. <span class="codefrag">/usr/joe/wordcount/input/file01</span>
  811. <br>
  812. <span class="codefrag">/usr/joe/wordcount/input/file02</span>
  813. <br>
  814. <br>
  815. <span class="codefrag">$ bin/hadoop dfs -cat /usr/joe/wordcount/input/file01</span>
  816. <br>
  817. <span class="codefrag">Hello World Bye World</span>
  818. <br>
  819. <br>
  820. <span class="codefrag">$ bin/hadoop dfs -cat /usr/joe/wordcount/input/file02</span>
  821. <br>
  822. <span class="codefrag">Hello Hadoop Goodbye Hadoop</span>
  823. </p>
  824. <p>Run the application:</p>
  825. <p>
  826. <span class="codefrag">
  827. $ bin/hadoop jar /usr/joe/wordcount.jar org.myorg.WordCount
  828. /usr/joe/wordcount/input /usr/joe/wordcount/output
  829. </span>
  830. </p>
  831. <p>Output:</p>
  832. <p>
  833. <span class="codefrag">
  834. $ bin/hadoop dfs -cat /usr/joe/wordcount/output/part-00000
  835. </span>
  836. <br>
  837. <span class="codefrag">Bye 1</span>
  838. <br>
  839. <span class="codefrag">Goodbye 1</span>
  840. <br>
  841. <span class="codefrag">Hadoop 2</span>
  842. <br>
  843. <span class="codefrag">Hello 2</span>
  844. <br>
  845. <span class="codefrag">World 2</span>
  846. <br>
  847. </p>
  848. <a name="N104EB"></a><a name="Walk-through"></a>
  849. <h3 class="h4">Walk-through</h3>
  850. <p>The <span class="codefrag">WordCount</span> application is quite straight-forward.</p>
  851. <p>The <span class="codefrag">Mapper</span> implementation (lines 14-26), via the
  852. <span class="codefrag">map</span> method (lines 18-25), processes one line at a time,
  853. as provided by the specified <span class="codefrag">TextInputFormat</span> (line 49).
  854. It then splits the line into tokens separated by whitespaces, via the
  855. <span class="codefrag">StringTokenizer</span>, and emits a key-value pair of
  856. <span class="codefrag">&lt; &lt;word&gt;, 1&gt;</span>.</p>
  857. <p>
  858. For the given sample input the first map emits:<br>
  859. <span class="codefrag">&lt; Hello, 1&gt;</span>
  860. <br>
  861. <span class="codefrag">&lt; World, 1&gt;</span>
  862. <br>
  863. <span class="codefrag">&lt; Bye, 1&gt;</span>
  864. <br>
  865. <span class="codefrag">&lt; World, 1&gt;</span>
  866. <br>
  867. </p>
  868. <p>
  869. The second map emits:<br>
  870. <span class="codefrag">&lt; Hello, 1&gt;</span>
  871. <br>
  872. <span class="codefrag">&lt; Hadoop, 1&gt;</span>
  873. <br>
  874. <span class="codefrag">&lt; Goodbye, 1&gt;</span>
  875. <br>
  876. <span class="codefrag">&lt; Hadoop, 1&gt;</span>
  877. <br>
  878. </p>
  879. <p>We'll learn more about the number of maps spawned for a given job, and
  880. how to control them in a fine-grained manner, a bit later in the
  881. tutorial.</p>
  882. <p>
  883. <span class="codefrag">WordCount</span> also specifies a <span class="codefrag">combiner</span> (line
  884. 46). Hence, the output of each map is passed through the local combiner
  885. (which is same as the <span class="codefrag">Reducer</span> as per the job
  886. configuration) for local aggregation, after being sorted on the
  887. <em>key</em>s.</p>
  888. <p>
  889. The output of the first map:<br>
  890. <span class="codefrag">&lt; Bye, 1&gt;</span>
  891. <br>
  892. <span class="codefrag">&lt; Hello, 1&gt;</span>
  893. <br>
  894. <span class="codefrag">&lt; World, 2&gt;</span>
  895. <br>
  896. </p>
  897. <p>
  898. The output of the second map:<br>
  899. <span class="codefrag">&lt; Goodbye, 1&gt;</span>
  900. <br>
  901. <span class="codefrag">&lt; Hadoop, 2&gt;</span>
  902. <br>
  903. <span class="codefrag">&lt; Hello, 1&gt;</span>
  904. <br>
  905. </p>
  906. <p>The <span class="codefrag">Reducer</span> implementation (lines 28-36), via the
  907. <span class="codefrag">reduce</span> method (lines 29-35) just sums up the values,
  908. which are the occurence counts for each key (i.e. words in this example).
  909. </p>
  910. <p>
  911. Thus the output of the job is:<br>
  912. <span class="codefrag">&lt; Bye, 1&gt;</span>
  913. <br>
  914. <span class="codefrag">&lt; Goodbye, 1&gt;</span>
  915. <br>
  916. <span class="codefrag">&lt; Hadoop, 2&gt;</span>
  917. <br>
  918. <span class="codefrag">&lt; Hello, 2&gt;</span>
  919. <br>
  920. <span class="codefrag">&lt; World, 2&gt;</span>
  921. <br>
  922. </p>
  923. <p>The <span class="codefrag">run</span> method specifies various facets of the job, such
  924. as the input/output paths (passed via the command line), key/value
  925. types, input/output formats etc., in the <span class="codefrag">JobConf</span>.
  926. It then calls the <span class="codefrag">JobClient.runJob</span> (line 55) to submit the
  927. and monitor its progress.</p>
  928. <p>We'll learn more about <span class="codefrag">JobConf</span>, <span class="codefrag">JobClient</span>,
  929. <span class="codefrag">Tool</span> and other interfaces and classes a bit later in the
  930. tutorial.</p>
  931. </div>
  932. <a name="N105A2"></a><a name="Map-Reduce+-+User+Interfaces"></a>
  933. <h2 class="h3">Map-Reduce - User Interfaces</h2>
  934. <div class="section">
  935. <p>This section provides a reasonable amount of detail on every user-facing
  936. aspect of the Map-Reduce framwork. This should help users implement,
  937. configure and tune their jobs in a fine-grained manner. However, please
  938. note that the javadoc for each class/interface remains the most
  939. comprehensive documentation available; this is only meant to be a tutorial.
  940. </p>
  941. <p>Let us first take the <span class="codefrag">Mapper</span> and <span class="codefrag">Reducer</span>
  942. interfaces. Applications typically implement them to provide the
  943. <span class="codefrag">map</span> and <span class="codefrag">reduce</span> methods.</p>
  944. <p>We will then discuss other core interfaces including
  945. <span class="codefrag">JobConf</span>, <span class="codefrag">JobClient</span>, <span class="codefrag">Partitioner</span>,
  946. <span class="codefrag">OutputCollector</span>, <span class="codefrag">Reporter</span>,
  947. <span class="codefrag">InputFormat</span>, <span class="codefrag">OutputFormat</span> and others.</p>
  948. <p>Finally, we will wrap up by discussing some useful features of the
  949. framework such as the <span class="codefrag">DistributedCache</span>,
  950. <span class="codefrag">IsolationRunner</span> etc.</p>
  951. <a name="N105DB"></a><a name="Payload"></a>
  952. <h3 class="h4">Payload</h3>
  953. <p>Applications typically implement the <span class="codefrag">Mapper</span> and
  954. <span class="codefrag">Reducer</span> interfaces to provide the <span class="codefrag">map</span> and
  955. <span class="codefrag">reduce</span> methods. These form the core of the job.</p>
  956. <a name="N105F0"></a><a name="Mapper"></a>
  957. <h4>Mapper</h4>
  958. <p>
  959. <a href="api/org/apache/hadoop/mapred/Mapper.html">
  960. Mapper</a> maps input key/value pairs to a set of intermediate
  961. key/value pairs.</p>
  962. <p>Maps are the individual tasks that transform input records into
  963. intermediate records. The transformed intermediate records do not need
  964. to be of the same type as the input records. A given input pair may
  965. map to zero or many output pairs.</p>
  966. <p>The Hadoop Map-Reduce framework spawns one map task for each
  967. <span class="codefrag">InputSplit</span> generated by the <span class="codefrag">InputFormat</span> for
  968. the job.</p>
  969. <p>Overall, <span class="codefrag">Mapper</span> implementations are passed the
  970. <span class="codefrag">JobConf</span> for the job via the
  971. <a href="api/org/apache/hadoop/mapred/JobConfigurable.html#configure(org.apache.hadoop.mapred.JobConf)">
  972. JobConfigurable.configure(JobConf)</a> method and override it to
  973. initialize themselves. The framework then calls
  974. <a href="api/org/apache/hadoop/mapred/Mapper.html#map(K1, V1, org.apache.hadoop.mapred.OutputCollector, org.apache.hadoop.mapred.Reporter)">
  975. map(WritableComparable, Writable, OutputCollector, Reporter)</a> for
  976. each key/value pair in the <span class="codefrag">InputSplit</span> for that task.
  977. Applications can then override the
  978. <a href="api/org/apache/hadoop/io/Closeable.html#close()">
  979. Closeable.close()</a> method to perform any required cleanup.</p>
  980. <p>Output pairs do not need to be of the same types as input pairs. A
  981. given input pair may map to zero or many output pairs. Output pairs
  982. are collected with calls to
  983. <a href="api/org/apache/hadoop/mapred/OutputCollector.html#collect(K, V)">
  984. OutputCollector.collect(WritableComparable,Writable)</a>.</p>
  985. <p>Applications can use the <span class="codefrag">Reporter</span> to report
  986. progress, set application-level status messages and update
  987. <span class="codefrag">Counters</span>, or just indicate that they are alive.</p>
  988. <p>All intermediate values associated with a given output key are
  989. subsequently grouped by the framework, and passed to the
  990. <span class="codefrag">Reducer</span>(s) to determine the final output. Users can
  991. control the grouping by specifying a <span class="codefrag">Comparator</span> via
  992. <a href="api/org/apache/hadoop/mapred/JobConf.html#setOutputKeyComparatorClass(java.lang.Class)">
  993. JobConf.setOutputKeyComparatorClass(Class)</a>.</p>
  994. <p>The <span class="codefrag">Mapper</span> outputs are sorted and then
  995. partitioned per <span class="codefrag">Reducer</span>. The total number of partitions is
  996. the same as the number of reduce tasks for the job. Users can control
  997. which keys (and hence records) go to which <span class="codefrag">Reducer</span> by
  998. implementing a custom <span class="codefrag">Partitioner</span>.</p>
  999. <p>Users can optionally specify a <span class="codefrag">combiner</span>, via
  1000. <a href="api/org/apache/hadoop/mapred/JobConf.html#setCombinerClass(java.lang.Class)">
  1001. JobConf.setCombinerClass(Class)</a>, to perform local aggregation of
  1002. the intermediate outputs, which helps to cut down the amount of data
  1003. transferred from the <span class="codefrag">Mapper</span> to the <span class="codefrag">Reducer</span>.
  1004. </p>
  1005. <p>The intermediate, sorted outputs are always stored in files of
  1006. <a href="api/org/apache/hadoop/io/SequenceFile.html">
  1007. SequenceFile</a> format. Applications can control if, and how, the
  1008. intermediate outputs are to be compressed and the
  1009. <a href="api/org/apache/hadoop/io/compress/CompressionCodec.html">
  1010. CompressionCodec</a> to be used via the <span class="codefrag">JobConf</span>.
  1011. </p>
  1012. <a name="N1066A"></a><a name="How+Many+Maps%3F"></a>
  1013. <h5>How Many Maps?</h5>
  1014. <p>The number of maps is usually driven by the total size of the
  1015. inputs, that is, the total number of blocks of the input files.</p>
  1016. <p>The right level of parallelism for maps seems to be around 10-100
  1017. maps per-node, although it has been set up to 300 maps for very
  1018. cpu-light map tasks. Task setup takes awhile, so it is best if the
  1019. maps take at least a minute to execute.</p>
  1020. <p>Thus, if you expect 10TB of input data and have a blocksize of
  1021. <span class="codefrag">128MB</span>, you'll end up with 82,000 maps, unless
  1022. <a href="api/org/apache/hadoop/mapred/JobConf.html#setNumMapTasks(int)">
  1023. setNumMapTasks(int)</a> (which only provides a hint to the framework)
  1024. is used to set it even higher.</p>
  1025. <a name="N10682"></a><a name="Reducer"></a>
  1026. <h4>Reducer</h4>
  1027. <p>
  1028. <a href="api/org/apache/hadoop/mapred/Reducer.html">
  1029. Reducer</a> reduces a set of intermediate values which share a key to
  1030. a smaller set of values.</p>
  1031. <p>The number of reduces for the job is set by the user
  1032. via <a href="api/org/apache/hadoop/mapred/JobConf.html#setNumReduceTasks(int)">
  1033. JobConf.setNumReduceTasks(int)</a>.</p>
  1034. <p>Overall, <span class="codefrag">Reducer</span> implementations are passed the
  1035. <span class="codefrag">JobConf</span> for the job via the
  1036. <a href="api/org/apache/hadoop/mapred/JobConfigurable.html#configure(org.apache.hadoop.mapred.JobConf)">
  1037. JobConfigurable.configure(JobConf)</a> method and can override it to
  1038. initialize themselves. The framework then calls
  1039. <a href="api/org/apache/hadoop/mapred/Reducer.html#reduce(K2, java.util.Iterator, org.apache.hadoop.mapred.OutputCollector, org.apache.hadoop.mapred.Reporter)">
  1040. reduce(WritableComparable, Iterator, OutputCollector, Reporter)</a>
  1041. method for each <span class="codefrag">&lt;key, (list of values)&gt;</span>
  1042. pair in the grouped inputs. Applications can then override the
  1043. <a href="api/org/apache/hadoop/io/Closeable.html#close()">
  1044. Closeable.close()</a> method to perform any required cleanup.</p>
  1045. <p>
  1046. <span class="codefrag">Reducer</span> has 3 primary phases: shuffle, sort and reduce.
  1047. </p>
  1048. <a name="N106B2"></a><a name="Shuffle"></a>
  1049. <h5>Shuffle</h5>
  1050. <p>Input to the <span class="codefrag">Reducer</span> is the sorted output of the
  1051. mappers. In this phase the framework fetches the relevant partition
  1052. of the output of all the mappers, via HTTP.</p>
  1053. <a name="N106BF"></a><a name="Sort"></a>
  1054. <h5>Sort</h5>
  1055. <p>The framework groups <span class="codefrag">Reducer</span> inputs by keys (since
  1056. different mappers may have output the same key) in this stage.</p>
  1057. <p>The shuffle and sort phases occur simultaneously; while
  1058. map-outputs are being fetched they are merged.</p>
  1059. <a name="N106CE"></a><a name="Secondary+Sort"></a>
  1060. <h5>Secondary Sort</h5>
  1061. <p>If equivalence rules for grouping the intermediate keys are
  1062. required to be different from those for grouping keys before
  1063. reduction, then one may specify a <span class="codefrag">Comparator</span> via
  1064. <a href="api/org/apache/hadoop/mapred/JobConf.html#setOutputValueGroupingComparator(java.lang.Class)">
  1065. JobConf.setOutputValueGroupingComparator(Class)</a>. Since
  1066. <a href="api/org/apache/hadoop/mapred/JobConf.html#setOutputKeyComparatorClass(java.lang.Class)">
  1067. JobConf.setOutputKeyComparatorClass(Class)</a> can be used to
  1068. control how intermediate keys are grouped, these can be used in
  1069. conjunction to simulate <em>secondary sort on values</em>.</p>
  1070. <a name="N106E7"></a><a name="Reduce"></a>
  1071. <h5>Reduce</h5>
  1072. <p>In this phase the
  1073. <a href="api/org/apache/hadoop/mapred/Reducer.html#reduce(K2, java.util.Iterator, org.apache.hadoop.mapred.OutputCollector, org.apache.hadoop.mapred.Reporter)">
  1074. reduce(WritableComparable, Iterator, OutputCollector, Reporter)</a>
  1075. method is called for each <span class="codefrag">&lt;key, (list of values)&gt;</span>
  1076. pair in the grouped inputs.</p>
  1077. <p>The output of the reduce task is typically written to the
  1078. <a href="api/org/apache/hadoop/fs/FileSystem.html">
  1079. FileSystem</a> via
  1080. <a href="api/org/apache/hadoop/mapred/OutputCollector.html#collect(K, V)">
  1081. OutputCollector.collect(WritableComparable, Writable)</a>.</p>
  1082. <p>Applications can use the <span class="codefrag">Reporter</span> to report
  1083. progress, set application-level status messages and update
  1084. <span class="codefrag">Counters</span>, or just indicate that they are alive.</p>
  1085. <p>The output of the <span class="codefrag">Reducer</span> is <em>not sorted</em>.</p>
  1086. <a name="N10715"></a><a name="How+Many+Reduces%3F"></a>
  1087. <h5>How Many Reduces?</h5>
  1088. <p>The right number of reduces seems to be <span class="codefrag">0.95</span> or
  1089. <span class="codefrag">1.75</span> multiplied by (&lt;<em>no. of nodes</em>&gt; *
  1090. <span class="codefrag">mapred.tasktracker.tasks.maximum</span>).</p>
  1091. <p>With <span class="codefrag">0.95</span> all of the reduces can launch immediately
  1092. and start transfering map outputs as the maps finish. With
  1093. <span class="codefrag">1.75</span> the faster nodes will finish their first round of
  1094. reduces and launch a second wave of reduces doing a much better job
  1095. of load balancing.</p>
  1096. <p>Increasing the number of reduces increases the framework overhead,
  1097. but increases load balancing and lowers the cost of failures.</p>
  1098. <p>The scaling factors above are slightly less than whole numbers to
  1099. reserve a few reduce slots in the framework for speculative-tasks and
  1100. failed tasks.</p>
  1101. <a name="N1073A"></a><a name="Reducer+NONE"></a>
  1102. <h5>Reducer NONE</h5>
  1103. <p>It is legal to set the number of reduce-tasks to <em>zero</em> if
  1104. no reduction is desired.</p>
  1105. <p>In this case the outputs of the map-tasks go directly to the
  1106. <span class="codefrag">FileSystem</span>, into the output path set by
  1107. <a href="api/org/apache/hadoop/mapred/JobConf.html#setOutputPath(org.apache.hadoop.fs.Path)">
  1108. setOutputPath(Path)</a>. The framework does not sort the
  1109. map-outputs before writing them out to the <span class="codefrag">FileSystem</span>.
  1110. </p>
  1111. <a name="N10755"></a><a name="Partitioner"></a>
  1112. <h4>Partitioner</h4>
  1113. <p>
  1114. <a href="api/org/apache/hadoop/mapred/Partitioner.html">
  1115. Partitioner</a> partitions the key space.</p>
  1116. <p>Partitioner controls the partitioning of the keys of the
  1117. intermediate map-outputs. The key (or a subset of the key) is used to
  1118. derive the partition, typically by a <em>hash function</em>. The total
  1119. number of partitions is the same as the number of reduce tasks for the
  1120. job. Hence this controls which of the <span class="codefrag">m</span> reduce tasks the
  1121. intermediate key (and hence the record) is sent to for reduction.</p>
  1122. <p>
  1123. <a href="api/org/apache/hadoop/mapred/lib/HashPartitioner.html">
  1124. HashPartitioner</a> is the default <span class="codefrag">Partitioner</span>.</p>
  1125. <a name="N10774"></a><a name="Reporter"></a>
  1126. <h4>Reporter</h4>
  1127. <p>
  1128. <a href="api/org/apache/hadoop/mapred/Reporter.html">
  1129. Reporter</a> is a facility for Map-Reduce applications to report
  1130. progress, set application-level status messages and update
  1131. <span class="codefrag">Counters</span>.</p>
  1132. <p>
  1133. <span class="codefrag">Mapper</span> and <span class="codefrag">Reducer</span> implementations can use
  1134. the <span class="codefrag">Reporter</span> to report progress or just indicate
  1135. that they are alive. In scenarios where the application takes a
  1136. significant amount of time to process individual key/value pairs,
  1137. this is crucial since the framework might assume that the task has
  1138. timed-out and kill that task. Another way to avoid this is to
  1139. set the configuration parameter <span class="codefrag">mapred.task.timeout</span> to a
  1140. high-enough value (or even set it to <em>zero</em> for no time-outs).
  1141. </p>
  1142. <p>Applications can also update <span class="codefrag">Counters</span> using the
  1143. <span class="codefrag">Reporter</span>.</p>
  1144. <a name="N1079E"></a><a name="OutputCollector"></a>
  1145. <h4>OutputCollector</h4>
  1146. <p>
  1147. <a href="api/org/apache/hadoop/mapred/OutputCollector.html">
  1148. OutputCollector</a> is a generalization of the facility provided by
  1149. the Map-Reduce framework to collect data output by the
  1150. <span class="codefrag">Mapper</span> or the <span class="codefrag">Reducer</span> (either the
  1151. intermediate outputs or the output of the job).</p>
  1152. <p>Hadoop Map-Reduce comes bundled with a
  1153. <a href="api/org/apache/hadoop/mapred/lib/package-summary.html">
  1154. library</a> of generally useful mappers, reducers, and partitioners.</p>
  1155. <a name="N107B9"></a><a name="Job+Configuration"></a>
  1156. <h3 class="h4">Job Configuration</h3>
  1157. <p>
  1158. <a href="api/org/apache/hadoop/mapred/JobConf.html">
  1159. JobConf</a> represents a Map-Reduce job configuration.</p>
  1160. <p>
  1161. <span class="codefrag">JobConf</span> is the primary interface for a user to describe
  1162. a map-reduce job to the Hadoop framework for execution. The framework
  1163. tries to faithfully execute the job as described by <span class="codefrag">JobConf</span>,
  1164. however:</p>
  1165. <ul>
  1166. <li>f
  1167. Some configuration parameters may have been marked as
  1168. <a href="api/org/apache/hadoop/conf/Configuration.html#FinalParams">
  1169. final</a> by administrators and hence cannot be altered.
  1170. </li>
  1171. <li>
  1172. While some job parameters are straight-forward to set (e.g.
  1173. <a href="api/org/apache/hadoop/mapred/JobConf.html#setNumReduceTasks(int)">
  1174. setNumReduceTasks(int)</a>), other parameters interact subtly with
  1175. the rest of the framework and/or job configuration and are
  1176. more complex to set (e.g.
  1177. <a href="api/org/apache/hadoop/mapred/JobConf.html#setNumMapTasks(int)">
  1178. setNumMapTasks(int)</a>).
  1179. </li>
  1180. </ul>
  1181. <p>
  1182. <span class="codefrag">JobConf</span> is typically used to specify the
  1183. <span class="codefrag">Mapper</span>, combiner (if any), <span class="codefrag">Partitioner</span>,
  1184. <span class="codefrag">Reducer</span>, <span class="codefrag">InputFormat</span> and
  1185. <span class="codefrag">OutputFormat</span> implementations. <span class="codefrag">JobConf</span> also
  1186. indicates the set of input files
  1187. (<a href="api/org/apache/hadoop/mapred/JobConf.html#setInputPath(org.apache.hadoop.fs.Path)">setInputPath(Path)</a>/<a href="api/org/apache/hadoop/mapred/JobConf.html#addInputPath(org.apache.hadoop.fs.Path)">addInputPath(Path)</a>)
  1188. and where the output files should be written
  1189. (<a href="api/org/apache/hadoop/mapred/JobConf.html#setOutputPath(org.apache.hadoop.fs.Path)">setOutputPath(Path)</a>).</p>
  1190. <p>Optionally, <span class="codefrag">JobConf</span> is used to specify other advanced
  1191. facets of the job such as the <span class="codefrag">Comparator</span> to be used, files
  1192. to be put in the <span class="codefrag">DistributedCache</span>, whether intermediate
  1193. and/or job outputs are to be compressed (and how), debugging via
  1194. user-provided scripts
  1195. (<a href="api/org/apache/hadoop/mapred/JobConf.html#setMapDebugScript(java.lang.String)">setMapDebugScript(String)</a>/<a href="api/org/apache/hadoop/mapred/JobConf.html#setReduceDebugScript(java.lang.String)">setReduceDebugScript(String)</a>)
  1196. , whether job tasks can be executed in a <em>speculative</em> manner
  1197. (<a href="api/org/apache/hadoop/mapred/JobConf.html#setSpeculativeExecution(boolean)">setSpeculativeExecution(boolean)</a>)
  1198. , maximum number of attempts per task
  1199. (<a href="api/org/apache/hadoop/mapred/JobConf.html#setMaxMapAttempts(int)">setMaxMapAttempts(int)</a>/<a href="api/org/apache/hadoop/mapred/JobConf.html#setMaxReduceAttempts(int)">setMaxReduceAttempts(int)</a>)
  1200. , percentage of tasks failure which can be tolerated by the job
  1201. (<a href="api/org/apache/hadoop/mapred/JobConf.html#setMaxMapTaskFailuresPercent(int)">setMaxMapTaskFailuresPercent(int)</a>/<a href="api/org/apache/hadoop/mapred/JobConf.html#setMaxReduceTaskFailuresPercent(int)">setMaxReduceTaskFailuresPercent(int)</a>)
  1202. etc.</p>
  1203. <p>Of course, users can use
  1204. <a href="api/org/apache/hadoop/conf/Configuration.html#set(java.lang.String, java.lang.String)">set(String, String)</a>/<a href="api/org/apache/hadoop/conf/Configuration.html#get(java.lang.String, java.lang.String)">get(String, String)</a>
  1205. to set/get arbitrary parameters needed by applications. However, use the
  1206. <span class="codefrag">DistributedCache</span> for large amounts of (read-only) data.</p>
  1207. <a name="N1083F"></a><a name="Job+Submission+and+Monitoring"></a>
  1208. <h3 class="h4">Job Submission and Monitoring</h3>
  1209. <p>
  1210. <a href="api/org/apache/hadoop/mapred/JobClient.html">
  1211. JobClient</a> is the primary interface by which user-job interacts
  1212. with the <span class="codefrag">JobTracker</span>.</p>
  1213. <p>
  1214. <span class="codefrag">JobClient</span> provides facilities to submit jobs, track their
  1215. progress, access component-tasks' reports/logs, get the Map-Reduce
  1216. cluster's status information and so on.</p>
  1217. <p>The job submission process involves:</p>
  1218. <ol>
  1219. <li>Checking the input and output specifications of the job.</li>
  1220. <li>Computing the <span class="codefrag">InputSplit</span> values for the job.</li>
  1221. <li>
  1222. Setting up the requisite accounting information for the
  1223. <span class="codefrag">DistributedCache</span> of the job, if necessary.
  1224. </li>
  1225. <li>
  1226. Copying the job's jar and configuration to the map-reduce system
  1227. directory on the <span class="codefrag">FileSystem</span>.
  1228. </li>
  1229. <li>
  1230. Submitting the job to the <span class="codefrag">JobTracker</span> and optionally
  1231. monitoring it's status.
  1232. </li>
  1233. </ol>
  1234. <p>Normally the user creates the application, describes various facets
  1235. of the job via <span class="codefrag">JobConf</span>, and then uses the
  1236. <span class="codefrag">JobClient</span> to submit the job and monitor its progress.</p>
  1237. <a name="N1087D"></a><a name="Job+Control"></a>
  1238. <h4>Job Control</h4>
  1239. <p>Users may need to chain map-reduce jobs to accomplish complex
  1240. tasks which cannot be done via a single map-reduce job. This is fairly
  1241. easy since the output of the job typically goes to distributed
  1242. file-system, and the output, in turn, can be used as the input for the
  1243. next job.</p>
  1244. <p>However, this also means that the onus on ensuring jobs are
  1245. complete (success/failure) lies squarely on the clients. In such
  1246. cases, the various job-control options are:</p>
  1247. <ul>
  1248. <li>
  1249. <a href="api/org/apache/hadoop/mapred/JobClient.html#runJob(org.apache.hadoop.mapred.JobConf)">
  1250. runJob(JobConf)</a> : Submits the job and returns only after the
  1251. job has completed.
  1252. </li>
  1253. <li>
  1254. <a href="api/org/apache/hadoop/mapred/JobClient.html#submitJob(org.apache.hadoop.mapred.JobConf)">
  1255. submitJob(JobConf)</a> : Only submits the job, then poll the
  1256. returned handle to the
  1257. <a href="api/org/apache/hadoop/mapred/RunningJob.html">
  1258. RunningJob</a> to query status and make scheduling decisions.
  1259. </li>
  1260. <li>
  1261. <a href="api/org/apache/hadoop/mapred/JobConf.html#setJobEndNotificationURI(java.lang.String)">
  1262. JobConf.setJobEndNotificationURI(String)</a> : Sets up a
  1263. notification upon job-completion, thus avoiding polling.
  1264. </li>
  1265. </ul>
  1266. <a name="N108A7"></a><a name="Job+Input"></a>
  1267. <h3 class="h4">Job Input</h3>
  1268. <p>
  1269. <a href="api/org/apache/hadoop/mapred/InputFormat.html">
  1270. InputFormat</a> describes the input-specification for a Map-Reduce job.
  1271. </p>
  1272. <p>The Map-Reduce framework relies on the <span class="codefrag">InputFormat</span> of
  1273. the job to:</p>
  1274. <ol>
  1275. <li>Validate the input-specification of the job.</li>
  1276. <li>
  1277. Split-up the input file(s) into logical <span class="codefrag">InputSplit</span>
  1278. instances, each of which is then assigned to an individual
  1279. <span class="codefrag">Mapper</span>.
  1280. </li>
  1281. <li>
  1282. Provide the <span class="codefrag">RecordReader</span> implementation used to
  1283. glean input records from the logical <span class="codefrag">InputSplit</span> for
  1284. processing by the <span class="codefrag">Mapper</span>.
  1285. </li>
  1286. </ol>
  1287. <p>The default behavior of file-based <span class="codefrag">InputFormat</span>
  1288. implementations, typically sub-classes of
  1289. <a href="api/org/apache/hadoop/mapred/FileInputFormat.html">
  1290. FileInputFormat</a>, is to split the input into <em>logical</em>
  1291. <span class="codefrag">InputSplit</span> instances based on the total size, in bytes, of
  1292. the input files. However, the <span class="codefrag">FileSystem</span> blocksize of the
  1293. input files is treated as an upper bound for input splits. A lower bound
  1294. on the split size can be set via <span class="codefrag">mapred.min.split.size</span>.</p>
  1295. <p>Clearly, logical splits based on input-size is insufficient for many
  1296. applications since record boundaries must be respected. In such cases,
  1297. the application should implement a <span class="codefrag">RecordReader</span>, who is
  1298. responsible for respecting record-boundaries and presents a
  1299. record-oriented view of the logical <span class="codefrag">InputSplit</span> to the
  1300. individual task.</p>
  1301. <p>
  1302. <a href="api/org/apache/hadoop/mapred/TextInputFormat.html">
  1303. TextInputFormat</a> is the default <span class="codefrag">InputFormat</span>.
  1304. </p>
  1305. <a name="N108FC"></a><a name="InputSplit"></a>
  1306. <h4>InputSplit</h4>
  1307. <p>
  1308. <a href="api/org/apache/hadoop/mapred/InputSplit.html">
  1309. InputSplit</a> represents the data to be processed by an individual
  1310. <span class="codefrag">Mapper</span>.</p>
  1311. <p>Typically <span class="codefrag">InputSplit</span> presents a byte-oriented view of
  1312. the input, and it is the responsibility of <span class="codefrag">RecordReader</span>
  1313. to process and present a record-oriented view.</p>
  1314. <p>
  1315. <a href="api/org/apache/hadoop/mapred/FileSplit.html">
  1316. FileSplit</a> is the default <span class="codefrag">InputSplit</span>. It sets
  1317. <span class="codefrag">map.input.file</span> to the path of the input file for the
  1318. logical split.</p>
  1319. <a name="N10921"></a><a name="RecordReader"></a>
  1320. <h4>RecordReader</h4>
  1321. <p>
  1322. <a href="api/org/apache/hadoop/mapred/RecordReader.html">
  1323. RecordReader</a> reads <span class="codefrag">&lt;key, value&gt;</span> pairs from an
  1324. <span class="codefrag">InputSplit</span>.</p>
  1325. <p>Typically the <span class="codefrag">RecordReader</span> converts the byte-oriented
  1326. view of the input, provided by the <span class="codefrag">InputSplit</span>, and
  1327. presents a record-oriented to the <span class="codefrag">Mapper</span> implementations
  1328. for processing. <span class="codefrag">RecordReader</span> thus assumes the
  1329. responsibility of processing record boundaries and presents the tasks
  1330. with keys and values.</p>
  1331. <a name="N10944"></a><a name="Job+Output"></a>
  1332. <h3 class="h4">Job Output</h3>
  1333. <p>
  1334. <a href="api/org/apache/hadoop/mapred/OutputFormat.html">
  1335. OutputFormat</a> describes the output-specification for a Map-Reduce
  1336. job.</p>
  1337. <p>The Map-Reduce framework relies on the <span class="codefrag">OutputFormat</span> of
  1338. the job to:</p>
  1339. <ol>
  1340. <li>
  1341. Validate the output-specification of the job; for example, check that
  1342. the output directory doesn't already exist.
  1343. </li>
  1344. <li>
  1345. Provide the <span class="codefrag">RecordWriter</span> implementation used to
  1346. write the output files of the job. Output files are stored in a
  1347. <span class="codefrag">FileSystem</span>.
  1348. </li>
  1349. </ol>
  1350. <p>
  1351. <span class="codefrag">TextOutputFormat</span> is the default
  1352. <span class="codefrag">OutputFormat</span>.</p>
  1353. <a name="N1096D"></a><a name="Task+Side-Effect+Files"></a>
  1354. <h4>Task Side-Effect Files</h4>
  1355. <p>In some applications, component tasks need to create and/or write to
  1356. side-files, which differ from the actual job-output files.</p>
  1357. <p>In such cases there could be issues with two instances of the same
  1358. <span class="codefrag">Mapper</span> or <span class="codefrag">Reducer</span> running simultaneously (for
  1359. example, speculative tasks) trying to open and/or write to the same
  1360. file (path) on the <span class="codefrag">FileSystem</span>. Hence the
  1361. application-writer will have to pick unique names per task-attempt
  1362. (using the taskid, say <span class="codefrag">task_200709221812_0001_m_000000_0</span>),
  1363. not just per task.</p>
  1364. <p>To avoid these issues the Map-Reduce framework maintains a special
  1365. <span class="codefrag">${mapred.output.dir}/_${taskid}</span> sub-directory for each
  1366. task-attempt on the <span class="codefrag">FileSystem</span> where the output of the
  1367. task-attempt is stored. On successful completion of the task-attempt,
  1368. the files in the <span class="codefrag">${mapred.output.dir}/_${taskid}</span> (only)
  1369. are <em>promoted</em> to <span class="codefrag">${mapred.output.dir}</span>. Of course,
  1370. the framework discards the sub-directory of unsuccessful task-attempts.
  1371. This process is completely transparent to the application.</p>
  1372. <p>The application-writer can take advantage of this feature by
  1373. creating any side-files required in <span class="codefrag">${mapred.output.dir}</span>
  1374. during execution of a task via
  1375. <a href="api/org/apache/hadoop/mapred/JobConf.html#getOutputPath()">
  1376. JobConf.getOutputPath()</a>, and the framework will promote them
  1377. similarly for succesful task-attempts, thus eliminating the need to
  1378. pick unique paths per task-attempt.</p>
  1379. <a name="N109A2"></a><a name="RecordWriter"></a>
  1380. <h4>RecordWriter</h4>
  1381. <p>
  1382. <a href="api/org/apache/hadoop/mapred/RecordWriter.html">
  1383. RecordWriter</a> writes the output <span class="codefrag">&lt;key, value&gt;</span>
  1384. pairs to an output file.</p>
  1385. <p>RecordWriter implementations write the job outputs to the
  1386. <span class="codefrag">FileSystem</span>.</p>
  1387. <a name="N109B9"></a><a name="Other+Useful+Features"></a>
  1388. <h3 class="h4">Other Useful Features</h3>
  1389. <a name="N109BF"></a><a name="Counters"></a>
  1390. <h4>Counters</h4>
  1391. <p>
  1392. <span class="codefrag">Counters</span> represent global counters, defined either by
  1393. the Map-Reduce framework or applications. Each <span class="codefrag">Counter</span> can
  1394. be of any <span class="codefrag">Enum</span> type. Counters of a particular
  1395. <span class="codefrag">Enum</span> are bunched into groups of type
  1396. <span class="codefrag">Counters.Group</span>.</p>
  1397. <p>Applications can define arbitrary <span class="codefrag">Counters</span> (of type
  1398. <span class="codefrag">Enum</span>) and update them via
  1399. <a href="api/org/apache/hadoop/mapred/Reporter.html#incrCounter(java.lang.Enum, long)">
  1400. Reporter.incrCounter(Enum, long)</a> in the <span class="codefrag">map</span> and/or
  1401. <span class="codefrag">reduce</span> methods. These counters are then globally
  1402. aggregated by the framework.</p>
  1403. <a name="N109EA"></a><a name="DistributedCache"></a>
  1404. <h4>DistributedCache</h4>
  1405. <p>
  1406. <a href="api/org/apache/hadoop/filecache/DistributedCache.html">
  1407. DistributedCache</a> distributes application-specific, large, read-only
  1408. files efficiently.</p>
  1409. <p>
  1410. <span class="codefrag">DistributedCache</span> is a facility provided by the
  1411. Map-Reduce framework to cache files (text, archives, jars and so on)
  1412. needed by applications.</p>
  1413. <p>Applications specify the files to be cached via urls (hdfs:// or
  1414. http://) in the <span class="codefrag">JobConf</span>. The <span class="codefrag">DistributedCache</span>
  1415. assumes that the files specified via hdfs:// urls are already present
  1416. on the <span class="codefrag">FileSystem</span>.</p>
  1417. <p>The framework will copy the necessary files to the slave node
  1418. before any tasks for the job are executed on that node. Its
  1419. efficiency stems from the fact that the files are only copied once
  1420. per job and the ability to cache archives which are un-archived on
  1421. the slaves.</p>
  1422. <p>
  1423. <span class="codefrag">DistributedCache</span> can be used to distribute simple,
  1424. read-only data/text files and more complex types such as archives and
  1425. jars. Archives (zip files) are <em>un-archived</em> at the slave nodes.
  1426. Jars maybe be optionally added to the classpath of the tasks, a
  1427. rudimentary <em>software distribution</em> mechanism. Files have
  1428. <em>execution permissions</em> set. Optionally users can also direct the
  1429. <span class="codefrag">DistributedCache</span> to <em>symlink</em> the cached file(s)
  1430. into the working directory of the task.</p>
  1431. <p>
  1432. <span class="codefrag">DistributedCache</span> tracks the modification timestamps of
  1433. the cached files. Clearly the cache files should not be modified by
  1434. the application or externally while the job is executing.</p>
  1435. <a name="N10A24"></a><a name="Tool"></a>
  1436. <h4>Tool</h4>
  1437. <p>The <a href="api/org/apache/hadoop/util/Tool.html">Tool</a>
  1438. interface supports the handling of generic Hadoop command-line options.
  1439. </p>
  1440. <p>
  1441. <span class="codefrag">Tool</span> is the standard for any Map-Reduce tool or
  1442. application. The application should delegate the handling of
  1443. standard command-line options to
  1444. <a href="api/org/apache/hadoop/util/GenericOptionsParser.html">
  1445. GenericOptionsParser</a> via
  1446. <a href="api/org/apache/hadoop/util/ToolRunner.html#run(org.apache.hadoop.util.Tool, java.lang.String[])">
  1447. ToolRunner.run(Tool, String[])</a> and only handle its custom
  1448. arguments.</p>
  1449. <p>
  1450. The generic Hadoop command-line options are:<br>
  1451. <span class="codefrag">
  1452. -conf &lt;configuration file&gt;
  1453. </span>
  1454. <br>
  1455. <span class="codefrag">
  1456. -D &lt;property=value&gt;
  1457. </span>
  1458. <br>
  1459. <span class="codefrag">
  1460. -fs &lt;local|namenode:port&gt;
  1461. </span>
  1462. <br>
  1463. <span class="codefrag">
  1464. -jt &lt;local|jobtracker:port&gt;
  1465. </span>
  1466. </p>
  1467. <a name="N10A56"></a><a name="IsolationRunner"></a>
  1468. <h4>IsolationRunner</h4>
  1469. <p>
  1470. <a href="api/org/apache/hadoop/mapred/IsolationRunner.html">
  1471. IsolationRunner</a> is a utility to help debug Map-Reduce programs.</p>
  1472. <p>To use the <span class="codefrag">IsolationRunner</span>, first set
  1473. <span class="codefrag">keep.failed.tasks.files</span> to <span class="codefrag">true</span>
  1474. (also see <span class="codefrag">keep.tasks.files.pattern</span>).</p>
  1475. <p>
  1476. Next, go to the node on which the failed task ran and go to the
  1477. <span class="codefrag">TaskTracker</span>'s local directory and run the
  1478. <span class="codefrag">IsolationRunner</span>:<br>
  1479. <span class="codefrag">$ cd &lt;local path&gt;/taskTracker/${taskid}/work</span>
  1480. <br>
  1481. <span class="codefrag">
  1482. $ bin/hadoop org.apache.hadoop.mapred.IsolationRunner ../job.xml
  1483. </span>
  1484. </p>
  1485. <p>
  1486. <span class="codefrag">IsolationRunner</span> will run the failed task in a single
  1487. jvm, which can be in the debugger, over precisely the same input.</p>
  1488. <a name="N10A89"></a><a name="JobControl"></a>
  1489. <h4>JobControl</h4>
  1490. <p>
  1491. <a href="api/org/apache/hadoop/mapred/jobcontrol/package-summary.html">
  1492. JobControl</a> is a utility which encapsulates a set of Map-Reduce jobs
  1493. and their dependencies.</p>
  1494. </div>
  1495. <a name="N10A98"></a><a name="Example%3A+WordCount+v2.0"></a>
  1496. <h2 class="h3">Example: WordCount v2.0</h2>
  1497. <div class="section">
  1498. <p>Here is a more complete <span class="codefrag">WordCount</span> which uses many of the
  1499. features provided by the Map-Reduce framework we discussed so far.</p>
  1500. <p>This needs the HDFS to be up and running, especially for the
  1501. <span class="codefrag">DistributedCache</span>-related features. Hence it only works with a
  1502. <a href="quickstart.html#SingleNodeSetup">pseudo-distributed</a> or
  1503. <a href="quickstart.html#Fully-Distributed+Operation">fully-distributed</a>
  1504. Hadoop installation.</p>
  1505. <a name="N10AB2"></a><a name="Source+Code-N10AB2"></a>
  1506. <h3 class="h4">Source Code</h3>
  1507. <table class="ForrestTable" cellspacing="1" cellpadding="4">
  1508. <tr>
  1509. <th colspan="1" rowspan="1"></th>
  1510. <th colspan="1" rowspan="1">WordCount.java</th>
  1511. </tr>
  1512. <tr>
  1513. <td colspan="1" rowspan="1">1.</td>
  1514. <td colspan="1" rowspan="1">
  1515. <span class="codefrag">package org.myorg;</span>
  1516. </td>
  1517. </tr>
  1518. <tr>
  1519. <td colspan="1" rowspan="1">2.</td>
  1520. <td colspan="1" rowspan="1"></td>
  1521. </tr>
  1522. <tr>
  1523. <td colspan="1" rowspan="1">3.</td>
  1524. <td colspan="1" rowspan="1">
  1525. <span class="codefrag">import java.io.*;</span>
  1526. </td>
  1527. </tr>
  1528. <tr>
  1529. <td colspan="1" rowspan="1">4.</td>
  1530. <td colspan="1" rowspan="1">
  1531. <span class="codefrag">import java.util.*;</span>
  1532. </td>
  1533. </tr>
  1534. <tr>
  1535. <td colspan="1" rowspan="1">5.</td>
  1536. <td colspan="1" rowspan="1"></td>
  1537. </tr>
  1538. <tr>
  1539. <td colspan="1" rowspan="1">6.</td>
  1540. <td colspan="1" rowspan="1">
  1541. <span class="codefrag">import org.apache.hadoop.fs.Path;</span>
  1542. </td>
  1543. </tr>
  1544. <tr>
  1545. <td colspan="1" rowspan="1">7.</td>
  1546. <td colspan="1" rowspan="1">
  1547. <span class="codefrag">import org.apache.hadoop.filecache.DistributedCache;</span>
  1548. </td>
  1549. </tr>
  1550. <tr>
  1551. <td colspan="1" rowspan="1">8.</td>
  1552. <td colspan="1" rowspan="1">
  1553. <span class="codefrag">import org.apache.hadoop.conf.*;</span>
  1554. </td>
  1555. </tr>
  1556. <tr>
  1557. <td colspan="1" rowspan="1">9.</td>
  1558. <td colspan="1" rowspan="1">
  1559. <span class="codefrag">import org.apache.hadoop.io.*;</span>
  1560. </td>
  1561. </tr>
  1562. <tr>
  1563. <td colspan="1" rowspan="1">10.</td>
  1564. <td colspan="1" rowspan="1">
  1565. <span class="codefrag">import org.apache.hadoop.mapred.*;</span>
  1566. </td>
  1567. </tr>
  1568. <tr>
  1569. <td colspan="1" rowspan="1">11.</td>
  1570. <td colspan="1" rowspan="1">
  1571. <span class="codefrag">import org.apache.hadoop.util.*;</span>
  1572. </td>
  1573. </tr>
  1574. <tr>
  1575. <td colspan="1" rowspan="1">12.</td>
  1576. <td colspan="1" rowspan="1"></td>
  1577. </tr>
  1578. <tr>
  1579. <td colspan="1" rowspan="1">13.</td>
  1580. <td colspan="1" rowspan="1">
  1581. <span class="codefrag">public class WordCount extends Configured implements Tool {</span>
  1582. </td>
  1583. </tr>
  1584. <tr>
  1585. <td colspan="1" rowspan="1">14.</td>
  1586. <td colspan="1" rowspan="1"></td>
  1587. </tr>
  1588. <tr>
  1589. <td colspan="1" rowspan="1">15.</td>
  1590. <td colspan="1" rowspan="1">
  1591. &nbsp;&nbsp;
  1592. <span class="codefrag">
  1593. public static class Map extends MapReduceBase
  1594. implements Mapper&lt;LongWritable, Text, Text, IntWritable&gt; {
  1595. </span>
  1596. </td>
  1597. </tr>
  1598. <tr>
  1599. <td colspan="1" rowspan="1">16.</td>
  1600. <td colspan="1" rowspan="1"></td>
  1601. </tr>
  1602. <tr>
  1603. <td colspan="1" rowspan="1">17.</td>
  1604. <td colspan="1" rowspan="1">
  1605. &nbsp;&nbsp;&nbsp;&nbsp;
  1606. <span class="codefrag">
  1607. static enum Counters { INPUT_WORDS }
  1608. </span>
  1609. </td>
  1610. </tr>
  1611. <tr>
  1612. <td colspan="1" rowspan="1">18.</td>
  1613. <td colspan="1" rowspan="1"></td>
  1614. </tr>
  1615. <tr>
  1616. <td colspan="1" rowspan="1">19.</td>
  1617. <td colspan="1" rowspan="1">
  1618. &nbsp;&nbsp;&nbsp;&nbsp;
  1619. <span class="codefrag">
  1620. private final static IntWritable one = new IntWritable(1);
  1621. </span>
  1622. </td>
  1623. </tr>
  1624. <tr>
  1625. <td colspan="1" rowspan="1">20.</td>
  1626. <td colspan="1" rowspan="1">
  1627. &nbsp;&nbsp;&nbsp;&nbsp;
  1628. <span class="codefrag">private Text word = new Text();</span>
  1629. </td>
  1630. </tr>
  1631. <tr>
  1632. <td colspan="1" rowspan="1">21.</td>
  1633. <td colspan="1" rowspan="1"></td>
  1634. </tr>
  1635. <tr>
  1636. <td colspan="1" rowspan="1">22.</td>
  1637. <td colspan="1" rowspan="1">
  1638. &nbsp;&nbsp;&nbsp;&nbsp;
  1639. <span class="codefrag">private boolean caseSensitive = true;</span>
  1640. </td>
  1641. </tr>
  1642. <tr>
  1643. <td colspan="1" rowspan="1">23.</td>
  1644. <td colspan="1" rowspan="1">
  1645. &nbsp;&nbsp;&nbsp;&nbsp;
  1646. <span class="codefrag">private Set&lt;String&gt; patternsToSkip = new HashSet&lt;String&gt;();</span>
  1647. </td>
  1648. </tr>
  1649. <tr>
  1650. <td colspan="1" rowspan="1">24.</td>
  1651. <td colspan="1" rowspan="1"></td>
  1652. </tr>
  1653. <tr>
  1654. <td colspan="1" rowspan="1">25.</td>
  1655. <td colspan="1" rowspan="1">
  1656. &nbsp;&nbsp;&nbsp;&nbsp;
  1657. <span class="codefrag">private long numRecords = 0;</span>
  1658. </td>
  1659. </tr>
  1660. <tr>
  1661. <td colspan="1" rowspan="1">26.</td>
  1662. <td colspan="1" rowspan="1">
  1663. &nbsp;&nbsp;&nbsp;&nbsp;
  1664. <span class="codefrag">private String inputFile;</span>
  1665. </td>
  1666. </tr>
  1667. <tr>
  1668. <td colspan="1" rowspan="1">27.</td>
  1669. <td colspan="1" rowspan="1"></td>
  1670. </tr>
  1671. <tr>
  1672. <td colspan="1" rowspan="1">28.</td>
  1673. <td colspan="1" rowspan="1">
  1674. &nbsp;&nbsp;&nbsp;&nbsp;
  1675. <span class="codefrag">public void configure(JobConf job) {</span>
  1676. </td>
  1677. </tr>
  1678. <tr>
  1679. <td colspan="1" rowspan="1">29.</td>
  1680. <td colspan="1" rowspan="1">
  1681. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1682. <span class="codefrag">
  1683. caseSensitive = job.getBoolean("wordcount.case.sensitive", true);
  1684. </span>
  1685. </td>
  1686. </tr>
  1687. <tr>
  1688. <td colspan="1" rowspan="1">30.</td>
  1689. <td colspan="1" rowspan="1">
  1690. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1691. <span class="codefrag">inputFile = job.get("map.input.file");</span>
  1692. </td>
  1693. </tr>
  1694. <tr>
  1695. <td colspan="1" rowspan="1">31.</td>
  1696. <td colspan="1" rowspan="1"></td>
  1697. </tr>
  1698. <tr>
  1699. <td colspan="1" rowspan="1">32.</td>
  1700. <td colspan="1" rowspan="1">
  1701. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1702. <span class="codefrag">if (job.getBoolean("wordcount.skip.patterns", false)) {</span>
  1703. </td>
  1704. </tr>
  1705. <tr>
  1706. <td colspan="1" rowspan="1">33.</td>
  1707. <td colspan="1" rowspan="1">
  1708. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1709. <span class="codefrag">Path[] patternsFiles = new Path[0];</span>
  1710. </td>
  1711. </tr>
  1712. <tr>
  1713. <td colspan="1" rowspan="1">34.</td>
  1714. <td colspan="1" rowspan="1">
  1715. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1716. <span class="codefrag">try {</span>
  1717. </td>
  1718. </tr>
  1719. <tr>
  1720. <td colspan="1" rowspan="1">35.</td>
  1721. <td colspan="1" rowspan="1">
  1722. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1723. <span class="codefrag">
  1724. patternsFiles = DistributedCache.getLocalCacheFiles(job);
  1725. </span>
  1726. </td>
  1727. </tr>
  1728. <tr>
  1729. <td colspan="1" rowspan="1">36.</td>
  1730. <td colspan="1" rowspan="1">
  1731. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1732. <span class="codefrag">} catch (IOException ioe) {</span>
  1733. </td>
  1734. </tr>
  1735. <tr>
  1736. <td colspan="1" rowspan="1">37.</td>
  1737. <td colspan="1" rowspan="1">
  1738. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1739. <span class="codefrag">
  1740. System.err.println("Caught exception while getting cached files: "
  1741. + StringUtils.stringifyException(ioe));
  1742. </span>
  1743. </td>
  1744. </tr>
  1745. <tr>
  1746. <td colspan="1" rowspan="1">38.</td>
  1747. <td colspan="1" rowspan="1">
  1748. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1749. <span class="codefrag">}</span>
  1750. </td>
  1751. </tr>
  1752. <tr>
  1753. <td colspan="1" rowspan="1">39.</td>
  1754. <td colspan="1" rowspan="1">
  1755. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1756. <span class="codefrag">for (Path patternsFile : patternsFiles) {</span>
  1757. </td>
  1758. </tr>
  1759. <tr>
  1760. <td colspan="1" rowspan="1">40.</td>
  1761. <td colspan="1" rowspan="1">
  1762. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1763. <span class="codefrag">parseSkipFile(patternsFile);</span>
  1764. </td>
  1765. </tr>
  1766. <tr>
  1767. <td colspan="1" rowspan="1">41.</td>
  1768. <td colspan="1" rowspan="1">
  1769. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1770. <span class="codefrag">}</span>
  1771. </td>
  1772. </tr>
  1773. <tr>
  1774. <td colspan="1" rowspan="1">42.</td>
  1775. <td colspan="1" rowspan="1">
  1776. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1777. <span class="codefrag">}</span>
  1778. </td>
  1779. </tr>
  1780. <tr>
  1781. <td colspan="1" rowspan="1">43.</td>
  1782. <td colspan="1" rowspan="1">
  1783. &nbsp;&nbsp;&nbsp;&nbsp;
  1784. <span class="codefrag">}</span>
  1785. </td>
  1786. </tr>
  1787. <tr>
  1788. <td colspan="1" rowspan="1">44.</td>
  1789. <td colspan="1" rowspan="1"></td>
  1790. </tr>
  1791. <tr>
  1792. <td colspan="1" rowspan="1">45.</td>
  1793. <td colspan="1" rowspan="1">
  1794. &nbsp;&nbsp;&nbsp;&nbsp;
  1795. <span class="codefrag">private void parseSkipFile(Path patternsFile) {</span>
  1796. </td>
  1797. </tr>
  1798. <tr>
  1799. <td colspan="1" rowspan="1">46.</td>
  1800. <td colspan="1" rowspan="1">
  1801. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1802. <span class="codefrag">try {</span>
  1803. </td>
  1804. </tr>
  1805. <tr>
  1806. <td colspan="1" rowspan="1">47.</td>
  1807. <td colspan="1" rowspan="1">
  1808. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1809. <span class="codefrag">
  1810. BufferedReader fis =
  1811. new BufferedReader(new FileReader(patternsFile.toString()));
  1812. </span>
  1813. </td>
  1814. </tr>
  1815. <tr>
  1816. <td colspan="1" rowspan="1">48.</td>
  1817. <td colspan="1" rowspan="1">
  1818. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1819. <span class="codefrag">String pattern = null;</span>
  1820. </td>
  1821. </tr>
  1822. <tr>
  1823. <td colspan="1" rowspan="1">49.</td>
  1824. <td colspan="1" rowspan="1">
  1825. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1826. <span class="codefrag">while ((pattern = fis.readLine()) != null) {</span>
  1827. </td>
  1828. </tr>
  1829. <tr>
  1830. <td colspan="1" rowspan="1">50.</td>
  1831. <td colspan="1" rowspan="1">
  1832. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1833. <span class="codefrag">patternsToSkip.add(pattern);</span>
  1834. </td>
  1835. </tr>
  1836. <tr>
  1837. <td colspan="1" rowspan="1">51.</td>
  1838. <td colspan="1" rowspan="1">
  1839. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1840. <span class="codefrag">}</span>
  1841. </td>
  1842. </tr>
  1843. <tr>
  1844. <td colspan="1" rowspan="1">52.</td>
  1845. <td colspan="1" rowspan="1">
  1846. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1847. <span class="codefrag">} catch (IOException ioe) {</span>
  1848. </td>
  1849. </tr>
  1850. <tr>
  1851. <td colspan="1" rowspan="1">53.</td>
  1852. <td colspan="1" rowspan="1">
  1853. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1854. <span class="codefrag">
  1855. System.err.println("Caught exception while parsing the cached file '" +
  1856. patternsFile + "' : " +
  1857. StringUtils.stringifyException(ioe));
  1858. </span>
  1859. </td>
  1860. </tr>
  1861. <tr>
  1862. <td colspan="1" rowspan="1">54.</td>
  1863. <td colspan="1" rowspan="1">
  1864. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1865. <span class="codefrag">}</span>
  1866. </td>
  1867. </tr>
  1868. <tr>
  1869. <td colspan="1" rowspan="1">55.</td>
  1870. <td colspan="1" rowspan="1">
  1871. &nbsp;&nbsp;&nbsp;&nbsp;
  1872. <span class="codefrag">}</span>
  1873. </td>
  1874. </tr>
  1875. <tr>
  1876. <td colspan="1" rowspan="1">56.</td>
  1877. <td colspan="1" rowspan="1"></td>
  1878. </tr>
  1879. <tr>
  1880. <td colspan="1" rowspan="1">57.</td>
  1881. <td colspan="1" rowspan="1">
  1882. &nbsp;&nbsp;&nbsp;&nbsp;
  1883. <span class="codefrag">
  1884. public void map(LongWritable key, Text value,
  1885. OutputCollector&lt;Text, IntWritable&gt; output,
  1886. Reporter reporter) throws IOException {
  1887. </span>
  1888. </td>
  1889. </tr>
  1890. <tr>
  1891. <td colspan="1" rowspan="1">58.</td>
  1892. <td colspan="1" rowspan="1">
  1893. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1894. <span class="codefrag">
  1895. String line =
  1896. (caseSensitive) ? value.toString() :
  1897. value.toString().toLowerCase();
  1898. </span>
  1899. </td>
  1900. </tr>
  1901. <tr>
  1902. <td colspan="1" rowspan="1">59.</td>
  1903. <td colspan="1" rowspan="1"></td>
  1904. </tr>
  1905. <tr>
  1906. <td colspan="1" rowspan="1">60.</td>
  1907. <td colspan="1" rowspan="1">
  1908. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1909. <span class="codefrag">for (String pattern : patternsToSkip) {</span>
  1910. </td>
  1911. </tr>
  1912. <tr>
  1913. <td colspan="1" rowspan="1">61.</td>
  1914. <td colspan="1" rowspan="1">
  1915. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1916. <span class="codefrag">line = line.replaceAll(pattern, "");</span>
  1917. </td>
  1918. </tr>
  1919. <tr>
  1920. <td colspan="1" rowspan="1">62.</td>
  1921. <td colspan="1" rowspan="1">
  1922. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1923. <span class="codefrag">}</span>
  1924. </td>
  1925. </tr>
  1926. <tr>
  1927. <td colspan="1" rowspan="1">63.</td>
  1928. <td colspan="1" rowspan="1"></td>
  1929. </tr>
  1930. <tr>
  1931. <td colspan="1" rowspan="1">64.</td>
  1932. <td colspan="1" rowspan="1">
  1933. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1934. <span class="codefrag">StringTokenizer tokenizer = new StringTokenizer(line);</span>
  1935. </td>
  1936. </tr>
  1937. <tr>
  1938. <td colspan="1" rowspan="1">65.</td>
  1939. <td colspan="1" rowspan="1">
  1940. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1941. <span class="codefrag">while (tokenizer.hasMoreTokens()) {</span>
  1942. </td>
  1943. </tr>
  1944. <tr>
  1945. <td colspan="1" rowspan="1">66.</td>
  1946. <td colspan="1" rowspan="1">
  1947. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1948. <span class="codefrag">word.set(tokenizer.nextToken());</span>
  1949. </td>
  1950. </tr>
  1951. <tr>
  1952. <td colspan="1" rowspan="1">67.</td>
  1953. <td colspan="1" rowspan="1">
  1954. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1955. <span class="codefrag">output.collect(word, one);</span>
  1956. </td>
  1957. </tr>
  1958. <tr>
  1959. <td colspan="1" rowspan="1">68.</td>
  1960. <td colspan="1" rowspan="1">
  1961. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1962. <span class="codefrag">reporter.incrCounter(Counters.INPUT_WORDS, 1);</span>
  1963. </td>
  1964. </tr>
  1965. <tr>
  1966. <td colspan="1" rowspan="1">69.</td>
  1967. <td colspan="1" rowspan="1">
  1968. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1969. <span class="codefrag">}</span>
  1970. </td>
  1971. </tr>
  1972. <tr>
  1973. <td colspan="1" rowspan="1">70.</td>
  1974. <td colspan="1" rowspan="1"></td>
  1975. </tr>
  1976. <tr>
  1977. <td colspan="1" rowspan="1">71.</td>
  1978. <td colspan="1" rowspan="1">
  1979. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1980. <span class="codefrag">if ((++numRecords % 100) == 0) {</span>
  1981. </td>
  1982. </tr>
  1983. <tr>
  1984. <td colspan="1" rowspan="1">72.</td>
  1985. <td colspan="1" rowspan="1">
  1986. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1987. <span class="codefrag">
  1988. reporter.setStatus("Finished processing " + numRecords +
  1989. " records " + "from the input file: " +
  1990. inputFile);
  1991. </span>
  1992. </td>
  1993. </tr>
  1994. <tr>
  1995. <td colspan="1" rowspan="1">73.</td>
  1996. <td colspan="1" rowspan="1">
  1997. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  1998. <span class="codefrag">}</span>
  1999. </td>
  2000. </tr>
  2001. <tr>
  2002. <td colspan="1" rowspan="1">74.</td>
  2003. <td colspan="1" rowspan="1">
  2004. &nbsp;&nbsp;&nbsp;&nbsp;
  2005. <span class="codefrag">}</span>
  2006. </td>
  2007. </tr>
  2008. <tr>
  2009. <td colspan="1" rowspan="1">75.</td>
  2010. <td colspan="1" rowspan="1">
  2011. &nbsp;&nbsp;
  2012. <span class="codefrag">}</span>
  2013. </td>
  2014. </tr>
  2015. <tr>
  2016. <td colspan="1" rowspan="1">76.</td>
  2017. <td colspan="1" rowspan="1"></td>
  2018. </tr>
  2019. <tr>
  2020. <td colspan="1" rowspan="1">77.</td>
  2021. <td colspan="1" rowspan="1">
  2022. &nbsp;&nbsp;
  2023. <span class="codefrag">
  2024. public static class Reduce extends MapReduceBase implements
  2025. Reducer&lt;Text, IntWritable, Text, IntWritable&gt; {
  2026. </span>
  2027. </td>
  2028. </tr>
  2029. <tr>
  2030. <td colspan="1" rowspan="1">78.</td>
  2031. <td colspan="1" rowspan="1">
  2032. &nbsp;&nbsp;&nbsp;&nbsp;
  2033. <span class="codefrag">
  2034. public void reduce(Text key, Iterator&lt;IntWritable&gt; values,
  2035. OutputCollector&lt;Text, IntWritable&gt; output,
  2036. Reporter reporter) throws IOException {
  2037. </span>
  2038. </td>
  2039. </tr>
  2040. <tr>
  2041. <td colspan="1" rowspan="1">79.</td>
  2042. <td colspan="1" rowspan="1">
  2043. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2044. <span class="codefrag">int sum = 0;</span>
  2045. </td>
  2046. </tr>
  2047. <tr>
  2048. <td colspan="1" rowspan="1">80.</td>
  2049. <td colspan="1" rowspan="1">
  2050. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2051. <span class="codefrag">while (values.hasNext()) {</span>
  2052. </td>
  2053. </tr>
  2054. <tr>
  2055. <td colspan="1" rowspan="1">81.</td>
  2056. <td colspan="1" rowspan="1">
  2057. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2058. <span class="codefrag">sum += values.next().get();</span>
  2059. </td>
  2060. </tr>
  2061. <tr>
  2062. <td colspan="1" rowspan="1">82.</td>
  2063. <td colspan="1" rowspan="1">
  2064. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2065. <span class="codefrag">}</span>
  2066. </td>
  2067. </tr>
  2068. <tr>
  2069. <td colspan="1" rowspan="1">83.</td>
  2070. <td colspan="1" rowspan="1">
  2071. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2072. <span class="codefrag">output.collect(key, new IntWritable(sum));</span>
  2073. </td>
  2074. </tr>
  2075. <tr>
  2076. <td colspan="1" rowspan="1">84.</td>
  2077. <td colspan="1" rowspan="1">
  2078. &nbsp;&nbsp;&nbsp;&nbsp;
  2079. <span class="codefrag">}</span>
  2080. </td>
  2081. </tr>
  2082. <tr>
  2083. <td colspan="1" rowspan="1">85.</td>
  2084. <td colspan="1" rowspan="1">
  2085. &nbsp;&nbsp;
  2086. <span class="codefrag">}</span>
  2087. </td>
  2088. </tr>
  2089. <tr>
  2090. <td colspan="1" rowspan="1">86.</td>
  2091. <td colspan="1" rowspan="1"></td>
  2092. </tr>
  2093. <tr>
  2094. <td colspan="1" rowspan="1">87.</td>
  2095. <td colspan="1" rowspan="1">
  2096. &nbsp;&nbsp;
  2097. <span class="codefrag">public int run(String[] args) throws Exception {</span>
  2098. </td>
  2099. </tr>
  2100. <tr>
  2101. <td colspan="1" rowspan="1">88.</td>
  2102. <td colspan="1" rowspan="1">
  2103. &nbsp;&nbsp;&nbsp;&nbsp;
  2104. <span class="codefrag">
  2105. JobConf conf = new JobConf(getConf(), WordCount.class);
  2106. </span>
  2107. </td>
  2108. </tr>
  2109. <tr>
  2110. <td colspan="1" rowspan="1">89.</td>
  2111. <td colspan="1" rowspan="1">
  2112. &nbsp;&nbsp;&nbsp;&nbsp;
  2113. <span class="codefrag">conf.setJobName("wordcount");</span>
  2114. </td>
  2115. </tr>
  2116. <tr>
  2117. <td colspan="1" rowspan="1">90.</td>
  2118. <td colspan="1" rowspan="1"></td>
  2119. </tr>
  2120. <tr>
  2121. <td colspan="1" rowspan="1">91.</td>
  2122. <td colspan="1" rowspan="1">
  2123. &nbsp;&nbsp;&nbsp;&nbsp;
  2124. <span class="codefrag">conf.setOutputKeyClass(Text.class);</span>
  2125. </td>
  2126. </tr>
  2127. <tr>
  2128. <td colspan="1" rowspan="1">92.</td>
  2129. <td colspan="1" rowspan="1">
  2130. &nbsp;&nbsp;&nbsp;&nbsp;
  2131. <span class="codefrag">conf.setOutputValueClass(IntWritable.class);</span>
  2132. </td>
  2133. </tr>
  2134. <tr>
  2135. <td colspan="1" rowspan="1">93.</td>
  2136. <td colspan="1" rowspan="1"></td>
  2137. </tr>
  2138. <tr>
  2139. <td colspan="1" rowspan="1">94.</td>
  2140. <td colspan="1" rowspan="1">
  2141. &nbsp;&nbsp;&nbsp;&nbsp;
  2142. <span class="codefrag">conf.setMapperClass(Map.class);</span>
  2143. </td>
  2144. </tr>
  2145. <tr>
  2146. <td colspan="1" rowspan="1">95.</td>
  2147. <td colspan="1" rowspan="1">
  2148. &nbsp;&nbsp;&nbsp;&nbsp;
  2149. <span class="codefrag">conf.setCombinerClass(Reduce.class);</span>
  2150. </td>
  2151. </tr>
  2152. <tr>
  2153. <td colspan="1" rowspan="1">96.</td>
  2154. <td colspan="1" rowspan="1">
  2155. &nbsp;&nbsp;&nbsp;&nbsp;
  2156. <span class="codefrag">conf.setReducerClass(Reduce.class);</span>
  2157. </td>
  2158. </tr>
  2159. <tr>
  2160. <td colspan="1" rowspan="1">97.</td>
  2161. <td colspan="1" rowspan="1"></td>
  2162. </tr>
  2163. <tr>
  2164. <td colspan="1" rowspan="1">98.</td>
  2165. <td colspan="1" rowspan="1">
  2166. &nbsp;&nbsp;&nbsp;&nbsp;
  2167. <span class="codefrag">conf.setInputFormat(TextInputFormat.class);</span>
  2168. </td>
  2169. </tr>
  2170. <tr>
  2171. <td colspan="1" rowspan="1">99.</td>
  2172. <td colspan="1" rowspan="1">
  2173. &nbsp;&nbsp;&nbsp;&nbsp;
  2174. <span class="codefrag">conf.setOutputFormat(TextOutputFormat.class);</span>
  2175. </td>
  2176. </tr>
  2177. <tr>
  2178. <td colspan="1" rowspan="1">100.</td>
  2179. <td colspan="1" rowspan="1"></td>
  2180. </tr>
  2181. <tr>
  2182. <td colspan="1" rowspan="1">101.</td>
  2183. <td colspan="1" rowspan="1">
  2184. &nbsp;&nbsp;&nbsp;&nbsp;
  2185. <span class="codefrag">
  2186. List&lt;String&gt; other_args = new ArrayList&lt;String&gt;();
  2187. </span>
  2188. </td>
  2189. </tr>
  2190. <tr>
  2191. <td colspan="1" rowspan="1">102.</td>
  2192. <td colspan="1" rowspan="1">
  2193. &nbsp;&nbsp;&nbsp;&nbsp;
  2194. <span class="codefrag">for (int i=0; i &lt; args.length; ++i) {</span>
  2195. </td>
  2196. </tr>
  2197. <tr>
  2198. <td colspan="1" rowspan="1">103.</td>
  2199. <td colspan="1" rowspan="1">
  2200. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2201. <span class="codefrag">if ("-skip".equals(args[i])) {</span>
  2202. </td>
  2203. </tr>
  2204. <tr>
  2205. <td colspan="1" rowspan="1">104.</td>
  2206. <td colspan="1" rowspan="1">
  2207. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2208. <span class="codefrag">
  2209. DistributedCache.addCacheFile(new Path(args[++i]).toUri(), conf);
  2210. </span>
  2211. </td>
  2212. </tr>
  2213. <tr>
  2214. <td colspan="1" rowspan="1">105.</td>
  2215. <td colspan="1" rowspan="1">
  2216. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2217. <span class="codefrag">
  2218. conf.setBoolean("wordcount.skip.patterns", true);
  2219. </span>
  2220. </td>
  2221. </tr>
  2222. <tr>
  2223. <td colspan="1" rowspan="1">106.</td>
  2224. <td colspan="1" rowspan="1">
  2225. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2226. <span class="codefrag">} else {</span>
  2227. </td>
  2228. </tr>
  2229. <tr>
  2230. <td colspan="1" rowspan="1">107.</td>
  2231. <td colspan="1" rowspan="1">
  2232. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2233. <span class="codefrag">other_args.add(args[i]);</span>
  2234. </td>
  2235. </tr>
  2236. <tr>
  2237. <td colspan="1" rowspan="1">108.</td>
  2238. <td colspan="1" rowspan="1">
  2239. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2240. <span class="codefrag">}</span>
  2241. </td>
  2242. </tr>
  2243. <tr>
  2244. <td colspan="1" rowspan="1">109.</td>
  2245. <td colspan="1" rowspan="1">
  2246. &nbsp;&nbsp;&nbsp;&nbsp;
  2247. <span class="codefrag">}</span>
  2248. </td>
  2249. </tr>
  2250. <tr>
  2251. <td colspan="1" rowspan="1">110.</td>
  2252. <td colspan="1" rowspan="1"></td>
  2253. </tr>
  2254. <tr>
  2255. <td colspan="1" rowspan="1">111.</td>
  2256. <td colspan="1" rowspan="1">
  2257. &nbsp;&nbsp;&nbsp;&nbsp;
  2258. <span class="codefrag">conf.setInputPath(new Path(other_args.get(0)));</span>
  2259. </td>
  2260. </tr>
  2261. <tr>
  2262. <td colspan="1" rowspan="1">112.</td>
  2263. <td colspan="1" rowspan="1">
  2264. &nbsp;&nbsp;&nbsp;&nbsp;
  2265. <span class="codefrag">conf.setOutputPath(new Path(other_args.get(1)));</span>
  2266. </td>
  2267. </tr>
  2268. <tr>
  2269. <td colspan="1" rowspan="1">113.</td>
  2270. <td colspan="1" rowspan="1"></td>
  2271. </tr>
  2272. <tr>
  2273. <td colspan="1" rowspan="1">114.</td>
  2274. <td colspan="1" rowspan="1">
  2275. &nbsp;&nbsp;&nbsp;&nbsp;
  2276. <span class="codefrag">JobClient.runJob(conf);</span>
  2277. </td>
  2278. </tr>
  2279. <tr>
  2280. <td colspan="1" rowspan="1">115.</td>
  2281. <td colspan="1" rowspan="1">
  2282. &nbsp;&nbsp;&nbsp;&nbsp;
  2283. <span class="codefrag">return 0;</span>
  2284. </td>
  2285. </tr>
  2286. <tr>
  2287. <td colspan="1" rowspan="1">116.</td>
  2288. <td colspan="1" rowspan="1">
  2289. &nbsp;&nbsp;
  2290. <span class="codefrag">}</span>
  2291. </td>
  2292. </tr>
  2293. <tr>
  2294. <td colspan="1" rowspan="1">117.</td>
  2295. <td colspan="1" rowspan="1"></td>
  2296. </tr>
  2297. <tr>
  2298. <td colspan="1" rowspan="1">118.</td>
  2299. <td colspan="1" rowspan="1">
  2300. &nbsp;&nbsp;
  2301. <span class="codefrag">
  2302. public static void main(String[] args) throws Exception {
  2303. </span>
  2304. </td>
  2305. </tr>
  2306. <tr>
  2307. <td colspan="1" rowspan="1">119.</td>
  2308. <td colspan="1" rowspan="1">
  2309. &nbsp;&nbsp;&nbsp;&nbsp;
  2310. <span class="codefrag">
  2311. int res = ToolRunner.run(new Configuration(), new WordCount(),
  2312. args);
  2313. </span>
  2314. </td>
  2315. </tr>
  2316. <tr>
  2317. <td colspan="1" rowspan="1">120.</td>
  2318. <td colspan="1" rowspan="1">
  2319. &nbsp;&nbsp;&nbsp;&nbsp;
  2320. <span class="codefrag">System.exit(res);</span>
  2321. </td>
  2322. </tr>
  2323. <tr>
  2324. <td colspan="1" rowspan="1">121.</td>
  2325. <td colspan="1" rowspan="1">
  2326. &nbsp;&nbsp;
  2327. <span class="codefrag">}</span>
  2328. </td>
  2329. </tr>
  2330. <tr>
  2331. <td colspan="1" rowspan="1">122.</td>
  2332. <td colspan="1" rowspan="1">
  2333. <span class="codefrag">}</span>
  2334. </td>
  2335. </tr>
  2336. <tr>
  2337. <td colspan="1" rowspan="1">123.</td>
  2338. <td colspan="1" rowspan="1"></td>
  2339. </tr>
  2340. </table>
  2341. <a name="N11214"></a><a name="Sample+Runs"></a>
  2342. <h3 class="h4">Sample Runs</h3>
  2343. <p>Sample text-files as input:</p>
  2344. <p>
  2345. <span class="codefrag">$ bin/hadoop dfs -ls /usr/joe/wordcount/input/</span>
  2346. <br>
  2347. <span class="codefrag">/usr/joe/wordcount/input/file01</span>
  2348. <br>
  2349. <span class="codefrag">/usr/joe/wordcount/input/file02</span>
  2350. <br>
  2351. <br>
  2352. <span class="codefrag">$ bin/hadoop dfs -cat /usr/joe/wordcount/input/file01</span>
  2353. <br>
  2354. <span class="codefrag">Hello World, Bye World!</span>
  2355. <br>
  2356. <br>
  2357. <span class="codefrag">$ bin/hadoop dfs -cat /usr/joe/wordcount/input/file02</span>
  2358. <br>
  2359. <span class="codefrag">Hello Hadoop, Goodbye to hadoop.</span>
  2360. </p>
  2361. <p>Run the application:</p>
  2362. <p>
  2363. <span class="codefrag">
  2364. $ bin/hadoop jar /usr/joe/wordcount.jar org.myorg.WordCount
  2365. /usr/joe/wordcount/input /usr/joe/wordcount/output
  2366. </span>
  2367. </p>
  2368. <p>Output:</p>
  2369. <p>
  2370. <span class="codefrag">
  2371. $ bin/hadoop dfs -cat /usr/joe/wordcount/output/part-00000
  2372. </span>
  2373. <br>
  2374. <span class="codefrag">Bye 1</span>
  2375. <br>
  2376. <span class="codefrag">Goodbye 1</span>
  2377. <br>
  2378. <span class="codefrag">Hadoop, 1</span>
  2379. <br>
  2380. <span class="codefrag">Hello 2</span>
  2381. <br>
  2382. <span class="codefrag">World! 1</span>
  2383. <br>
  2384. <span class="codefrag">World, 1</span>
  2385. <br>
  2386. <span class="codefrag">hadoop. 1</span>
  2387. <br>
  2388. <span class="codefrag">to 1</span>
  2389. <br>
  2390. </p>
  2391. <p>Notice that the inputs differ from the first version we looked at,
  2392. and how they affect the outputs.</p>
  2393. <p>Now, lets plug-in a pattern-file which lists the word-patterns to be
  2394. ignored, via the <span class="codefrag">DistributedCache</span>.</p>
  2395. <p>
  2396. <span class="codefrag">$ hadoop dfs -cat /user/joe/wordcount/patterns.txt</span>
  2397. <br>
  2398. <span class="codefrag">\.</span>
  2399. <br>
  2400. <span class="codefrag">\,</span>
  2401. <br>
  2402. <span class="codefrag">\!</span>
  2403. <br>
  2404. <span class="codefrag">to</span>
  2405. <br>
  2406. </p>
  2407. <p>Run it again, this time with more options:</p>
  2408. <p>
  2409. <span class="codefrag">
  2410. $ bin/hadoop jar /usr/joe/wordcount.jar org.myorg.WordCount
  2411. -Dwordcount.case.sensitive=true /usr/joe/wordcount/input
  2412. /usr/joe/wordcount/output -skip /user/joe/wordcount/patterns.txt
  2413. </span>
  2414. </p>
  2415. <p>As expected, the output:</p>
  2416. <p>
  2417. <span class="codefrag">
  2418. $ bin/hadoop dfs -cat /usr/joe/wordcount/output/part-00000
  2419. </span>
  2420. <br>
  2421. <span class="codefrag">Bye 1</span>
  2422. <br>
  2423. <span class="codefrag">Goodbye 1</span>
  2424. <br>
  2425. <span class="codefrag">Hadoop 1</span>
  2426. <br>
  2427. <span class="codefrag">Hello 2</span>
  2428. <br>
  2429. <span class="codefrag">World 2</span>
  2430. <br>
  2431. <span class="codefrag">hadoop 1</span>
  2432. <br>
  2433. </p>
  2434. <p>Run it once more, this time switch-off case-sensitivity:</p>
  2435. <p>
  2436. <span class="codefrag">
  2437. $ bin/hadoop jar /usr/joe/wordcount.jar org.myorg.WordCount
  2438. -Dwordcount.case.sensitive=false /usr/joe/wordcount/input
  2439. /usr/joe/wordcount/output -skip /user/joe/wordcount/patterns.txt
  2440. </span>
  2441. </p>
  2442. <p>Sure enough, the output:</p>
  2443. <p>
  2444. <span class="codefrag">
  2445. $ bin/hadoop dfs -cat /usr/joe/wordcount/output/part-00000
  2446. </span>
  2447. <br>
  2448. <span class="codefrag">bye 1</span>
  2449. <br>
  2450. <span class="codefrag">goodbye 1</span>
  2451. <br>
  2452. <span class="codefrag">hadoop 2</span>
  2453. <br>
  2454. <span class="codefrag">hello 2</span>
  2455. <br>
  2456. <span class="codefrag">world 2</span>
  2457. <br>
  2458. </p>
  2459. <a name="N112E8"></a><a name="Highlights"></a>
  2460. <h3 class="h4">Highlights</h3>
  2461. <p>The second version of <span class="codefrag">WordCount</span> improves upon the
  2462. previous one by using some features offered by the Map-Reduce framework:
  2463. </p>
  2464. <ul>
  2465. <li>
  2466. Demonstrates how applications can access configuration parameters
  2467. in the <span class="codefrag">configure</span> method of the <span class="codefrag">Mapper</span> (and
  2468. <span class="codefrag">Reducer</span>) implementations (lines 28-43).
  2469. </li>
  2470. <li>
  2471. Demonstrates how the <span class="codefrag">DistributedCache</span> can be used to
  2472. distribute read-only data needed by the jobs. Here it allows the user
  2473. to specify word-patterns to skip while counting (line 104).
  2474. </li>
  2475. <li>
  2476. Demonstrates the utility of the <span class="codefrag">Tool</span> interface and the
  2477. <span class="codefrag">GenericOptionsParser</span> to handle generic Hadoop
  2478. command-line options (lines 87-116, 119).
  2479. </li>
  2480. <li>
  2481. Demonstrates how applications can use <span class="codefrag">Counters</span> (line 68)
  2482. and how they can set application-specific status information via
  2483. the <span class="codefrag">Reporter</span> instance passed to the <span class="codefrag">map</span> (and
  2484. <span class="codefrag">reduce</span>) method (line 72).
  2485. </li>
  2486. </ul>
  2487. </div>
  2488. <p>
  2489. <em>Java and JNI are trademarks or registered trademarks of
  2490. Sun Microsystems, Inc. in the United States and other countries.</em>
  2491. </p>
  2492. </div>
  2493. <!--+
  2494. |end content
  2495. +-->
  2496. <div class="clearboth">&nbsp;</div>
  2497. </div>
  2498. <div id="footer">
  2499. <!--+
  2500. |start bottomstrip
  2501. +-->
  2502. <div class="lastmodified">
  2503. <script type="text/javascript"><!--
  2504. document.write("Last Published: " + document.lastModified);
  2505. // --></script>
  2506. </div>
  2507. <div class="copyright">
  2508. Copyright &copy;
  2509. 2007 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
  2510. </div>
  2511. <!--+
  2512. |end bottomstrip
  2513. +-->
  2514. </div>
  2515. </body>
  2516. </html>