mapred_tutorial.html 130 KB

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