一直對書和各種介紹不太滿意, 終于看到一篇比較好的了,迅速轉載.
首先要推薦一下: http://www.alidata.org/archives/1470
阿里的大牛在上面的文章中比較詳細的介紹了shuffle過程中mapper和reduce的每個過程,強烈推薦先讀一下。
?
不過,上文沒有寫明一些實現的細節,比如:spill的過程,mapper生成文件的 partition是怎么做的等等,相信有很多人跟我一樣在看了上面的文章后還是有很多疑問,我也是帶著疑問花了很久的看了cdh4.1.0版本 shuffle的邏輯,整理成本文,為以后回顧所用。
?
?首先用一張圖展示下map的流程:
- <span?style= ???*?Called?once?for?each?key/value?pair?in?the?input?split.?Most?applications?
- ???*?should?override?this,?but?the?default?is?the?identity?function.?
- ???*/ ( )??
- protected void throws </span>??
- <span?style= public void throws while ??}</span>??
key value在寫入context中后實際是寫入MapOutputBuffer類中。在第一個階段的初始化過程中,MapOutputBuffer類會根據配置文件初始化內存buffer,我們來看下都有哪些參數:
- <span?style= ??
- final float float 0.8 final int );??
- if float 1.0 float 0.0 throw new if )?!=?sortmb)?{??
- throw new ,??
- class class ),?job);</span>??
- <span?style= ??
- public void throws ????}</span>??
在keyvalue對寫入MapOutputBuffer時會調用 partitioner.getPartition方法計算partition即應該分配到哪個reducer,這里的partition只是在內存的 buf的index區寫入一條記錄而已,和下一個部分的partition不一樣哦。看下默認的partitioner:HashPartition
- <span?style= ??
- public int int return ??}</span>??
HashPartition只是把key hash后按reduceTask的個數取模, 因此一般來說,不同的key分配到哪個reducer是隨即的!所以,reducer內的所有數據是有序的,但reducer之間的數據卻是亂序的!要想數據整體排序,要不只設一個reducer,要不使用TotalOrderPartitioner!
- <span?style= this ,?mstart,?mend,?reporter);</span>??
- <span?style= public int final int final int final int final int final int final int ??
- if return ??
- return ????}</span>??
- <span?style= for int ;?i?<?partitions;?++i)?{??
- null try long new if null ??
- new while final int else int while ??
- ??
- if new ????????????}</span>??
在生成spill文件后還會將此次spillRecord的記錄寫在一個index文件中。
- <span?style= ??????????spillRec.writeToFile(indexFilename,?job);</span>??
- <span?style= ????????????spillRec.putIndex(rec,?i);</span>??
- <span?style= int );</span>??
- <span?style= new null ??
- long new if null else ??????????}</span>??
- <span?style= ??
- ??????????spillRec.putIndex(rec,?parts);</span>??
最后,我們再對mapper過程中的要點總結一下:
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

