mapred_tutorial.html 110 KB

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