mapred_tutorial.html 120 KB

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