四种常见的MapReduce设计模式
為什么80%的碼農都做不了架構師?>>> ??
MapReduce設計模式(MapReduce Design Pattern)
整個MapReduce作業的階段主要可以分為以下四種:
1、Input-Map-Reduce-Output
2、Input-Map-Output
3、Input-Multiple Maps-Reduce-Output
4、Input-Map-Combiner-Reduce-Output
下面我將一一介紹哪種場景使用哪種設計模式。
Input-Map-Reduce-Output
Input?Map?Reduce?Output如果我們需要做一些聚合操作(aggregation),我們就需要使用這種模式。
| Map(Key, Value) | Key: Gender Value: Their Salary |
| Reduce | 對Gender進行Group by,并計算每種性別的總薪水 |
Input-Map-Output
Input?Map?Output如果我們僅僅想改變輸入數據的格式,這時候我們可以使用這種模式。
| Map(Key, Value) | Key : Employee Id Value : Gender -> if Gender is Female/ F/ f/ 0 then converted to F else if Gender is Male/M/m/1 then convert to M |
Input-Multiple Maps-Reduce-Output
Input1?Map1?Reduce?Output Input2?Map2?在這種設計模式中,我們有兩個輸入文件,其文件的格式都不一樣,
文件一的格式是性別作為名字的前綴,比如:Ms. Shital Katkar或Mr. Krishna Katkar
文件二的格式是性別的格式是固定的,但是其位置不固定,比如 Female/Male, 0/1, F/M
| Map(Key, Value) | Map 1 (For input 1):我們需要將性別從名字中分割出來,然后根據前綴來確定性別,然后得到 (Gender,Salary)鍵值對; Map 2 (For input 2):這種情況程序編寫比較直接,處理固定格式的性別,然后得到(Gender,Salary)鍵值對。 |
| Reduce | 對Gender進行Group by,并計算每種性別的總薪水 |
Input-Map-Combiner-Reduce-Output
Input?Map?Combiner?Reduce?Output在MapReduce中,Combiner也被成為Reduce,其接收Map端的輸出作為其輸入,并且將輸出的 key-value 鍵值對作為Reduce的輸入。Combiner的使用目的是為了減少數據傳入到Reduce的負載。
在MapReduce程序中,20%的工作是在Map階段執行的,這個階段也被成為數據的準備階段,各階段的工作是并行進行的。
80%的工作是在Reduce階段執行的,這個階段被成為計算階段,其不是并行的。因此,次階段一般要比Map階段要滿。為了節約時間,一些在Reduce階段處理的工作可以在combiner階段完成。
假設我們有5個部門(departments),我們需要計算個性別的總薪水。但是計算薪水的規則有點奇怪,比如某個性別的總薪水大于200k,那么這個性別的總薪水需要加上20k;如果某個性別的總薪水大于100k,那么這個性別的總薪水需要加上10k。如下:
| Map階段: Dept 1: Male<10,20,25,45,15,45,25,20>,Female <10,30,20,25,35> Dept 2: Male<15,30,40,25,45>,Female <20,35,25,35,40> Dept 3: Male<10,20,20,40>,Female <10,30,25,70> Dept 4: Male<45,25,20>,Female <30,20,25,35> Dept 5: Male<10,20>,Female <10,30,20,25,35> ? Combiner階段: Dept 1:Male <250,20>,Female <120,10> Dept 2:Male <155,10>,Female <175,10> Dept 3:Male <90,00>,Female <135,10> Dept 4:Male <90,00>,Female <110,10> Dept 5:Male <30,00>,Female <130,10> ? Reduce階段: Male< 250,20,155,10,90,90,30>,Female<120,10,175,10,135,10,110,10,130,10> ? Output: Male<645>,Female<720> |
以上四種MapReduce模式只是最基本的,我們可以根據自己問題設計不一樣的設計模式。
本文翻譯自:https://dzone.com/articles/mapreduce-design-patterns
轉載于:https://my.oschina.net/jsan/blog/749046
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的四种常见的MapReduce设计模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: thinkphp phpexcel导出
- 下一篇: Memcache mutex设计模式