mapred_tutorial.html 149 KB

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