mapred_tutorial.html 111 KB

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