mapred_tutorial.html 150 KB

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