mapred_tutorial.html 120 KB

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