mapred_tutorial.html 151 KB

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