mapred_tutorial.html 129 KB

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