1
0

mapred_tutorial.html 106 KB

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