mapred_tutorial.html 150 KB

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