mapred_tutorial.html 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723
  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-Reduce+-+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-N10DD5">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. <a name="N104EC"></a><a name="Walk-through"></a>
  894. <h3 class="h4">Walk-through</h3>
  895. <p>The <span class="codefrag">WordCount</span> application is quite straight-forward.</p>
  896. <p>The <span class="codefrag">Mapper</span> implementation (lines 14-26), via the
  897. <span class="codefrag">map</span> method (lines 18-25), processes one line at a time,
  898. as provided by the specified <span class="codefrag">TextInputFormat</span> (line 49).
  899. It then splits the line into tokens separated by whitespaces, via the
  900. <span class="codefrag">StringTokenizer</span>, and emits a key-value pair of
  901. <span class="codefrag">&lt; &lt;word&gt;, 1&gt;</span>.</p>
  902. <p>
  903. For the given sample input the first map emits:<br>
  904. <span class="codefrag">&lt; Hello, 1&gt;</span>
  905. <br>
  906. <span class="codefrag">&lt; World, 1&gt;</span>
  907. <br>
  908. <span class="codefrag">&lt; Bye, 1&gt;</span>
  909. <br>
  910. <span class="codefrag">&lt; World, 1&gt;</span>
  911. <br>
  912. </p>
  913. <p>
  914. The second map emits:<br>
  915. <span class="codefrag">&lt; Hello, 1&gt;</span>
  916. <br>
  917. <span class="codefrag">&lt; Hadoop, 1&gt;</span>
  918. <br>
  919. <span class="codefrag">&lt; Goodbye, 1&gt;</span>
  920. <br>
  921. <span class="codefrag">&lt; Hadoop, 1&gt;</span>
  922. <br>
  923. </p>
  924. <p>We'll learn more about the number of maps spawned for a given job, and
  925. how to control them in a fine-grained manner, a bit later in the
  926. tutorial.</p>
  927. <p>
  928. <span class="codefrag">WordCount</span> also specifies a <span class="codefrag">combiner</span> (line
  929. 46). Hence, the output of each map is passed through the local combiner
  930. (which is same as the <span class="codefrag">Reducer</span> as per the job
  931. configuration) for local aggregation, after being sorted on the
  932. <em>key</em>s.</p>
  933. <p>
  934. The output of the first map:<br>
  935. <span class="codefrag">&lt; Bye, 1&gt;</span>
  936. <br>
  937. <span class="codefrag">&lt; Hello, 1&gt;</span>
  938. <br>
  939. <span class="codefrag">&lt; World, 2&gt;</span>
  940. <br>
  941. </p>
  942. <p>
  943. The output of the second map:<br>
  944. <span class="codefrag">&lt; Goodbye, 1&gt;</span>
  945. <br>
  946. <span class="codefrag">&lt; Hadoop, 2&gt;</span>
  947. <br>
  948. <span class="codefrag">&lt; Hello, 1&gt;</span>
  949. <br>
  950. </p>
  951. <p>The <span class="codefrag">Reducer</span> implementation (lines 28-36), via the
  952. <span class="codefrag">reduce</span> method (lines 29-35) just sums up the values,
  953. which are the occurence counts for each key (i.e. words in this example).
  954. </p>
  955. <p>
  956. Thus the output of the job is:<br>
  957. <span class="codefrag">&lt; Bye, 1&gt;</span>
  958. <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, 2&gt;</span>
  964. <br>
  965. <span class="codefrag">&lt; World, 2&gt;</span>
  966. <br>
  967. </p>
  968. <p>The <span class="codefrag">run</span> method specifies various facets of the job, such
  969. as the input/output paths (passed via the command line), key/value
  970. types, input/output formats etc., in the <span class="codefrag">JobConf</span>.
  971. It then calls the <span class="codefrag">JobClient.runJob</span> (line 55) to submit the
  972. and monitor its progress.</p>
  973. <p>We'll learn more about <span class="codefrag">JobConf</span>, <span class="codefrag">JobClient</span>,
  974. <span class="codefrag">Tool</span> and other interfaces and classes a bit later in the
  975. tutorial.</p>
  976. </div>
  977. <a name="N105A3"></a><a name="Map-Reduce+-+User+Interfaces"></a>
  978. <h2 class="h3">Map-Reduce - User Interfaces</h2>
  979. <div class="section">
  980. <p>This section provides a reasonable amount of detail on every user-facing
  981. aspect of the Map-Reduce framwork. This should help users implement,
  982. configure and tune their jobs in a fine-grained manner. However, please
  983. note that the javadoc for each class/interface remains the most
  984. comprehensive documentation available; this is only meant to be a tutorial.
  985. </p>
  986. <p>Let us first take the <span class="codefrag">Mapper</span> and <span class="codefrag">Reducer</span>
  987. interfaces. Applications typically implement them to provide the
  988. <span class="codefrag">map</span> and <span class="codefrag">reduce</span> methods.</p>
  989. <p>We will then discuss other core interfaces including
  990. <span class="codefrag">JobConf</span>, <span class="codefrag">JobClient</span>, <span class="codefrag">Partitioner</span>,
  991. <span class="codefrag">OutputCollector</span>, <span class="codefrag">Reporter</span>,
  992. <span class="codefrag">InputFormat</span>, <span class="codefrag">OutputFormat</span> and others.</p>
  993. <p>Finally, we will wrap up by discussing some useful features of the
  994. framework such as the <span class="codefrag">DistributedCache</span>,
  995. <span class="codefrag">IsolationRunner</span> etc.</p>
  996. <a name="N105DC"></a><a name="Payload"></a>
  997. <h3 class="h4">Payload</h3>
  998. <p>Applications typically implement the <span class="codefrag">Mapper</span> and
  999. <span class="codefrag">Reducer</span> interfaces to provide the <span class="codefrag">map</span> and
  1000. <span class="codefrag">reduce</span> methods. These form the core of the job.</p>
  1001. <a name="N105F1"></a><a name="Mapper"></a>
  1002. <h4>Mapper</h4>
  1003. <p>
  1004. <a href="api/org/apache/hadoop/mapred/Mapper.html">
  1005. Mapper</a> maps input key/value pairs to a set of intermediate
  1006. key/value pairs.</p>
  1007. <p>Maps are the individual tasks that transform input records into
  1008. intermediate records. The transformed intermediate records do not need
  1009. to be of the same type as the input records. A given input pair may
  1010. map to zero or many output pairs.</p>
  1011. <p>The Hadoop Map-Reduce framework spawns one map task for each
  1012. <span class="codefrag">InputSplit</span> generated by the <span class="codefrag">InputFormat</span> for
  1013. the job.</p>
  1014. <p>Overall, <span class="codefrag">Mapper</span> implementations are passed the
  1015. <span class="codefrag">JobConf</span> for the job via the
  1016. <a href="api/org/apache/hadoop/mapred/JobConfigurable.html#configure(org.apache.hadoop.mapred.JobConf)">
  1017. JobConfigurable.configure(JobConf)</a> method and override it to
  1018. initialize themselves. The framework then calls
  1019. <a href="api/org/apache/hadoop/mapred/Mapper.html#map(K1, V1, org.apache.hadoop.mapred.OutputCollector, org.apache.hadoop.mapred.Reporter)">
  1020. map(WritableComparable, Writable, OutputCollector, Reporter)</a> for
  1021. each key/value pair in the <span class="codefrag">InputSplit</span> for that task.
  1022. Applications can then override the
  1023. <a href="api/org/apache/hadoop/io/Closeable.html#close()">
  1024. Closeable.close()</a> method to perform any required cleanup.</p>
  1025. <p>Output pairs do not need to be of the same types as input pairs. A
  1026. given input pair may map to zero or many output pairs. Output pairs
  1027. are collected with calls to
  1028. <a href="api/org/apache/hadoop/mapred/OutputCollector.html#collect(K, V)">
  1029. OutputCollector.collect(WritableComparable,Writable)</a>.</p>
  1030. <p>Applications can use the <span class="codefrag">Reporter</span> to report
  1031. progress, set application-level status messages and update
  1032. <span class="codefrag">Counters</span>, or just indicate that they are alive.</p>
  1033. <p>All intermediate values associated with a given output key are
  1034. subsequently grouped by the framework, and passed to the
  1035. <span class="codefrag">Reducer</span>(s) to determine the final output. Users can
  1036. control the grouping by specifying a <span class="codefrag">Comparator</span> via
  1037. <a href="api/org/apache/hadoop/mapred/JobConf.html#setOutputKeyComparatorClass(java.lang.Class)">
  1038. JobConf.setOutputKeyComparatorClass(Class)</a>.</p>
  1039. <p>The <span class="codefrag">Mapper</span> outputs are sorted and then
  1040. partitioned per <span class="codefrag">Reducer</span>. The total number of partitions is
  1041. the same as the number of reduce tasks for the job. Users can control
  1042. which keys (and hence records) go to which <span class="codefrag">Reducer</span> by
  1043. implementing a custom <span class="codefrag">Partitioner</span>.</p>
  1044. <p>Users can optionally specify a <span class="codefrag">combiner</span>, via
  1045. <a href="api/org/apache/hadoop/mapred/JobConf.html#setCombinerClass(java.lang.Class)">
  1046. JobConf.setCombinerClass(Class)</a>, to perform local aggregation of
  1047. the intermediate outputs, which helps to cut down the amount of data
  1048. transferred from the <span class="codefrag">Mapper</span> to the <span class="codefrag">Reducer</span>.
  1049. </p>
  1050. <p>The intermediate, sorted outputs are always stored in a simple
  1051. (key-len, key, value-len, value) format.
  1052. Applications can control if, and how, the
  1053. intermediate outputs are to be compressed and the
  1054. <a href="api/org/apache/hadoop/io/compress/CompressionCodec.html">
  1055. CompressionCodec</a> to be used via the <span class="codefrag">JobConf</span>.
  1056. </p>
  1057. <a name="N10667"></a><a name="How+Many+Maps%3F"></a>
  1058. <h5>How Many Maps?</h5>
  1059. <p>The number of maps is usually driven by the total size of the
  1060. inputs, that is, the total number of blocks of the input files.</p>
  1061. <p>The right level of parallelism for maps seems to be around 10-100
  1062. maps per-node, although it has been set up to 300 maps for very
  1063. cpu-light map tasks. Task setup takes awhile, so it is best if the
  1064. maps take at least a minute to execute.</p>
  1065. <p>Thus, if you expect 10TB of input data and have a blocksize of
  1066. <span class="codefrag">128MB</span>, you'll end up with 82,000 maps, unless
  1067. <a href="api/org/apache/hadoop/mapred/JobConf.html#setNumMapTasks(int)">
  1068. setNumMapTasks(int)</a> (which only provides a hint to the framework)
  1069. is used to set it even higher.</p>
  1070. <a name="N1067F"></a><a name="Reducer"></a>
  1071. <h4>Reducer</h4>
  1072. <p>
  1073. <a href="api/org/apache/hadoop/mapred/Reducer.html">
  1074. Reducer</a> reduces a set of intermediate values which share a key to
  1075. a smaller set of values.</p>
  1076. <p>The number of reduces for the job is set by the user
  1077. via <a href="api/org/apache/hadoop/mapred/JobConf.html#setNumReduceTasks(int)">
  1078. JobConf.setNumReduceTasks(int)</a>.</p>
  1079. <p>Overall, <span class="codefrag">Reducer</span> implementations are passed the
  1080. <span class="codefrag">JobConf</span> for the job via the
  1081. <a href="api/org/apache/hadoop/mapred/JobConfigurable.html#configure(org.apache.hadoop.mapred.JobConf)">
  1082. JobConfigurable.configure(JobConf)</a> method and can override it to
  1083. initialize themselves. The framework then calls
  1084. <a href="api/org/apache/hadoop/mapred/Reducer.html#reduce(K2, java.util.Iterator, org.apache.hadoop.mapred.OutputCollector, org.apache.hadoop.mapred.Reporter)">
  1085. reduce(WritableComparable, Iterator, OutputCollector, Reporter)</a>
  1086. method for each <span class="codefrag">&lt;key, (list of values)&gt;</span>
  1087. pair in the grouped inputs. Applications can then override the
  1088. <a href="api/org/apache/hadoop/io/Closeable.html#close()">
  1089. Closeable.close()</a> method to perform any required cleanup.</p>
  1090. <p>
  1091. <span class="codefrag">Reducer</span> has 3 primary phases: shuffle, sort and reduce.
  1092. </p>
  1093. <a name="N106AF"></a><a name="Shuffle"></a>
  1094. <h5>Shuffle</h5>
  1095. <p>Input to the <span class="codefrag">Reducer</span> is the sorted output of the
  1096. mappers. In this phase the framework fetches the relevant partition
  1097. of the output of all the mappers, via HTTP.</p>
  1098. <a name="N106BC"></a><a name="Sort"></a>
  1099. <h5>Sort</h5>
  1100. <p>The framework groups <span class="codefrag">Reducer</span> inputs by keys (since
  1101. different mappers may have output the same key) in this stage.</p>
  1102. <p>The shuffle and sort phases occur simultaneously; while
  1103. map-outputs are being fetched they are merged.</p>
  1104. <a name="N106CB"></a><a name="Secondary+Sort"></a>
  1105. <h5>Secondary Sort</h5>
  1106. <p>If equivalence rules for grouping the intermediate keys are
  1107. required to be different from those for grouping keys before
  1108. reduction, then one may specify a <span class="codefrag">Comparator</span> via
  1109. <a href="api/org/apache/hadoop/mapred/JobConf.html#setOutputValueGroupingComparator(java.lang.Class)">
  1110. JobConf.setOutputValueGroupingComparator(Class)</a>. Since
  1111. <a href="api/org/apache/hadoop/mapred/JobConf.html#setOutputKeyComparatorClass(java.lang.Class)">
  1112. JobConf.setOutputKeyComparatorClass(Class)</a> can be used to
  1113. control how intermediate keys are grouped, these can be used in
  1114. conjunction to simulate <em>secondary sort on values</em>.</p>
  1115. <a name="N106E4"></a><a name="Reduce"></a>
  1116. <h5>Reduce</h5>
  1117. <p>In this phase the
  1118. <a href="api/org/apache/hadoop/mapred/Reducer.html#reduce(K2, java.util.Iterator, org.apache.hadoop.mapred.OutputCollector, org.apache.hadoop.mapred.Reporter)">
  1119. reduce(WritableComparable, Iterator, OutputCollector, Reporter)</a>
  1120. method is called for each <span class="codefrag">&lt;key, (list of values)&gt;</span>
  1121. pair in the grouped inputs.</p>
  1122. <p>The output of the reduce task is typically written to the
  1123. <a href="api/org/apache/hadoop/fs/FileSystem.html">
  1124. FileSystem</a> via
  1125. <a href="api/org/apache/hadoop/mapred/OutputCollector.html#collect(K, V)">
  1126. OutputCollector.collect(WritableComparable, Writable)</a>.</p>
  1127. <p>Applications can use the <span class="codefrag">Reporter</span> to report
  1128. progress, set application-level status messages and update
  1129. <span class="codefrag">Counters</span>, or just indicate that they are alive.</p>
  1130. <p>The output of the <span class="codefrag">Reducer</span> is <em>not sorted</em>.</p>
  1131. <a name="N10712"></a><a name="How+Many+Reduces%3F"></a>
  1132. <h5>How Many Reduces?</h5>
  1133. <p>The right number of reduces seems to be <span class="codefrag">0.95</span> or
  1134. <span class="codefrag">1.75</span> multiplied by (&lt;<em>no. of nodes</em>&gt; *
  1135. <span class="codefrag">mapred.tasktracker.reduce.tasks.maximum</span>).</p>
  1136. <p>With <span class="codefrag">0.95</span> all of the reduces can launch immediately
  1137. and start transfering map outputs as the maps finish. With
  1138. <span class="codefrag">1.75</span> the faster nodes will finish their first round of
  1139. reduces and launch a second wave of reduces doing a much better job
  1140. of load balancing.</p>
  1141. <p>Increasing the number of reduces increases the framework overhead,
  1142. but increases load balancing and lowers the cost of failures.</p>
  1143. <p>The scaling factors above are slightly less than whole numbers to
  1144. reserve a few reduce slots in the framework for speculative-tasks and
  1145. failed tasks.</p>
  1146. <a name="N10737"></a><a name="Reducer+NONE"></a>
  1147. <h5>Reducer NONE</h5>
  1148. <p>It is legal to set the number of reduce-tasks to <em>zero</em> if
  1149. no reduction is desired.</p>
  1150. <p>In this case the outputs of the map-tasks go directly to the
  1151. <span class="codefrag">FileSystem</span>, into the output path set by
  1152. <a href="api/org/apache/hadoop/mapred/FileOutputFormat.html#setOutputPath(org.apache.hadoop.mapred.JobConf,%20org.apache.hadoop.fs.Path)">
  1153. setOutputPath(Path)</a>. The framework does not sort the
  1154. map-outputs before writing them out to the <span class="codefrag">FileSystem</span>.
  1155. </p>
  1156. <a name="N10752"></a><a name="Partitioner"></a>
  1157. <h4>Partitioner</h4>
  1158. <p>
  1159. <a href="api/org/apache/hadoop/mapred/Partitioner.html">
  1160. Partitioner</a> partitions the key space.</p>
  1161. <p>Partitioner controls the partitioning of the keys of the
  1162. intermediate map-outputs. The key (or a subset of the key) is used to
  1163. derive the partition, typically by a <em>hash function</em>. The total
  1164. number of partitions is the same as the number of reduce tasks for the
  1165. job. Hence this controls which of the <span class="codefrag">m</span> reduce tasks the
  1166. intermediate key (and hence the record) is sent to for reduction.</p>
  1167. <p>
  1168. <a href="api/org/apache/hadoop/mapred/lib/HashPartitioner.html">
  1169. HashPartitioner</a> is the default <span class="codefrag">Partitioner</span>.</p>
  1170. <a name="N10771"></a><a name="Reporter"></a>
  1171. <h4>Reporter</h4>
  1172. <p>
  1173. <a href="api/org/apache/hadoop/mapred/Reporter.html">
  1174. Reporter</a> is a facility for Map-Reduce applications to report
  1175. progress, set application-level status messages and update
  1176. <span class="codefrag">Counters</span>.</p>
  1177. <p>
  1178. <span class="codefrag">Mapper</span> and <span class="codefrag">Reducer</span> implementations can use
  1179. the <span class="codefrag">Reporter</span> to report progress or just indicate
  1180. that they are alive. In scenarios where the application takes a
  1181. significant amount of time to process individual key/value pairs,
  1182. this is crucial since the framework might assume that the task has
  1183. timed-out and kill that task. Another way to avoid this is to
  1184. set the configuration parameter <span class="codefrag">mapred.task.timeout</span> to a
  1185. high-enough value (or even set it to <em>zero</em> for no time-outs).
  1186. </p>
  1187. <p>Applications can also update <span class="codefrag">Counters</span> using the
  1188. <span class="codefrag">Reporter</span>.</p>
  1189. <a name="N1079B"></a><a name="OutputCollector"></a>
  1190. <h4>OutputCollector</h4>
  1191. <p>
  1192. <a href="api/org/apache/hadoop/mapred/OutputCollector.html">
  1193. OutputCollector</a> is a generalization of the facility provided by
  1194. the Map-Reduce framework to collect data output by the
  1195. <span class="codefrag">Mapper</span> or the <span class="codefrag">Reducer</span> (either the
  1196. intermediate outputs or the output of the job).</p>
  1197. <p>Hadoop Map-Reduce comes bundled with a
  1198. <a href="api/org/apache/hadoop/mapred/lib/package-summary.html">
  1199. library</a> of generally useful mappers, reducers, and partitioners.</p>
  1200. <a name="N107B6"></a><a name="Job+Configuration"></a>
  1201. <h3 class="h4">Job Configuration</h3>
  1202. <p>
  1203. <a href="api/org/apache/hadoop/mapred/JobConf.html">
  1204. JobConf</a> represents a Map-Reduce job configuration.</p>
  1205. <p>
  1206. <span class="codefrag">JobConf</span> is the primary interface for a user to describe
  1207. a map-reduce job to the Hadoop framework for execution. The framework
  1208. tries to faithfully execute the job as described by <span class="codefrag">JobConf</span>,
  1209. however:</p>
  1210. <ul>
  1211. <li>f
  1212. Some configuration parameters may have been marked as
  1213. <a href="api/org/apache/hadoop/conf/Configuration.html#FinalParams">
  1214. final</a> by administrators and hence cannot be altered.
  1215. </li>
  1216. <li>
  1217. While some job parameters are straight-forward to set (e.g.
  1218. <a href="api/org/apache/hadoop/mapred/JobConf.html#setNumReduceTasks(int)">
  1219. setNumReduceTasks(int)</a>), other parameters interact subtly with
  1220. the rest of the framework and/or job configuration and are
  1221. more complex to set (e.g.
  1222. <a href="api/org/apache/hadoop/mapred/JobConf.html#setNumMapTasks(int)">
  1223. setNumMapTasks(int)</a>).
  1224. </li>
  1225. </ul>
  1226. <p>
  1227. <span class="codefrag">JobConf</span> is typically used to specify the
  1228. <span class="codefrag">Mapper</span>, combiner (if any), <span class="codefrag">Partitioner</span>,
  1229. <span class="codefrag">Reducer</span>, <span class="codefrag">InputFormat</span> and
  1230. <span class="codefrag">OutputFormat</span> implementations. <span class="codefrag">JobConf</span> also
  1231. indicates the set of input files
  1232. (<a href="api/org/apache/hadoop/mapred/FileInputFormat.html#setInputPaths(org.apache.hadoop.mapred.JobConf,%20org.apache.hadoop.fs.Path[])">setInputPaths(JobConf, Path...)</a>
  1233. /<a href="api/org/apache/hadoop/mapred/FileInputFormat.html#addInputPath(org.apache.hadoop.mapred.JobConf,%20org.apache.hadoop.fs.Path)">addInputPath(JobConf, Path)</a>)
  1234. and (<a href="api/org/apache/hadoop/mapred/FileInputFormat.html#setInputPaths(org.apache.hadoop.mapred.JobConf,%20java.lang.String)">setInputPaths(JobConf, String)</a>
  1235. /<a href="api/org/apache/hadoop/mapred/FileInputFormat.html#addInputPath(org.apache.hadoop.mapred.JobConf,%20java.lang.String)">addInputPaths(JobConf, String)</a>)
  1236. and where the output files should be written
  1237. (<a href="api/org/apache/hadoop/mapred/FileOutputFormat.html#setOutputPath(org.apache.hadoop.mapred.JobConf,%20org.apache.hadoop.fs.Path)">setOutputPath(Path)</a>).</p>
  1238. <p>Optionally, <span class="codefrag">JobConf</span> is used to specify other advanced
  1239. facets of the job such as the <span class="codefrag">Comparator</span> to be used, files
  1240. to be put in the <span class="codefrag">DistributedCache</span>, whether intermediate
  1241. and/or job outputs are to be compressed (and how), debugging via
  1242. user-provided scripts
  1243. (<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>)
  1244. , whether job tasks can be executed in a <em>speculative</em> manner
  1245. (<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>)
  1246. , maximum number of attempts per task
  1247. (<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>)
  1248. , percentage of tasks failure which can be tolerated by the job
  1249. (<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>)
  1250. etc.</p>
  1251. <p>Of course, users can use
  1252. <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>
  1253. to set/get arbitrary parameters needed by applications. However, use the
  1254. <span class="codefrag">DistributedCache</span> for large amounts of (read-only) data.</p>
  1255. <a name="N10848"></a><a name="Task+Execution+%26+Environment"></a>
  1256. <h3 class="h4">Task Execution &amp; Environment</h3>
  1257. <p>The <span class="codefrag">TaskTracker</span> executes the <span class="codefrag">Mapper</span>/
  1258. <span class="codefrag">Reducer</span> <em>task</em> as a child process in a separate jvm.
  1259. </p>
  1260. <p>The child-task inherits the environment of the parent
  1261. <span class="codefrag">TaskTracker</span>. The user can specify additional options to the
  1262. child-jvm via the <span class="codefrag">mapred.child.java.opts</span> configuration
  1263. parameter in the <span class="codefrag">JobConf</span> such as non-standard paths for the
  1264. run-time linker to search shared libraries via
  1265. <span class="codefrag">-Djava.library.path=&lt;&gt;</span> etc. If the
  1266. <span class="codefrag">mapred.child.java.opts</span> contains the symbol <em>@taskid@</em>
  1267. it is interpolated with value of <span class="codefrag">taskid</span> of the map/reduce
  1268. task.</p>
  1269. <p>Here is an example with multiple arguments and substitutions,
  1270. showing jvm GC logging, and start of a passwordless JVM JMX agent so that
  1271. it can connect with jconsole and the likes to watch child memory,
  1272. threads and get thread dumps. It also sets the maximum heap-size of the
  1273. child jvm to 512MB and adds an additional path to the
  1274. <span class="codefrag">java.library.path</span> of the child-jvm.</p>
  1275. <p>
  1276. <span class="codefrag">&lt;property&gt;</span>
  1277. <br>
  1278. &nbsp;&nbsp;<span class="codefrag">&lt;name&gt;mapred.child.java.opts&lt;/name&gt;</span>
  1279. <br>
  1280. &nbsp;&nbsp;<span class="codefrag">&lt;value&gt;</span>
  1281. <br>
  1282. &nbsp;&nbsp;&nbsp;&nbsp;<span class="codefrag">
  1283. -Xmx512M -Djava.library.path=/home/mycompany/lib
  1284. -verbose:gc -Xloggc:/tmp/@taskid@.gc</span>
  1285. <br>
  1286. &nbsp;&nbsp;&nbsp;&nbsp;<span class="codefrag">
  1287. -Dcom.sun.management.jmxremote.authenticate=false
  1288. -Dcom.sun.management.jmxremote.ssl=false</span>
  1289. <br>
  1290. &nbsp;&nbsp;<span class="codefrag">&lt;/value&gt;</span>
  1291. <br>
  1292. <span class="codefrag">&lt;/property&gt;</span>
  1293. </p>
  1294. <p>Users/admins can also specify the maximum virtual memory
  1295. of the launched child-task using <span class="codefrag">mapred.child.ulimit</span>.
  1296. The value for <span class="codefrag">mapred.child.ulimit</span> should be specified
  1297. in kilo bytes (KB). And also the value must be greater than
  1298. or equal to the -Xmx passed to JavaVM, else the VM might not start.
  1299. </p>
  1300. <p>Note: <span class="codefrag">mapred.child.java.opts</span> are used only for
  1301. configuring the launched child tasks from task tracker. Configuring
  1302. the memory options for daemons is documented in
  1303. <a href="cluster_setup.html#Configuring+the+Environment+of+the+Hadoop+Daemons">
  1304. cluster_setup.html </a>
  1305. </p>
  1306. <p>The task tracker has local directory,
  1307. <span class="codefrag"> ${mapred.local.dir}/taskTracker/</span> to create localized
  1308. cache and localized job. It can define multiple local directories
  1309. (spanning multiple disks) and then each filename is assigned to a
  1310. semi-random local directory. When the job starts, task tracker
  1311. creates a localized job directory relative to the local directory
  1312. specified in the configuration. Thus the task tracker directory
  1313. structure looks the following: </p>
  1314. <ul>
  1315. <li>
  1316. <span class="codefrag">${mapred.local.dir}/taskTracker/archive/</span> :
  1317. The distributed cache. This directory holds the localized distributed
  1318. cache. Thus localized distributed cache is shared among all
  1319. the tasks and jobs </li>
  1320. <li>
  1321. <span class="codefrag">${mapred.local.dir}/taskTracker/jobcache/$jobid/</span> :
  1322. The localized job directory
  1323. <ul>
  1324. <li>
  1325. <span class="codefrag">${mapred.local.dir}/taskTracker/jobcache/$jobid/work/</span>
  1326. : The job-specific shared directory. The tasks can use this space as
  1327. scratch space and share files among them. This directory is exposed
  1328. to the users through the configuration property
  1329. <span class="codefrag">job.local.dir</span>. The directory can accessed through
  1330. api <a href="api/org/apache/hadoop/mapred/JobConf.html#getJobLocalDir()">
  1331. JobConf.getJobLocalDir()</a>. It is available as System property also.
  1332. So, users (streaming etc.) can call
  1333. <span class="codefrag">System.getProperty("job.local.dir")</span> to access the
  1334. directory.</li>
  1335. <li>
  1336. <span class="codefrag">${mapred.local.dir}/taskTracker/jobcache/$jobid/jars/</span>
  1337. : The jars directory, which has the job jar file and expanded jar.
  1338. The <span class="codefrag">job.jar</span> is the application's jar file that is
  1339. automatically distributed to each machine. It is expanded in jars
  1340. directory before the tasks for the job start. The job.jar location
  1341. is accessible to the application through the api
  1342. <a href="api/org/apache/hadoop/mapred/JobConf.html#getJar()">
  1343. JobConf.getJar() </a>. To access the unjarred directory,
  1344. JobConf.getJar().getParent() can be called.</li>
  1345. <li>
  1346. <span class="codefrag">${mapred.local.dir}/taskTracker/jobcache/$jobid/job.xml</span>
  1347. : The job.xml file, the generic job configuration, localized for
  1348. the job. </li>
  1349. <li>
  1350. <span class="codefrag">${mapred.local.dir}/taskTracker/jobcache/$jobid/$taskid</span>
  1351. : The task direcrory for each task attempt. Each task directory
  1352. again has the following structure :
  1353. <ul>
  1354. <li>
  1355. <span class="codefrag">${mapred.local.dir}/taskTracker/jobcache/$jobid/$taskid/job.xml</span>
  1356. : A job.xml file, task localized job configuration, Task localization
  1357. means that properties have been set that are specific to
  1358. this particular task within the job. The properties localized for
  1359. each task are described below.</li>
  1360. <li>
  1361. <span class="codefrag">${mapred.local.dir}/taskTracker/jobcache/$jobid/$taskid/output</span>
  1362. : A directory for intermediate output files. This contains the
  1363. temporary map reduce data generated by the framework
  1364. such as map output files etc. </li>
  1365. <li>
  1366. <span class="codefrag">${mapred.local.dir}/taskTracker/jobcache/$jobid/$taskid/work</span>
  1367. : The curernt working directory of the task. </li>
  1368. <li>
  1369. <span class="codefrag">${mapred.local.dir}/taskTracker/jobcache/$jobid/$taskid/work/tmp</span>
  1370. : The temporary directory for the task.
  1371. (User can specify the property <span class="codefrag">mapred.child.tmp</span> to set
  1372. the value of temporary directory for map and reduce tasks. This
  1373. defaults to <span class="codefrag">./tmp</span>. If the value is not an absolute path,
  1374. it is prepended with task's working directory. Otherwise, it is
  1375. directly assigned. The directory will be created if it doesn't exist.
  1376. Then, the child java tasks are executed with option
  1377. <span class="codefrag">-Djava.io.tmpdir='the absolute path of the tmp dir'</span>.
  1378. Anp pipes and streaming are set with environment variable,
  1379. <span class="codefrag">TMPDIR='the absolute path of the tmp dir'</span>). This
  1380. directory is created, if <span class="codefrag">mapred.child.tmp</span> has the value
  1381. <span class="codefrag">./tmp</span>
  1382. </li>
  1383. </ul>
  1384. </li>
  1385. </ul>
  1386. </li>
  1387. </ul>
  1388. <p>The following properties are localized in the job configuration
  1389. for each task's execution: </p>
  1390. <table class="ForrestTable" cellspacing="1" cellpadding="4">
  1391. <tr>
  1392. <th colspan="1" rowspan="1">Name</th><th colspan="1" rowspan="1">Type</th><th colspan="1" rowspan="1">Description</th>
  1393. </tr>
  1394. <tr>
  1395. <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>
  1396. </tr>
  1397. <tr>
  1398. <td colspan="1" rowspan="1">mapred.jar</td><td colspan="1" rowspan="1">String</td>
  1399. <td colspan="1" rowspan="1">job.jar location in job directory</td>
  1400. </tr>
  1401. <tr>
  1402. <td colspan="1" rowspan="1">job.local.dir</td><td colspan="1" rowspan="1"> String</td>
  1403. <td colspan="1" rowspan="1"> The job specific shared scratch space</td>
  1404. </tr>
  1405. <tr>
  1406. <td colspan="1" rowspan="1">mapred.tip.id</td><td colspan="1" rowspan="1"> String</td>
  1407. <td colspan="1" rowspan="1"> The task id</td>
  1408. </tr>
  1409. <tr>
  1410. <td colspan="1" rowspan="1">mapred.task.id</td><td colspan="1" rowspan="1"> String</td>
  1411. <td colspan="1" rowspan="1"> The task attempt id</td>
  1412. </tr>
  1413. <tr>
  1414. <td colspan="1" rowspan="1">mapred.task.is.map</td><td colspan="1" rowspan="1"> boolean </td>
  1415. <td colspan="1" rowspan="1">Is this a map task</td>
  1416. </tr>
  1417. <tr>
  1418. <td colspan="1" rowspan="1">mapred.task.partition</td><td colspan="1" rowspan="1"> int </td>
  1419. <td colspan="1" rowspan="1">The id of the task within the job</td>
  1420. </tr>
  1421. <tr>
  1422. <td colspan="1" rowspan="1">map.input.file</td><td colspan="1" rowspan="1"> String</td>
  1423. <td colspan="1" rowspan="1"> The filename that the map is reading from</td>
  1424. </tr>
  1425. <tr>
  1426. <td colspan="1" rowspan="1">map.input.start</td><td colspan="1" rowspan="1"> long</td>
  1427. <td colspan="1" rowspan="1"> The offset of the start of the map input split</td>
  1428. </tr>
  1429. <tr>
  1430. <td colspan="1" rowspan="1">map.input.length </td><td colspan="1" rowspan="1">long </td>
  1431. <td colspan="1" rowspan="1">The number of bytes in the map input split</td>
  1432. </tr>
  1433. <tr>
  1434. <td colspan="1" rowspan="1">mapred.work.output.dir</td><td colspan="1" rowspan="1"> String </td>
  1435. <td colspan="1" rowspan="1">The task's temporary output directory</td>
  1436. </tr>
  1437. </table>
  1438. <p>The standard output (stdout) and error (stderr) streams of the task
  1439. are read by the TaskTracker and logged to
  1440. <span class="codefrag">${HADOOP_LOG_DIR}/userlogs</span>
  1441. </p>
  1442. <p>The <a href="#DistributedCache">DistributedCache</a> can also be used
  1443. to distribute both jars and native libraries for use in the map
  1444. and/or reduce tasks. The child-jvm always has its
  1445. <em>current working directory</em> added to the
  1446. <span class="codefrag">java.library.path</span> and <span class="codefrag">LD_LIBRARY_PATH</span>.
  1447. And hence the cached libraries can be loaded via
  1448. <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#loadLibrary(java.lang.String)">
  1449. System.loadLibrary</a> or
  1450. <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#load(java.lang.String)">
  1451. System.load</a>. More details on how to load shared libraries through
  1452. distributed cache are documented at
  1453. <a href="native_libraries.html#Loading+native+libraries+through+DistributedCache">
  1454. native_libraries.html</a>
  1455. </p>
  1456. <a name="N109E8"></a><a name="Job+Submission+and+Monitoring"></a>
  1457. <h3 class="h4">Job Submission and Monitoring</h3>
  1458. <p>
  1459. <a href="api/org/apache/hadoop/mapred/JobClient.html">
  1460. JobClient</a> is the primary interface by which user-job interacts
  1461. with the <span class="codefrag">JobTracker</span>.</p>
  1462. <p>
  1463. <span class="codefrag">JobClient</span> provides facilities to submit jobs, track their
  1464. progress, access component-tasks' reports/logs, get the Map-Reduce
  1465. cluster's status information and so on.</p>
  1466. <p>The job submission process involves:</p>
  1467. <ol>
  1468. <li>Checking the input and output specifications of the job.</li>
  1469. <li>Computing the <span class="codefrag">InputSplit</span> values for the job.</li>
  1470. <li>
  1471. Setting up the requisite accounting information for the
  1472. <span class="codefrag">DistributedCache</span> of the job, if necessary.
  1473. </li>
  1474. <li>
  1475. Copying the job's jar and configuration to the map-reduce system
  1476. directory on the <span class="codefrag">FileSystem</span>.
  1477. </li>
  1478. <li>
  1479. Submitting the job to the <span class="codefrag">JobTracker</span> and optionally
  1480. monitoring it's status.
  1481. </li>
  1482. </ol>
  1483. <p> Job history files are also logged to user specified directory
  1484. <span class="codefrag">hadoop.job.history.user.location</span>
  1485. which defaults to job output directory. The files are stored in
  1486. "_logs/history/" in the specified directory. Hence, by default they
  1487. will be in mapred.output.dir/_logs/history. User can stop
  1488. logging by giving the value <span class="codefrag">none</span> for
  1489. <span class="codefrag">hadoop.job.history.user.location</span>
  1490. </p>
  1491. <p> User can view the history logs summary in specified directory
  1492. using the following command <br>
  1493. <span class="codefrag">$ bin/hadoop job -history output-dir</span>
  1494. <br>
  1495. This command will print job details, failed and killed tip
  1496. details. <br>
  1497. More details about the job such as successful tasks and
  1498. task attempts made for each task can be viewed using the
  1499. following command <br>
  1500. <span class="codefrag">$ bin/hadoop job -history all output-dir</span>
  1501. <br>
  1502. </p>
  1503. <p> User can use
  1504. <a href="api/org/apache/hadoop/mapred/OutputLogFilter.html">OutputLogFilter</a>
  1505. to filter log files from the output directory listing. </p>
  1506. <p>Normally the user creates the application, describes various facets
  1507. of the job via <span class="codefrag">JobConf</span>, and then uses the
  1508. <span class="codefrag">JobClient</span> to submit the job and monitor its progress.</p>
  1509. <a name="N10A48"></a><a name="Job+Control"></a>
  1510. <h4>Job Control</h4>
  1511. <p>Users may need to chain map-reduce jobs to accomplish complex
  1512. tasks which cannot be done via a single map-reduce job. This is fairly
  1513. easy since the output of the job typically goes to distributed
  1514. file-system, and the output, in turn, can be used as the input for the
  1515. next job.</p>
  1516. <p>However, this also means that the onus on ensuring jobs are
  1517. complete (success/failure) lies squarely on the clients. In such
  1518. cases, the various job-control options are:</p>
  1519. <ul>
  1520. <li>
  1521. <a href="api/org/apache/hadoop/mapred/JobClient.html#runJob(org.apache.hadoop.mapred.JobConf)">
  1522. runJob(JobConf)</a> : Submits the job and returns only after the
  1523. job has completed.
  1524. </li>
  1525. <li>
  1526. <a href="api/org/apache/hadoop/mapred/JobClient.html#submitJob(org.apache.hadoop.mapred.JobConf)">
  1527. submitJob(JobConf)</a> : Only submits the job, then poll the
  1528. returned handle to the
  1529. <a href="api/org/apache/hadoop/mapred/RunningJob.html">
  1530. RunningJob</a> to query status and make scheduling decisions.
  1531. </li>
  1532. <li>
  1533. <a href="api/org/apache/hadoop/mapred/JobConf.html#setJobEndNotificationURI(java.lang.String)">
  1534. JobConf.setJobEndNotificationURI(String)</a> : Sets up a
  1535. notification upon job-completion, thus avoiding polling.
  1536. </li>
  1537. </ul>
  1538. <a name="N10A72"></a><a name="Job+Input"></a>
  1539. <h3 class="h4">Job Input</h3>
  1540. <p>
  1541. <a href="api/org/apache/hadoop/mapred/InputFormat.html">
  1542. InputFormat</a> describes the input-specification for a Map-Reduce job.
  1543. </p>
  1544. <p>The Map-Reduce framework relies on the <span class="codefrag">InputFormat</span> of
  1545. the job to:</p>
  1546. <ol>
  1547. <li>Validate the input-specification of the job.</li>
  1548. <li>
  1549. Split-up the input file(s) into logical <span class="codefrag">InputSplit</span>
  1550. instances, each of which is then assigned to an individual
  1551. <span class="codefrag">Mapper</span>.
  1552. </li>
  1553. <li>
  1554. Provide the <span class="codefrag">RecordReader</span> implementation used to
  1555. glean input records from the logical <span class="codefrag">InputSplit</span> for
  1556. processing by the <span class="codefrag">Mapper</span>.
  1557. </li>
  1558. </ol>
  1559. <p>The default behavior of file-based <span class="codefrag">InputFormat</span>
  1560. implementations, typically sub-classes of
  1561. <a href="api/org/apache/hadoop/mapred/FileInputFormat.html">
  1562. FileInputFormat</a>, is to split the input into <em>logical</em>
  1563. <span class="codefrag">InputSplit</span> instances based on the total size, in bytes, of
  1564. the input files. However, the <span class="codefrag">FileSystem</span> blocksize of the
  1565. input files is treated as an upper bound for input splits. A lower bound
  1566. on the split size can be set via <span class="codefrag">mapred.min.split.size</span>.</p>
  1567. <p>Clearly, logical splits based on input-size is insufficient for many
  1568. applications since record boundaries must be respected. In such cases,
  1569. the application should implement a <span class="codefrag">RecordReader</span>, who is
  1570. responsible for respecting record-boundaries and presents a
  1571. record-oriented view of the logical <span class="codefrag">InputSplit</span> to the
  1572. individual task.</p>
  1573. <p>
  1574. <a href="api/org/apache/hadoop/mapred/TextInputFormat.html">
  1575. TextInputFormat</a> is the default <span class="codefrag">InputFormat</span>.</p>
  1576. <p>If <span class="codefrag">TextInputFormat</span> is the <span class="codefrag">InputFormat</span> for a
  1577. given job, the framework detects input-files with the <em>.gz</em> and
  1578. <em>.lzo</em> extensions and automatically decompresses them using the
  1579. appropriate <span class="codefrag">CompressionCodec</span>. However, it must be noted that
  1580. compressed files with the above extensions cannot be <em>split</em> and
  1581. each compressed file is processed in its entirety by a single mapper.</p>
  1582. <a name="N10ADC"></a><a name="InputSplit"></a>
  1583. <h4>InputSplit</h4>
  1584. <p>
  1585. <a href="api/org/apache/hadoop/mapred/InputSplit.html">
  1586. InputSplit</a> represents the data to be processed by an individual
  1587. <span class="codefrag">Mapper</span>.</p>
  1588. <p>Typically <span class="codefrag">InputSplit</span> presents a byte-oriented view of
  1589. the input, and it is the responsibility of <span class="codefrag">RecordReader</span>
  1590. to process and present a record-oriented view.</p>
  1591. <p>
  1592. <a href="api/org/apache/hadoop/mapred/FileSplit.html">
  1593. FileSplit</a> is the default <span class="codefrag">InputSplit</span>. It sets
  1594. <span class="codefrag">map.input.file</span> to the path of the input file for the
  1595. logical split.</p>
  1596. <a name="N10B01"></a><a name="RecordReader"></a>
  1597. <h4>RecordReader</h4>
  1598. <p>
  1599. <a href="api/org/apache/hadoop/mapred/RecordReader.html">
  1600. RecordReader</a> reads <span class="codefrag">&lt;key, value&gt;</span> pairs from an
  1601. <span class="codefrag">InputSplit</span>.</p>
  1602. <p>Typically the <span class="codefrag">RecordReader</span> converts the byte-oriented
  1603. view of the input, provided by the <span class="codefrag">InputSplit</span>, and
  1604. presents a record-oriented to the <span class="codefrag">Mapper</span> implementations
  1605. for processing. <span class="codefrag">RecordReader</span> thus assumes the
  1606. responsibility of processing record boundaries and presents the tasks
  1607. with keys and values.</p>
  1608. <a name="N10B24"></a><a name="Job+Output"></a>
  1609. <h3 class="h4">Job Output</h3>
  1610. <p>
  1611. <a href="api/org/apache/hadoop/mapred/OutputFormat.html">
  1612. OutputFormat</a> describes the output-specification for a Map-Reduce
  1613. job.</p>
  1614. <p>The Map-Reduce framework relies on the <span class="codefrag">OutputFormat</span> of
  1615. the job to:</p>
  1616. <ol>
  1617. <li>
  1618. Validate the output-specification of the job; for example, check that
  1619. the output directory doesn't already exist.
  1620. </li>
  1621. <li>
  1622. Provide the <span class="codefrag">RecordWriter</span> implementation used to
  1623. write the output files of the job. Output files are stored in a
  1624. <span class="codefrag">FileSystem</span>.
  1625. </li>
  1626. </ol>
  1627. <p>
  1628. <span class="codefrag">TextOutputFormat</span> is the default
  1629. <span class="codefrag">OutputFormat</span>.</p>
  1630. <a name="N10B4D"></a><a name="Task+Side-Effect+Files"></a>
  1631. <h4>Task Side-Effect Files</h4>
  1632. <p>In some applications, component tasks need to create and/or write to
  1633. side-files, which differ from the actual job-output files.</p>
  1634. <p>In such cases there could be issues with two instances of the same
  1635. <span class="codefrag">Mapper</span> or <span class="codefrag">Reducer</span> running simultaneously (for
  1636. example, speculative tasks) trying to open and/or write to the same
  1637. file (path) on the <span class="codefrag">FileSystem</span>. Hence the
  1638. application-writer will have to pick unique names per task-attempt
  1639. (using the attemptid, say <span class="codefrag">attempt_200709221812_0001_m_000000_0</span>),
  1640. not just per task.</p>
  1641. <p>To avoid these issues the Map-Reduce framework maintains a special
  1642. <span class="codefrag">${mapred.output.dir}/_temporary/_${taskid}</span> sub-directory
  1643. accessible via <span class="codefrag">${mapred.work.output.dir}</span>
  1644. for each task-attempt on the <span class="codefrag">FileSystem</span> where the output
  1645. of the task-attempt is stored. On successful completion of the
  1646. task-attempt, the files in the
  1647. <span class="codefrag">${mapred.output.dir}/_temporary/_${taskid}</span> (only)
  1648. are <em>promoted</em> to <span class="codefrag">${mapred.output.dir}</span>. Of course,
  1649. the framework discards the sub-directory of unsuccessful task-attempts.
  1650. This process is completely transparent to the application.</p>
  1651. <p>The application-writer can take advantage of this feature by
  1652. creating any side-files required in <span class="codefrag">${mapred.work.output.dir}</span>
  1653. during execution of a task via
  1654. <a href="api/org/apache/hadoop/mapred/FileOutputFormat.html#getWorkOutputPath(org.apache.hadoop.mapred.JobConf)">
  1655. FileOutputFormat.getWorkOutputPath()</a>, and the framework will promote them
  1656. similarly for succesful task-attempts, thus eliminating the need to
  1657. pick unique paths per task-attempt.</p>
  1658. <p>Note: The value of <span class="codefrag">${mapred.work.output.dir}</span> during
  1659. execution of a particular task-attempt is actually
  1660. <span class="codefrag">${mapred.output.dir}/_temporary/_{$taskid}</span>, and this value is
  1661. set by the map-reduce framework. So, just create any side-files in the
  1662. path returned by
  1663. <a href="api/org/apache/hadoop/mapred/FileOutputFormat.html#getWorkOutputPath(org.apache.hadoop.mapred.JobConf)">
  1664. FileOutputFormat.getWorkOutputPath() </a>from map/reduce
  1665. task to take advantage of this feature.</p>
  1666. <p>The entire discussion holds true for maps of jobs with
  1667. reducer=NONE (i.e. 0 reduces) since output of the map, in that case,
  1668. goes directly to HDFS.</p>
  1669. <a name="N10B95"></a><a name="RecordWriter"></a>
  1670. <h4>RecordWriter</h4>
  1671. <p>
  1672. <a href="api/org/apache/hadoop/mapred/RecordWriter.html">
  1673. RecordWriter</a> writes the output <span class="codefrag">&lt;key, value&gt;</span>
  1674. pairs to an output file.</p>
  1675. <p>RecordWriter implementations write the job outputs to the
  1676. <span class="codefrag">FileSystem</span>.</p>
  1677. <a name="N10BAC"></a><a name="Other+Useful+Features"></a>
  1678. <h3 class="h4">Other Useful Features</h3>
  1679. <a name="N10BB2"></a><a name="Counters"></a>
  1680. <h4>Counters</h4>
  1681. <p>
  1682. <span class="codefrag">Counters</span> represent global counters, defined either by
  1683. the Map-Reduce framework or applications. Each <span class="codefrag">Counter</span> can
  1684. be of any <span class="codefrag">Enum</span> type. Counters of a particular
  1685. <span class="codefrag">Enum</span> are bunched into groups of type
  1686. <span class="codefrag">Counters.Group</span>.</p>
  1687. <p>Applications can define arbitrary <span class="codefrag">Counters</span> (of type
  1688. <span class="codefrag">Enum</span>) and update them via
  1689. <a href="api/org/apache/hadoop/mapred/Reporter.html#incrCounter(java.lang.Enum, long)">
  1690. Reporter.incrCounter(Enum, long)</a> or
  1691. <a href="api/org/apache/hadoop/mapred/Reporter.html#incrCounter(java.lang.String, java.lang.String, long amount)">
  1692. Reporter.incrCounter(String, String, long)</a>
  1693. in the <span class="codefrag">map</span> and/or
  1694. <span class="codefrag">reduce</span> methods. These counters are then globally
  1695. aggregated by the framework.</p>
  1696. <a name="N10BE1"></a><a name="DistributedCache"></a>
  1697. <h4>DistributedCache</h4>
  1698. <p>
  1699. <a href="api/org/apache/hadoop/filecache/DistributedCache.html">
  1700. DistributedCache</a> distributes application-specific, large, read-only
  1701. files efficiently.</p>
  1702. <p>
  1703. <span class="codefrag">DistributedCache</span> is a facility provided by the
  1704. Map-Reduce framework to cache files (text, archives, jars and so on)
  1705. needed by applications.</p>
  1706. <p>Applications specify the files to be cached via urls (hdfs://)
  1707. in the <span class="codefrag">JobConf</span>. The <span class="codefrag">DistributedCache</span>
  1708. assumes that the files specified via hdfs:// urls are already present
  1709. on the <span class="codefrag">FileSystem</span>.</p>
  1710. <p>The framework will copy the necessary files to the slave node
  1711. before any tasks for the job are executed on that node. Its
  1712. efficiency stems from the fact that the files are only copied once
  1713. per job and the ability to cache archives which are un-archived on
  1714. the slaves.</p>
  1715. <p>
  1716. <span class="codefrag">DistributedCache</span> tracks the modification timestamps of
  1717. the cached files. Clearly the cache files should not be modified by
  1718. the application or externally while the job is executing.</p>
  1719. <p>
  1720. <span class="codefrag">DistributedCache</span> can be used to distribute simple,
  1721. read-only data/text files and more complex types such as archives and
  1722. jars. Archives (zip, tar, tgz and tar.gz files) are
  1723. <em>un-archived</em> at the slave nodes. Files
  1724. have <em>execution permissions</em> set. </p>
  1725. <p>The files/archives can be distributed by setting the property
  1726. <span class="codefrag">mapred.cache.{files|archives}</span>. If more than one
  1727. file/archive has to be distributed, they can be added as comma
  1728. separated paths. The properties can also be set by APIs
  1729. <a href="api/org/apache/hadoop/filecache/DistributedCache.html#addCacheFile(java.net.URI,%20org.apache.hadoop.conf.Configuration)">
  1730. DistributedCache.addCacheFile(URI,conf)</a>/
  1731. <a href="api/org/apache/hadoop/filecache/DistributedCache.html#addCacheArchive(java.net.URI,%20org.apache.hadoop.conf.Configuration)">
  1732. DistributedCache.addCacheArchive(URI,conf)</a> and
  1733. <a href="api/org/apache/hadoop/filecache/DistributedCache.html#setCacheFiles(java.net.URI[],%20org.apache.hadoop.conf.Configuration)">
  1734. DistributedCache.setCacheFiles(URIs,conf)</a>/
  1735. <a href="api/org/apache/hadoop/filecache/DistributedCache.html#setCacheArchives(java.net.URI[],%20org.apache.hadoop.conf.Configuration)">
  1736. DistributedCache.setCacheArchives(URIs,conf)</a>
  1737. where URI is of the form
  1738. <span class="codefrag">hdfs://host:port/absolute-path#link-name</span>.
  1739. In Streaming, the files can be distributed through command line
  1740. option <span class="codefrag">-cacheFile/-cacheArchive</span>.</p>
  1741. <p>Optionally users can also direct the <span class="codefrag">DistributedCache</span>
  1742. to <em>symlink</em> the cached file(s) into the <span class="codefrag">current working
  1743. directory</span> of the task via the
  1744. <a href="api/org/apache/hadoop/filecache/DistributedCache.html#createSymlink(org.apache.hadoop.conf.Configuration)">
  1745. DistributedCache.createSymlink(Configuration)</a> api. Or by setting
  1746. the configuration property <span class="codefrag">mapred.create.symlink</span>
  1747. as <span class="codefrag">yes</span>. The DistributedCache will use the
  1748. <span class="codefrag">fragment</span> of the URI as the name of the symlink.
  1749. For example, the URI
  1750. <span class="codefrag">hdfs://namenode:port/lib.so.1#lib.so</span>
  1751. will have the symlink name as <span class="codefrag">lib.so</span> in task's cwd
  1752. for the file <span class="codefrag">lib.so.1</span> in distributed cache.</p>
  1753. <p>The <span class="codefrag">DistributedCache</span> can also be used as a
  1754. rudimentary software distribution mechanism for use in the
  1755. map and/or reduce tasks. It can be used to distribute both
  1756. jars and native libraries. The
  1757. <a href="api/org/apache/hadoop/filecache/DistributedCache.html#addArchiveToClassPath(org.apache.hadoop.fs.Path,%20org.apache.hadoop.conf.Configuration)">
  1758. DistributedCache.addArchiveToClassPath(Path, Configuration)</a> or
  1759. <a href="api/org/apache/hadoop/filecache/DistributedCache.html#addFileToClassPath(org.apache.hadoop.fs.Path,%20org.apache.hadoop.conf.Configuration)">
  1760. DistributedCache.addFileToClassPath(Path, Configuration)</a> api
  1761. can be used to cache files/jars and also add them to the
  1762. <em>classpath</em> of child-jvm. The same can be done by setting
  1763. the configuration properties
  1764. <span class="codefrag">mapred.job.classpath.{files|archives}</span>. Similarly the
  1765. cached files that are symlinked into the working directory of the
  1766. task can be used to distribute native libraries and load them.</p>
  1767. <a name="N10C64"></a><a name="Tool"></a>
  1768. <h4>Tool</h4>
  1769. <p>The <a href="api/org/apache/hadoop/util/Tool.html">Tool</a>
  1770. interface supports the handling of generic Hadoop command-line options.
  1771. </p>
  1772. <p>
  1773. <span class="codefrag">Tool</span> is the standard for any Map-Reduce tool or
  1774. application. The application should delegate the handling of
  1775. standard command-line options to
  1776. <a href="api/org/apache/hadoop/util/GenericOptionsParser.html">
  1777. GenericOptionsParser</a> via
  1778. <a href="api/org/apache/hadoop/util/ToolRunner.html#run(org.apache.hadoop.util.Tool, java.lang.String[])">
  1779. ToolRunner.run(Tool, String[])</a> and only handle its custom
  1780. arguments.</p>
  1781. <p>
  1782. The generic Hadoop command-line options are:<br>
  1783. <span class="codefrag">
  1784. -conf &lt;configuration file&gt;
  1785. </span>
  1786. <br>
  1787. <span class="codefrag">
  1788. -D &lt;property=value&gt;
  1789. </span>
  1790. <br>
  1791. <span class="codefrag">
  1792. -fs &lt;local|namenode:port&gt;
  1793. </span>
  1794. <br>
  1795. <span class="codefrag">
  1796. -jt &lt;local|jobtracker:port&gt;
  1797. </span>
  1798. </p>
  1799. <a name="N10C96"></a><a name="IsolationRunner"></a>
  1800. <h4>IsolationRunner</h4>
  1801. <p>
  1802. <a href="api/org/apache/hadoop/mapred/IsolationRunner.html">
  1803. IsolationRunner</a> is a utility to help debug Map-Reduce programs.</p>
  1804. <p>To use the <span class="codefrag">IsolationRunner</span>, first set
  1805. <span class="codefrag">keep.failed.tasks.files</span> to <span class="codefrag">true</span>
  1806. (also see <span class="codefrag">keep.tasks.files.pattern</span>).</p>
  1807. <p>
  1808. Next, go to the node on which the failed task ran and go to the
  1809. <span class="codefrag">TaskTracker</span>'s local directory and run the
  1810. <span class="codefrag">IsolationRunner</span>:<br>
  1811. <span class="codefrag">$ cd &lt;local path&gt;/taskTracker/${taskid}/work</span>
  1812. <br>
  1813. <span class="codefrag">
  1814. $ bin/hadoop org.apache.hadoop.mapred.IsolationRunner ../job.xml
  1815. </span>
  1816. </p>
  1817. <p>
  1818. <span class="codefrag">IsolationRunner</span> will run the failed task in a single
  1819. jvm, which can be in the debugger, over precisely the same input.</p>
  1820. <a name="N10CC9"></a><a name="Profiling"></a>
  1821. <h4>Profiling</h4>
  1822. <p>Profiling is a utility to get a representative (2 or 3) sample
  1823. of built-in java profiler for a sample of maps and reduces. </p>
  1824. <p>User can specify whether the system should collect profiler
  1825. information for some of the tasks in the job by setting the
  1826. configuration property <span class="codefrag">mapred.task.profile</span>. The
  1827. value can be set using the api
  1828. <a href="api/org/apache/hadoop/mapred/JobConf.html#setProfileEnabled(boolean)">
  1829. JobConf.setProfileEnabled(boolean)</a>. If the value is set
  1830. <span class="codefrag">true</span>, the task profiling is enabled. The profiler
  1831. information is stored in the the user log directory. By default,
  1832. profiling is not enabled for the job. </p>
  1833. <p>Once user configures that profiling is needed, she/he can use
  1834. the configuration property
  1835. <span class="codefrag">mapred.task.profile.{maps|reduces}</span> to set the ranges
  1836. of map/reduce tasks to profile. The value can be set using the api
  1837. <a href="api/org/apache/hadoop/mapred/JobConf.html#setProfileTaskRange(boolean,%20java.lang.String)">
  1838. JobConf.setProfileTaskRange(boolean,String)</a>.
  1839. By default, the specified range is <span class="codefrag">0-2</span>.</p>
  1840. <p>User can also specify the profiler configuration arguments by
  1841. setting the configuration property
  1842. <span class="codefrag">mapred.task.profile.params</span>. The value can be specified
  1843. using the api
  1844. <a href="api/org/apache/hadoop/mapred/JobConf.html#setProfileParams(java.lang.String)">
  1845. JobConf.setProfileParams(String)</a>. If the string contains a
  1846. <span class="codefrag">%s</span>, it will be replaced with the name of the profiling
  1847. output file when the task runs. These parameters are passed to the
  1848. task child JVM on the command line. The default value for
  1849. the profiling parameters is
  1850. <span class="codefrag">-agentlib:hprof=cpu=samples,heap=sites,force=n,thread=y,verbose=n,file=%s</span>
  1851. </p>
  1852. <a name="N10CFD"></a><a name="Debugging"></a>
  1853. <h4>Debugging</h4>
  1854. <p>Map/Reduce framework provides a facility to run user-provided
  1855. scripts for debugging. When map/reduce task fails, user can run
  1856. script for doing post-processing on task logs i.e task's stdout,
  1857. stderr, syslog and jobconf. The stdout and stderr of the
  1858. user-provided debug script are printed on the diagnostics.
  1859. These outputs are also displayed on job UI on demand. </p>
  1860. <p> In the following sections we discuss how to submit debug script
  1861. along with the job. For submitting debug script, first it has to
  1862. distributed. Then the script has to supplied in Configuration. </p>
  1863. <a name="N10D09"></a><a name="How+to+distribute+script+file%3A"></a>
  1864. <h5> How to distribute script file: </h5>
  1865. <p>
  1866. The user has to use
  1867. <a href="mapred_tutorial.html#DistributedCache">DistributedCache</a>
  1868. mechanism to <em>distribute</em> and <em>symlink</em> the
  1869. debug script file.</p>
  1870. <a name="N10D1D"></a><a name="How+to+submit+script%3A"></a>
  1871. <h5> How to submit script: </h5>
  1872. <p> A quick way to submit debug script is to set values for the
  1873. properties "mapred.map.task.debug.script" and
  1874. "mapred.reduce.task.debug.script" for debugging map task and reduce
  1875. task respectively. These properties can also be set by using APIs
  1876. <a href="api/org/apache/hadoop/mapred/JobConf.html#setMapDebugScript(java.lang.String)">
  1877. JobConf.setMapDebugScript(String) </a> and
  1878. <a href="api/org/apache/hadoop/mapred/JobConf.html#setReduceDebugScript(java.lang.String)">
  1879. JobConf.setReduceDebugScript(String) </a>. For streaming, debug
  1880. script can be submitted with command-line options -mapdebug,
  1881. -reducedebug for debugging mapper and reducer respectively.</p>
  1882. <p>The arguments of the script are task's stdout, stderr,
  1883. syslog and jobconf files. The debug command, run on the node where
  1884. the map/reduce failed, is: <br>
  1885. <span class="codefrag"> $script $stdout $stderr $syslog $jobconf </span>
  1886. </p>
  1887. <p> Pipes programs have the c++ program name as a fifth argument
  1888. for the command. Thus for the pipes programs the command is <br>
  1889. <span class="codefrag">$script $stdout $stderr $syslog $jobconf $program </span>
  1890. </p>
  1891. <a name="N10D3F"></a><a name="Default+Behavior%3A"></a>
  1892. <h5> Default Behavior: </h5>
  1893. <p> For pipes, a default script is run to process core dumps under
  1894. gdb, prints stack trace and gives info about running threads. </p>
  1895. <a name="N10D4A"></a><a name="JobControl"></a>
  1896. <h4>JobControl</h4>
  1897. <p>
  1898. <a href="api/org/apache/hadoop/mapred/jobcontrol/package-summary.html">
  1899. JobControl</a> is a utility which encapsulates a set of Map-Reduce jobs
  1900. and their dependencies.</p>
  1901. <a name="N10D57"></a><a name="Data+Compression"></a>
  1902. <h4>Data Compression</h4>
  1903. <p>Hadoop Map-Reduce provides facilities for the application-writer to
  1904. specify compression for both intermediate map-outputs and the
  1905. job-outputs i.e. output of the reduces. It also comes bundled with
  1906. <a href="api/org/apache/hadoop/io/compress/CompressionCodec.html">
  1907. CompressionCodec</a> implementations for the
  1908. <a href="http://www.zlib.net/">zlib</a> and <a href="http://www.oberhumer.com/opensource/lzo/">lzo</a> compression
  1909. algorithms. The <a href="http://www.gzip.org/">gzip</a> file format is also
  1910. supported.</p>
  1911. <p>Hadoop also provides native implementations of the above compression
  1912. codecs for reasons of both performance (zlib) and non-availability of
  1913. Java libraries (lzo). More details on their usage and availability are
  1914. available <a href="native_libraries.html">here</a>.</p>
  1915. <a name="N10D77"></a><a name="Intermediate+Outputs"></a>
  1916. <h5>Intermediate Outputs</h5>
  1917. <p>Applications can control compression of intermediate map-outputs
  1918. via the
  1919. <a href="api/org/apache/hadoop/mapred/JobConf.html#setCompressMapOutput(boolean)">
  1920. JobConf.setCompressMapOutput(boolean)</a> api and the
  1921. <span class="codefrag">CompressionCodec</span> to be used via the
  1922. <a href="api/org/apache/hadoop/mapred/JobConf.html#setMapOutputCompressorClass(java.lang.Class)">
  1923. JobConf.setMapOutputCompressorClass(Class)</a> api.</p>
  1924. <a name="N10D8C"></a><a name="Job+Outputs"></a>
  1925. <h5>Job Outputs</h5>
  1926. <p>Applications can control compression of job-outputs via the
  1927. <a href="api/org/apache/hadoop/mapred/FileOutputFormat.html#setCompressOutput(org.apache.hadoop.mapred.JobConf,%20boolean)">
  1928. FileOutputFormat.setCompressOutput(JobConf, boolean)</a> api and the
  1929. <span class="codefrag">CompressionCodec</span> to be used can be specified via the
  1930. <a href="api/org/apache/hadoop/mapred/FileOutputFormat.html#setOutputCompressorClass(org.apache.hadoop.mapred.JobConf,%20java.lang.Class)">
  1931. FileOutputFormat.setOutputCompressorClass(JobConf, Class)</a> api.</p>
  1932. <p>If the job outputs are to be stored in the
  1933. <a href="api/org/apache/hadoop/mapred/SequenceFileOutputFormat.html">
  1934. SequenceFileOutputFormat</a>, the required
  1935. <span class="codefrag">SequenceFile.CompressionType</span> (i.e. <span class="codefrag">RECORD</span> /
  1936. <span class="codefrag">BLOCK</span> - defaults to <span class="codefrag">RECORD</span>) can be
  1937. specified via the
  1938. <a href="api/org/apache/hadoop/mapred/SequenceFileOutputFormat.html#setOutputCompressionType(org.apache.hadoop.mapred.JobConf,%20org.apache.hadoop.io.SequenceFile.CompressionType)">
  1939. SequenceFileOutputFormat.setOutputCompressionType(JobConf,
  1940. SequenceFile.CompressionType)</a> api.</p>
  1941. </div>
  1942. <a name="N10DBB"></a><a name="Example%3A+WordCount+v2.0"></a>
  1943. <h2 class="h3">Example: WordCount v2.0</h2>
  1944. <div class="section">
  1945. <p>Here is a more complete <span class="codefrag">WordCount</span> which uses many of the
  1946. features provided by the Map-Reduce framework we discussed so far.</p>
  1947. <p>This needs the HDFS to be up and running, especially for the
  1948. <span class="codefrag">DistributedCache</span>-related features. Hence it only works with a
  1949. <a href="quickstart.html#SingleNodeSetup">pseudo-distributed</a> or
  1950. <a href="quickstart.html#Fully-Distributed+Operation">fully-distributed</a>
  1951. Hadoop installation.</p>
  1952. <a name="N10DD5"></a><a name="Source+Code-N10DD5"></a>
  1953. <h3 class="h4">Source Code</h3>
  1954. <table class="ForrestTable" cellspacing="1" cellpadding="4">
  1955. <tr>
  1956. <th colspan="1" rowspan="1"></th>
  1957. <th colspan="1" rowspan="1">WordCount.java</th>
  1958. </tr>
  1959. <tr>
  1960. <td colspan="1" rowspan="1">1.</td>
  1961. <td colspan="1" rowspan="1">
  1962. <span class="codefrag">package org.myorg;</span>
  1963. </td>
  1964. </tr>
  1965. <tr>
  1966. <td colspan="1" rowspan="1">2.</td>
  1967. <td colspan="1" rowspan="1"></td>
  1968. </tr>
  1969. <tr>
  1970. <td colspan="1" rowspan="1">3.</td>
  1971. <td colspan="1" rowspan="1">
  1972. <span class="codefrag">import java.io.*;</span>
  1973. </td>
  1974. </tr>
  1975. <tr>
  1976. <td colspan="1" rowspan="1">4.</td>
  1977. <td colspan="1" rowspan="1">
  1978. <span class="codefrag">import java.util.*;</span>
  1979. </td>
  1980. </tr>
  1981. <tr>
  1982. <td colspan="1" rowspan="1">5.</td>
  1983. <td colspan="1" rowspan="1"></td>
  1984. </tr>
  1985. <tr>
  1986. <td colspan="1" rowspan="1">6.</td>
  1987. <td colspan="1" rowspan="1">
  1988. <span class="codefrag">import org.apache.hadoop.fs.Path;</span>
  1989. </td>
  1990. </tr>
  1991. <tr>
  1992. <td colspan="1" rowspan="1">7.</td>
  1993. <td colspan="1" rowspan="1">
  1994. <span class="codefrag">import org.apache.hadoop.filecache.DistributedCache;</span>
  1995. </td>
  1996. </tr>
  1997. <tr>
  1998. <td colspan="1" rowspan="1">8.</td>
  1999. <td colspan="1" rowspan="1">
  2000. <span class="codefrag">import org.apache.hadoop.conf.*;</span>
  2001. </td>
  2002. </tr>
  2003. <tr>
  2004. <td colspan="1" rowspan="1">9.</td>
  2005. <td colspan="1" rowspan="1">
  2006. <span class="codefrag">import org.apache.hadoop.io.*;</span>
  2007. </td>
  2008. </tr>
  2009. <tr>
  2010. <td colspan="1" rowspan="1">10.</td>
  2011. <td colspan="1" rowspan="1">
  2012. <span class="codefrag">import org.apache.hadoop.mapred.*;</span>
  2013. </td>
  2014. </tr>
  2015. <tr>
  2016. <td colspan="1" rowspan="1">11.</td>
  2017. <td colspan="1" rowspan="1">
  2018. <span class="codefrag">import org.apache.hadoop.util.*;</span>
  2019. </td>
  2020. </tr>
  2021. <tr>
  2022. <td colspan="1" rowspan="1">12.</td>
  2023. <td colspan="1" rowspan="1"></td>
  2024. </tr>
  2025. <tr>
  2026. <td colspan="1" rowspan="1">13.</td>
  2027. <td colspan="1" rowspan="1">
  2028. <span class="codefrag">public class WordCount extends Configured implements Tool {</span>
  2029. </td>
  2030. </tr>
  2031. <tr>
  2032. <td colspan="1" rowspan="1">14.</td>
  2033. <td colspan="1" rowspan="1"></td>
  2034. </tr>
  2035. <tr>
  2036. <td colspan="1" rowspan="1">15.</td>
  2037. <td colspan="1" rowspan="1">
  2038. &nbsp;&nbsp;
  2039. <span class="codefrag">
  2040. public static class Map extends MapReduceBase
  2041. implements Mapper&lt;LongWritable, Text, Text, IntWritable&gt; {
  2042. </span>
  2043. </td>
  2044. </tr>
  2045. <tr>
  2046. <td colspan="1" rowspan="1">16.</td>
  2047. <td colspan="1" rowspan="1"></td>
  2048. </tr>
  2049. <tr>
  2050. <td colspan="1" rowspan="1">17.</td>
  2051. <td colspan="1" rowspan="1">
  2052. &nbsp;&nbsp;&nbsp;&nbsp;
  2053. <span class="codefrag">
  2054. static enum Counters { INPUT_WORDS }
  2055. </span>
  2056. </td>
  2057. </tr>
  2058. <tr>
  2059. <td colspan="1" rowspan="1">18.</td>
  2060. <td colspan="1" rowspan="1"></td>
  2061. </tr>
  2062. <tr>
  2063. <td colspan="1" rowspan="1">19.</td>
  2064. <td colspan="1" rowspan="1">
  2065. &nbsp;&nbsp;&nbsp;&nbsp;
  2066. <span class="codefrag">
  2067. private final static IntWritable one = new IntWritable(1);
  2068. </span>
  2069. </td>
  2070. </tr>
  2071. <tr>
  2072. <td colspan="1" rowspan="1">20.</td>
  2073. <td colspan="1" rowspan="1">
  2074. &nbsp;&nbsp;&nbsp;&nbsp;
  2075. <span class="codefrag">private Text word = new Text();</span>
  2076. </td>
  2077. </tr>
  2078. <tr>
  2079. <td colspan="1" rowspan="1">21.</td>
  2080. <td colspan="1" rowspan="1"></td>
  2081. </tr>
  2082. <tr>
  2083. <td colspan="1" rowspan="1">22.</td>
  2084. <td colspan="1" rowspan="1">
  2085. &nbsp;&nbsp;&nbsp;&nbsp;
  2086. <span class="codefrag">private boolean caseSensitive = true;</span>
  2087. </td>
  2088. </tr>
  2089. <tr>
  2090. <td colspan="1" rowspan="1">23.</td>
  2091. <td colspan="1" rowspan="1">
  2092. &nbsp;&nbsp;&nbsp;&nbsp;
  2093. <span class="codefrag">private Set&lt;String&gt; patternsToSkip = new HashSet&lt;String&gt;();</span>
  2094. </td>
  2095. </tr>
  2096. <tr>
  2097. <td colspan="1" rowspan="1">24.</td>
  2098. <td colspan="1" rowspan="1"></td>
  2099. </tr>
  2100. <tr>
  2101. <td colspan="1" rowspan="1">25.</td>
  2102. <td colspan="1" rowspan="1">
  2103. &nbsp;&nbsp;&nbsp;&nbsp;
  2104. <span class="codefrag">private long numRecords = 0;</span>
  2105. </td>
  2106. </tr>
  2107. <tr>
  2108. <td colspan="1" rowspan="1">26.</td>
  2109. <td colspan="1" rowspan="1">
  2110. &nbsp;&nbsp;&nbsp;&nbsp;
  2111. <span class="codefrag">private String inputFile;</span>
  2112. </td>
  2113. </tr>
  2114. <tr>
  2115. <td colspan="1" rowspan="1">27.</td>
  2116. <td colspan="1" rowspan="1"></td>
  2117. </tr>
  2118. <tr>
  2119. <td colspan="1" rowspan="1">28.</td>
  2120. <td colspan="1" rowspan="1">
  2121. &nbsp;&nbsp;&nbsp;&nbsp;
  2122. <span class="codefrag">public void configure(JobConf job) {</span>
  2123. </td>
  2124. </tr>
  2125. <tr>
  2126. <td colspan="1" rowspan="1">29.</td>
  2127. <td colspan="1" rowspan="1">
  2128. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2129. <span class="codefrag">
  2130. caseSensitive = job.getBoolean("wordcount.case.sensitive", true);
  2131. </span>
  2132. </td>
  2133. </tr>
  2134. <tr>
  2135. <td colspan="1" rowspan="1">30.</td>
  2136. <td colspan="1" rowspan="1">
  2137. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2138. <span class="codefrag">inputFile = job.get("map.input.file");</span>
  2139. </td>
  2140. </tr>
  2141. <tr>
  2142. <td colspan="1" rowspan="1">31.</td>
  2143. <td colspan="1" rowspan="1"></td>
  2144. </tr>
  2145. <tr>
  2146. <td colspan="1" rowspan="1">32.</td>
  2147. <td colspan="1" rowspan="1">
  2148. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2149. <span class="codefrag">if (job.getBoolean("wordcount.skip.patterns", false)) {</span>
  2150. </td>
  2151. </tr>
  2152. <tr>
  2153. <td colspan="1" rowspan="1">33.</td>
  2154. <td colspan="1" rowspan="1">
  2155. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2156. <span class="codefrag">Path[] patternsFiles = new Path[0];</span>
  2157. </td>
  2158. </tr>
  2159. <tr>
  2160. <td colspan="1" rowspan="1">34.</td>
  2161. <td colspan="1" rowspan="1">
  2162. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2163. <span class="codefrag">try {</span>
  2164. </td>
  2165. </tr>
  2166. <tr>
  2167. <td colspan="1" rowspan="1">35.</td>
  2168. <td colspan="1" rowspan="1">
  2169. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2170. <span class="codefrag">
  2171. patternsFiles = DistributedCache.getLocalCacheFiles(job);
  2172. </span>
  2173. </td>
  2174. </tr>
  2175. <tr>
  2176. <td colspan="1" rowspan="1">36.</td>
  2177. <td colspan="1" rowspan="1">
  2178. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2179. <span class="codefrag">} catch (IOException ioe) {</span>
  2180. </td>
  2181. </tr>
  2182. <tr>
  2183. <td colspan="1" rowspan="1">37.</td>
  2184. <td colspan="1" rowspan="1">
  2185. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2186. <span class="codefrag">
  2187. System.err.println("Caught exception while getting cached files: "
  2188. + StringUtils.stringifyException(ioe));
  2189. </span>
  2190. </td>
  2191. </tr>
  2192. <tr>
  2193. <td colspan="1" rowspan="1">38.</td>
  2194. <td colspan="1" rowspan="1">
  2195. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2196. <span class="codefrag">}</span>
  2197. </td>
  2198. </tr>
  2199. <tr>
  2200. <td colspan="1" rowspan="1">39.</td>
  2201. <td colspan="1" rowspan="1">
  2202. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2203. <span class="codefrag">for (Path patternsFile : patternsFiles) {</span>
  2204. </td>
  2205. </tr>
  2206. <tr>
  2207. <td colspan="1" rowspan="1">40.</td>
  2208. <td colspan="1" rowspan="1">
  2209. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2210. <span class="codefrag">parseSkipFile(patternsFile);</span>
  2211. </td>
  2212. </tr>
  2213. <tr>
  2214. <td colspan="1" rowspan="1">41.</td>
  2215. <td colspan="1" rowspan="1">
  2216. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2217. <span class="codefrag">}</span>
  2218. </td>
  2219. </tr>
  2220. <tr>
  2221. <td colspan="1" rowspan="1">42.</td>
  2222. <td colspan="1" rowspan="1">
  2223. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2224. <span class="codefrag">}</span>
  2225. </td>
  2226. </tr>
  2227. <tr>
  2228. <td colspan="1" rowspan="1">43.</td>
  2229. <td colspan="1" rowspan="1">
  2230. &nbsp;&nbsp;&nbsp;&nbsp;
  2231. <span class="codefrag">}</span>
  2232. </td>
  2233. </tr>
  2234. <tr>
  2235. <td colspan="1" rowspan="1">44.</td>
  2236. <td colspan="1" rowspan="1"></td>
  2237. </tr>
  2238. <tr>
  2239. <td colspan="1" rowspan="1">45.</td>
  2240. <td colspan="1" rowspan="1">
  2241. &nbsp;&nbsp;&nbsp;&nbsp;
  2242. <span class="codefrag">private void parseSkipFile(Path patternsFile) {</span>
  2243. </td>
  2244. </tr>
  2245. <tr>
  2246. <td colspan="1" rowspan="1">46.</td>
  2247. <td colspan="1" rowspan="1">
  2248. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2249. <span class="codefrag">try {</span>
  2250. </td>
  2251. </tr>
  2252. <tr>
  2253. <td colspan="1" rowspan="1">47.</td>
  2254. <td colspan="1" rowspan="1">
  2255. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2256. <span class="codefrag">
  2257. BufferedReader fis =
  2258. new BufferedReader(new FileReader(patternsFile.toString()));
  2259. </span>
  2260. </td>
  2261. </tr>
  2262. <tr>
  2263. <td colspan="1" rowspan="1">48.</td>
  2264. <td colspan="1" rowspan="1">
  2265. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2266. <span class="codefrag">String pattern = null;</span>
  2267. </td>
  2268. </tr>
  2269. <tr>
  2270. <td colspan="1" rowspan="1">49.</td>
  2271. <td colspan="1" rowspan="1">
  2272. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2273. <span class="codefrag">while ((pattern = fis.readLine()) != null) {</span>
  2274. </td>
  2275. </tr>
  2276. <tr>
  2277. <td colspan="1" rowspan="1">50.</td>
  2278. <td colspan="1" rowspan="1">
  2279. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2280. <span class="codefrag">patternsToSkip.add(pattern);</span>
  2281. </td>
  2282. </tr>
  2283. <tr>
  2284. <td colspan="1" rowspan="1">51.</td>
  2285. <td colspan="1" rowspan="1">
  2286. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2287. <span class="codefrag">}</span>
  2288. </td>
  2289. </tr>
  2290. <tr>
  2291. <td colspan="1" rowspan="1">52.</td>
  2292. <td colspan="1" rowspan="1">
  2293. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2294. <span class="codefrag">} catch (IOException ioe) {</span>
  2295. </td>
  2296. </tr>
  2297. <tr>
  2298. <td colspan="1" rowspan="1">53.</td>
  2299. <td colspan="1" rowspan="1">
  2300. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2301. <span class="codefrag">
  2302. System.err.println("Caught exception while parsing the cached file '" +
  2303. patternsFile + "' : " +
  2304. StringUtils.stringifyException(ioe));
  2305. </span>
  2306. </td>
  2307. </tr>
  2308. <tr>
  2309. <td colspan="1" rowspan="1">54.</td>
  2310. <td colspan="1" rowspan="1">
  2311. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2312. <span class="codefrag">}</span>
  2313. </td>
  2314. </tr>
  2315. <tr>
  2316. <td colspan="1" rowspan="1">55.</td>
  2317. <td colspan="1" rowspan="1">
  2318. &nbsp;&nbsp;&nbsp;&nbsp;
  2319. <span class="codefrag">}</span>
  2320. </td>
  2321. </tr>
  2322. <tr>
  2323. <td colspan="1" rowspan="1">56.</td>
  2324. <td colspan="1" rowspan="1"></td>
  2325. </tr>
  2326. <tr>
  2327. <td colspan="1" rowspan="1">57.</td>
  2328. <td colspan="1" rowspan="1">
  2329. &nbsp;&nbsp;&nbsp;&nbsp;
  2330. <span class="codefrag">
  2331. public void map(LongWritable key, Text value,
  2332. OutputCollector&lt;Text, IntWritable&gt; output,
  2333. Reporter reporter) throws IOException {
  2334. </span>
  2335. </td>
  2336. </tr>
  2337. <tr>
  2338. <td colspan="1" rowspan="1">58.</td>
  2339. <td colspan="1" rowspan="1">
  2340. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2341. <span class="codefrag">
  2342. String line =
  2343. (caseSensitive) ? value.toString() :
  2344. value.toString().toLowerCase();
  2345. </span>
  2346. </td>
  2347. </tr>
  2348. <tr>
  2349. <td colspan="1" rowspan="1">59.</td>
  2350. <td colspan="1" rowspan="1"></td>
  2351. </tr>
  2352. <tr>
  2353. <td colspan="1" rowspan="1">60.</td>
  2354. <td colspan="1" rowspan="1">
  2355. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2356. <span class="codefrag">for (String pattern : patternsToSkip) {</span>
  2357. </td>
  2358. </tr>
  2359. <tr>
  2360. <td colspan="1" rowspan="1">61.</td>
  2361. <td colspan="1" rowspan="1">
  2362. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2363. <span class="codefrag">line = line.replaceAll(pattern, "");</span>
  2364. </td>
  2365. </tr>
  2366. <tr>
  2367. <td colspan="1" rowspan="1">62.</td>
  2368. <td colspan="1" rowspan="1">
  2369. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2370. <span class="codefrag">}</span>
  2371. </td>
  2372. </tr>
  2373. <tr>
  2374. <td colspan="1" rowspan="1">63.</td>
  2375. <td colspan="1" rowspan="1"></td>
  2376. </tr>
  2377. <tr>
  2378. <td colspan="1" rowspan="1">64.</td>
  2379. <td colspan="1" rowspan="1">
  2380. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2381. <span class="codefrag">StringTokenizer tokenizer = new StringTokenizer(line);</span>
  2382. </td>
  2383. </tr>
  2384. <tr>
  2385. <td colspan="1" rowspan="1">65.</td>
  2386. <td colspan="1" rowspan="1">
  2387. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2388. <span class="codefrag">while (tokenizer.hasMoreTokens()) {</span>
  2389. </td>
  2390. </tr>
  2391. <tr>
  2392. <td colspan="1" rowspan="1">66.</td>
  2393. <td colspan="1" rowspan="1">
  2394. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2395. <span class="codefrag">word.set(tokenizer.nextToken());</span>
  2396. </td>
  2397. </tr>
  2398. <tr>
  2399. <td colspan="1" rowspan="1">67.</td>
  2400. <td colspan="1" rowspan="1">
  2401. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2402. <span class="codefrag">output.collect(word, one);</span>
  2403. </td>
  2404. </tr>
  2405. <tr>
  2406. <td colspan="1" rowspan="1">68.</td>
  2407. <td colspan="1" rowspan="1">
  2408. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2409. <span class="codefrag">reporter.incrCounter(Counters.INPUT_WORDS, 1);</span>
  2410. </td>
  2411. </tr>
  2412. <tr>
  2413. <td colspan="1" rowspan="1">69.</td>
  2414. <td colspan="1" rowspan="1">
  2415. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2416. <span class="codefrag">}</span>
  2417. </td>
  2418. </tr>
  2419. <tr>
  2420. <td colspan="1" rowspan="1">70.</td>
  2421. <td colspan="1" rowspan="1"></td>
  2422. </tr>
  2423. <tr>
  2424. <td colspan="1" rowspan="1">71.</td>
  2425. <td colspan="1" rowspan="1">
  2426. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2427. <span class="codefrag">if ((++numRecords % 100) == 0) {</span>
  2428. </td>
  2429. </tr>
  2430. <tr>
  2431. <td colspan="1" rowspan="1">72.</td>
  2432. <td colspan="1" rowspan="1">
  2433. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2434. <span class="codefrag">
  2435. reporter.setStatus("Finished processing " + numRecords +
  2436. " records " + "from the input file: " +
  2437. inputFile);
  2438. </span>
  2439. </td>
  2440. </tr>
  2441. <tr>
  2442. <td colspan="1" rowspan="1">73.</td>
  2443. <td colspan="1" rowspan="1">
  2444. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2445. <span class="codefrag">}</span>
  2446. </td>
  2447. </tr>
  2448. <tr>
  2449. <td colspan="1" rowspan="1">74.</td>
  2450. <td colspan="1" rowspan="1">
  2451. &nbsp;&nbsp;&nbsp;&nbsp;
  2452. <span class="codefrag">}</span>
  2453. </td>
  2454. </tr>
  2455. <tr>
  2456. <td colspan="1" rowspan="1">75.</td>
  2457. <td colspan="1" rowspan="1">
  2458. &nbsp;&nbsp;
  2459. <span class="codefrag">}</span>
  2460. </td>
  2461. </tr>
  2462. <tr>
  2463. <td colspan="1" rowspan="1">76.</td>
  2464. <td colspan="1" rowspan="1"></td>
  2465. </tr>
  2466. <tr>
  2467. <td colspan="1" rowspan="1">77.</td>
  2468. <td colspan="1" rowspan="1">
  2469. &nbsp;&nbsp;
  2470. <span class="codefrag">
  2471. public static class Reduce extends MapReduceBase implements
  2472. Reducer&lt;Text, IntWritable, Text, IntWritable&gt; {
  2473. </span>
  2474. </td>
  2475. </tr>
  2476. <tr>
  2477. <td colspan="1" rowspan="1">78.</td>
  2478. <td colspan="1" rowspan="1">
  2479. &nbsp;&nbsp;&nbsp;&nbsp;
  2480. <span class="codefrag">
  2481. public void reduce(Text key, Iterator&lt;IntWritable&gt; values,
  2482. OutputCollector&lt;Text, IntWritable&gt; output,
  2483. Reporter reporter) throws IOException {
  2484. </span>
  2485. </td>
  2486. </tr>
  2487. <tr>
  2488. <td colspan="1" rowspan="1">79.</td>
  2489. <td colspan="1" rowspan="1">
  2490. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2491. <span class="codefrag">int sum = 0;</span>
  2492. </td>
  2493. </tr>
  2494. <tr>
  2495. <td colspan="1" rowspan="1">80.</td>
  2496. <td colspan="1" rowspan="1">
  2497. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2498. <span class="codefrag">while (values.hasNext()) {</span>
  2499. </td>
  2500. </tr>
  2501. <tr>
  2502. <td colspan="1" rowspan="1">81.</td>
  2503. <td colspan="1" rowspan="1">
  2504. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2505. <span class="codefrag">sum += values.next().get();</span>
  2506. </td>
  2507. </tr>
  2508. <tr>
  2509. <td colspan="1" rowspan="1">82.</td>
  2510. <td colspan="1" rowspan="1">
  2511. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2512. <span class="codefrag">}</span>
  2513. </td>
  2514. </tr>
  2515. <tr>
  2516. <td colspan="1" rowspan="1">83.</td>
  2517. <td colspan="1" rowspan="1">
  2518. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2519. <span class="codefrag">output.collect(key, new IntWritable(sum));</span>
  2520. </td>
  2521. </tr>
  2522. <tr>
  2523. <td colspan="1" rowspan="1">84.</td>
  2524. <td colspan="1" rowspan="1">
  2525. &nbsp;&nbsp;&nbsp;&nbsp;
  2526. <span class="codefrag">}</span>
  2527. </td>
  2528. </tr>
  2529. <tr>
  2530. <td colspan="1" rowspan="1">85.</td>
  2531. <td colspan="1" rowspan="1">
  2532. &nbsp;&nbsp;
  2533. <span class="codefrag">}</span>
  2534. </td>
  2535. </tr>
  2536. <tr>
  2537. <td colspan="1" rowspan="1">86.</td>
  2538. <td colspan="1" rowspan="1"></td>
  2539. </tr>
  2540. <tr>
  2541. <td colspan="1" rowspan="1">87.</td>
  2542. <td colspan="1" rowspan="1">
  2543. &nbsp;&nbsp;
  2544. <span class="codefrag">public int run(String[] args) throws Exception {</span>
  2545. </td>
  2546. </tr>
  2547. <tr>
  2548. <td colspan="1" rowspan="1">88.</td>
  2549. <td colspan="1" rowspan="1">
  2550. &nbsp;&nbsp;&nbsp;&nbsp;
  2551. <span class="codefrag">
  2552. JobConf conf = new JobConf(getConf(), WordCount.class);
  2553. </span>
  2554. </td>
  2555. </tr>
  2556. <tr>
  2557. <td colspan="1" rowspan="1">89.</td>
  2558. <td colspan="1" rowspan="1">
  2559. &nbsp;&nbsp;&nbsp;&nbsp;
  2560. <span class="codefrag">conf.setJobName("wordcount");</span>
  2561. </td>
  2562. </tr>
  2563. <tr>
  2564. <td colspan="1" rowspan="1">90.</td>
  2565. <td colspan="1" rowspan="1"></td>
  2566. </tr>
  2567. <tr>
  2568. <td colspan="1" rowspan="1">91.</td>
  2569. <td colspan="1" rowspan="1">
  2570. &nbsp;&nbsp;&nbsp;&nbsp;
  2571. <span class="codefrag">conf.setOutputKeyClass(Text.class);</span>
  2572. </td>
  2573. </tr>
  2574. <tr>
  2575. <td colspan="1" rowspan="1">92.</td>
  2576. <td colspan="1" rowspan="1">
  2577. &nbsp;&nbsp;&nbsp;&nbsp;
  2578. <span class="codefrag">conf.setOutputValueClass(IntWritable.class);</span>
  2579. </td>
  2580. </tr>
  2581. <tr>
  2582. <td colspan="1" rowspan="1">93.</td>
  2583. <td colspan="1" rowspan="1"></td>
  2584. </tr>
  2585. <tr>
  2586. <td colspan="1" rowspan="1">94.</td>
  2587. <td colspan="1" rowspan="1">
  2588. &nbsp;&nbsp;&nbsp;&nbsp;
  2589. <span class="codefrag">conf.setMapperClass(Map.class);</span>
  2590. </td>
  2591. </tr>
  2592. <tr>
  2593. <td colspan="1" rowspan="1">95.</td>
  2594. <td colspan="1" rowspan="1">
  2595. &nbsp;&nbsp;&nbsp;&nbsp;
  2596. <span class="codefrag">conf.setCombinerClass(Reduce.class);</span>
  2597. </td>
  2598. </tr>
  2599. <tr>
  2600. <td colspan="1" rowspan="1">96.</td>
  2601. <td colspan="1" rowspan="1">
  2602. &nbsp;&nbsp;&nbsp;&nbsp;
  2603. <span class="codefrag">conf.setReducerClass(Reduce.class);</span>
  2604. </td>
  2605. </tr>
  2606. <tr>
  2607. <td colspan="1" rowspan="1">97.</td>
  2608. <td colspan="1" rowspan="1"></td>
  2609. </tr>
  2610. <tr>
  2611. <td colspan="1" rowspan="1">98.</td>
  2612. <td colspan="1" rowspan="1">
  2613. &nbsp;&nbsp;&nbsp;&nbsp;
  2614. <span class="codefrag">conf.setInputFormat(TextInputFormat.class);</span>
  2615. </td>
  2616. </tr>
  2617. <tr>
  2618. <td colspan="1" rowspan="1">99.</td>
  2619. <td colspan="1" rowspan="1">
  2620. &nbsp;&nbsp;&nbsp;&nbsp;
  2621. <span class="codefrag">conf.setOutputFormat(TextOutputFormat.class);</span>
  2622. </td>
  2623. </tr>
  2624. <tr>
  2625. <td colspan="1" rowspan="1">100.</td>
  2626. <td colspan="1" rowspan="1"></td>
  2627. </tr>
  2628. <tr>
  2629. <td colspan="1" rowspan="1">101.</td>
  2630. <td colspan="1" rowspan="1">
  2631. &nbsp;&nbsp;&nbsp;&nbsp;
  2632. <span class="codefrag">
  2633. List&lt;String&gt; other_args = new ArrayList&lt;String&gt;();
  2634. </span>
  2635. </td>
  2636. </tr>
  2637. <tr>
  2638. <td colspan="1" rowspan="1">102.</td>
  2639. <td colspan="1" rowspan="1">
  2640. &nbsp;&nbsp;&nbsp;&nbsp;
  2641. <span class="codefrag">for (int i=0; i &lt; args.length; ++i) {</span>
  2642. </td>
  2643. </tr>
  2644. <tr>
  2645. <td colspan="1" rowspan="1">103.</td>
  2646. <td colspan="1" rowspan="1">
  2647. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2648. <span class="codefrag">if ("-skip".equals(args[i])) {</span>
  2649. </td>
  2650. </tr>
  2651. <tr>
  2652. <td colspan="1" rowspan="1">104.</td>
  2653. <td colspan="1" rowspan="1">
  2654. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2655. <span class="codefrag">
  2656. DistributedCache.addCacheFile(new Path(args[++i]).toUri(), conf);
  2657. </span>
  2658. </td>
  2659. </tr>
  2660. <tr>
  2661. <td colspan="1" rowspan="1">105.</td>
  2662. <td colspan="1" rowspan="1">
  2663. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2664. <span class="codefrag">
  2665. conf.setBoolean("wordcount.skip.patterns", true);
  2666. </span>
  2667. </td>
  2668. </tr>
  2669. <tr>
  2670. <td colspan="1" rowspan="1">106.</td>
  2671. <td colspan="1" rowspan="1">
  2672. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2673. <span class="codefrag">} else {</span>
  2674. </td>
  2675. </tr>
  2676. <tr>
  2677. <td colspan="1" rowspan="1">107.</td>
  2678. <td colspan="1" rowspan="1">
  2679. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2680. <span class="codefrag">other_args.add(args[i]);</span>
  2681. </td>
  2682. </tr>
  2683. <tr>
  2684. <td colspan="1" rowspan="1">108.</td>
  2685. <td colspan="1" rowspan="1">
  2686. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  2687. <span class="codefrag">}</span>
  2688. </td>
  2689. </tr>
  2690. <tr>
  2691. <td colspan="1" rowspan="1">109.</td>
  2692. <td colspan="1" rowspan="1">
  2693. &nbsp;&nbsp;&nbsp;&nbsp;
  2694. <span class="codefrag">}</span>
  2695. </td>
  2696. </tr>
  2697. <tr>
  2698. <td colspan="1" rowspan="1">110.</td>
  2699. <td colspan="1" rowspan="1"></td>
  2700. </tr>
  2701. <tr>
  2702. <td colspan="1" rowspan="1">111.</td>
  2703. <td colspan="1" rowspan="1">
  2704. &nbsp;&nbsp;&nbsp;&nbsp;
  2705. <span class="codefrag">FileInputFormat.setInputPaths(conf, new Path(other_args.get(0)));</span>
  2706. </td>
  2707. </tr>
  2708. <tr>
  2709. <td colspan="1" rowspan="1">112.</td>
  2710. <td colspan="1" rowspan="1">
  2711. &nbsp;&nbsp;&nbsp;&nbsp;
  2712. <span class="codefrag">FileOutputFormat.setOutputPath(conf, new Path(other_args.get(1)));</span>
  2713. </td>
  2714. </tr>
  2715. <tr>
  2716. <td colspan="1" rowspan="1">113.</td>
  2717. <td colspan="1" rowspan="1"></td>
  2718. </tr>
  2719. <tr>
  2720. <td colspan="1" rowspan="1">114.</td>
  2721. <td colspan="1" rowspan="1">
  2722. &nbsp;&nbsp;&nbsp;&nbsp;
  2723. <span class="codefrag">JobClient.runJob(conf);</span>
  2724. </td>
  2725. </tr>
  2726. <tr>
  2727. <td colspan="1" rowspan="1">115.</td>
  2728. <td colspan="1" rowspan="1">
  2729. &nbsp;&nbsp;&nbsp;&nbsp;
  2730. <span class="codefrag">return 0;</span>
  2731. </td>
  2732. </tr>
  2733. <tr>
  2734. <td colspan="1" rowspan="1">116.</td>
  2735. <td colspan="1" rowspan="1">
  2736. &nbsp;&nbsp;
  2737. <span class="codefrag">}</span>
  2738. </td>
  2739. </tr>
  2740. <tr>
  2741. <td colspan="1" rowspan="1">117.</td>
  2742. <td colspan="1" rowspan="1"></td>
  2743. </tr>
  2744. <tr>
  2745. <td colspan="1" rowspan="1">118.</td>
  2746. <td colspan="1" rowspan="1">
  2747. &nbsp;&nbsp;
  2748. <span class="codefrag">
  2749. public static void main(String[] args) throws Exception {
  2750. </span>
  2751. </td>
  2752. </tr>
  2753. <tr>
  2754. <td colspan="1" rowspan="1">119.</td>
  2755. <td colspan="1" rowspan="1">
  2756. &nbsp;&nbsp;&nbsp;&nbsp;
  2757. <span class="codefrag">
  2758. int res = ToolRunner.run(new Configuration(), new WordCount(),
  2759. args);
  2760. </span>
  2761. </td>
  2762. </tr>
  2763. <tr>
  2764. <td colspan="1" rowspan="1">120.</td>
  2765. <td colspan="1" rowspan="1">
  2766. &nbsp;&nbsp;&nbsp;&nbsp;
  2767. <span class="codefrag">System.exit(res);</span>
  2768. </td>
  2769. </tr>
  2770. <tr>
  2771. <td colspan="1" rowspan="1">121.</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">122.</td>
  2779. <td colspan="1" rowspan="1">
  2780. <span class="codefrag">}</span>
  2781. </td>
  2782. </tr>
  2783. <tr>
  2784. <td colspan="1" rowspan="1">123.</td>
  2785. <td colspan="1" rowspan="1"></td>
  2786. </tr>
  2787. </table>
  2788. <a name="N11537"></a><a name="Sample+Runs"></a>
  2789. <h3 class="h4">Sample Runs</h3>
  2790. <p>Sample text-files as input:</p>
  2791. <p>
  2792. <span class="codefrag">$ bin/hadoop dfs -ls /usr/joe/wordcount/input/</span>
  2793. <br>
  2794. <span class="codefrag">/usr/joe/wordcount/input/file01</span>
  2795. <br>
  2796. <span class="codefrag">/usr/joe/wordcount/input/file02</span>
  2797. <br>
  2798. <br>
  2799. <span class="codefrag">$ bin/hadoop dfs -cat /usr/joe/wordcount/input/file01</span>
  2800. <br>
  2801. <span class="codefrag">Hello World, Bye World!</span>
  2802. <br>
  2803. <br>
  2804. <span class="codefrag">$ bin/hadoop dfs -cat /usr/joe/wordcount/input/file02</span>
  2805. <br>
  2806. <span class="codefrag">Hello Hadoop, Goodbye to hadoop.</span>
  2807. </p>
  2808. <p>Run the application:</p>
  2809. <p>
  2810. <span class="codefrag">
  2811. $ bin/hadoop jar /usr/joe/wordcount.jar org.myorg.WordCount
  2812. /usr/joe/wordcount/input /usr/joe/wordcount/output
  2813. </span>
  2814. </p>
  2815. <p>Output:</p>
  2816. <p>
  2817. <span class="codefrag">
  2818. $ bin/hadoop dfs -cat /usr/joe/wordcount/output/part-00000
  2819. </span>
  2820. <br>
  2821. <span class="codefrag">Bye 1</span>
  2822. <br>
  2823. <span class="codefrag">Goodbye 1</span>
  2824. <br>
  2825. <span class="codefrag">Hadoop, 1</span>
  2826. <br>
  2827. <span class="codefrag">Hello 2</span>
  2828. <br>
  2829. <span class="codefrag">World! 1</span>
  2830. <br>
  2831. <span class="codefrag">World, 1</span>
  2832. <br>
  2833. <span class="codefrag">hadoop. 1</span>
  2834. <br>
  2835. <span class="codefrag">to 1</span>
  2836. <br>
  2837. </p>
  2838. <p>Notice that the inputs differ from the first version we looked at,
  2839. and how they affect the outputs.</p>
  2840. <p>Now, lets plug-in a pattern-file which lists the word-patterns to be
  2841. ignored, via the <span class="codefrag">DistributedCache</span>.</p>
  2842. <p>
  2843. <span class="codefrag">$ hadoop dfs -cat /user/joe/wordcount/patterns.txt</span>
  2844. <br>
  2845. <span class="codefrag">\.</span>
  2846. <br>
  2847. <span class="codefrag">\,</span>
  2848. <br>
  2849. <span class="codefrag">\!</span>
  2850. <br>
  2851. <span class="codefrag">to</span>
  2852. <br>
  2853. </p>
  2854. <p>Run it again, this time with more options:</p>
  2855. <p>
  2856. <span class="codefrag">
  2857. $ bin/hadoop jar /usr/joe/wordcount.jar org.myorg.WordCount
  2858. -Dwordcount.case.sensitive=true /usr/joe/wordcount/input
  2859. /usr/joe/wordcount/output -skip /user/joe/wordcount/patterns.txt
  2860. </span>
  2861. </p>
  2862. <p>As expected, the output:</p>
  2863. <p>
  2864. <span class="codefrag">
  2865. $ bin/hadoop dfs -cat /usr/joe/wordcount/output/part-00000
  2866. </span>
  2867. <br>
  2868. <span class="codefrag">Bye 1</span>
  2869. <br>
  2870. <span class="codefrag">Goodbye 1</span>
  2871. <br>
  2872. <span class="codefrag">Hadoop 1</span>
  2873. <br>
  2874. <span class="codefrag">Hello 2</span>
  2875. <br>
  2876. <span class="codefrag">World 2</span>
  2877. <br>
  2878. <span class="codefrag">hadoop 1</span>
  2879. <br>
  2880. </p>
  2881. <p>Run it once more, this time switch-off case-sensitivity:</p>
  2882. <p>
  2883. <span class="codefrag">
  2884. $ bin/hadoop jar /usr/joe/wordcount.jar org.myorg.WordCount
  2885. -Dwordcount.case.sensitive=false /usr/joe/wordcount/input
  2886. /usr/joe/wordcount/output -skip /user/joe/wordcount/patterns.txt
  2887. </span>
  2888. </p>
  2889. <p>Sure enough, the output:</p>
  2890. <p>
  2891. <span class="codefrag">
  2892. $ bin/hadoop dfs -cat /usr/joe/wordcount/output/part-00000
  2893. </span>
  2894. <br>
  2895. <span class="codefrag">bye 1</span>
  2896. <br>
  2897. <span class="codefrag">goodbye 1</span>
  2898. <br>
  2899. <span class="codefrag">hadoop 2</span>
  2900. <br>
  2901. <span class="codefrag">hello 2</span>
  2902. <br>
  2903. <span class="codefrag">world 2</span>
  2904. <br>
  2905. </p>
  2906. <a name="N1160B"></a><a name="Highlights"></a>
  2907. <h3 class="h4">Highlights</h3>
  2908. <p>The second version of <span class="codefrag">WordCount</span> improves upon the
  2909. previous one by using some features offered by the Map-Reduce framework:
  2910. </p>
  2911. <ul>
  2912. <li>
  2913. Demonstrates how applications can access configuration parameters
  2914. in the <span class="codefrag">configure</span> method of the <span class="codefrag">Mapper</span> (and
  2915. <span class="codefrag">Reducer</span>) implementations (lines 28-43).
  2916. </li>
  2917. <li>
  2918. Demonstrates how the <span class="codefrag">DistributedCache</span> can be used to
  2919. distribute read-only data needed by the jobs. Here it allows the user
  2920. to specify word-patterns to skip while counting (line 104).
  2921. </li>
  2922. <li>
  2923. Demonstrates the utility of the <span class="codefrag">Tool</span> interface and the
  2924. <span class="codefrag">GenericOptionsParser</span> to handle generic Hadoop
  2925. command-line options (lines 87-116, 119).
  2926. </li>
  2927. <li>
  2928. Demonstrates how applications can use <span class="codefrag">Counters</span> (line 68)
  2929. and how they can set application-specific status information via
  2930. the <span class="codefrag">Reporter</span> instance passed to the <span class="codefrag">map</span> (and
  2931. <span class="codefrag">reduce</span>) method (line 72).
  2932. </li>
  2933. </ul>
  2934. </div>
  2935. <p>
  2936. <em>Java and JNI are trademarks or registered trademarks of
  2937. Sun Microsystems, Inc. in the United States and other countries.</em>
  2938. </p>
  2939. </div>
  2940. <!--+
  2941. |end content
  2942. +-->
  2943. <div class="clearboth">&nbsp;</div>
  2944. </div>
  2945. <div id="footer">
  2946. <!--+
  2947. |start bottomstrip
  2948. +-->
  2949. <div class="lastmodified">
  2950. <script type="text/javascript"><!--
  2951. document.write("Last Published: " + document.lastModified);
  2952. // --></script>
  2953. </div>
  2954. <div class="copyright">
  2955. Copyright &copy;
  2956. 2008 <a href="http://www.apache.org/licenses/">The Apache Software Foundation.</a>
  2957. </div>
  2958. <!--+
  2959. |end bottomstrip
  2960. +-->
  2961. </div>
  2962. </body>
  2963. </html>