mapred_tutorial.html 132 KB

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