mapred_tutorial.html 128 KB

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