mapred_tutorial.html 102 KB

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