mapred_tutorial.html 149 KB

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