1
0

mapred_tutorial.html 133 KB

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