mapred_tutorial.html 148 KB

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