mapred_tutorial.html 130 KB

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