mapred_tutorial.html 127 KB

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