Hadoop裏的Partitioner和Combiner兩個階段
人們對於Mapreduce程序剛開始時都認為隻需要一個reduce就夠了。畢竟,在你處理數據之前一個reducer已經把數據都分好類了,有誰不喜歡分好類的數據呢。但是這樣我們就忽略了並行計算的優勢。如果隻有一個reducer,我們的雲計算就退化成了一個小雨點。
在多個reducer的情況下,我們需要某種機製來控製mapper的結果的分配問題。這是就Partitioner的工作了。
在默認情況下,hadoop通過比較key的hash值來分配,默認使用HashPartitioner。
有時默認的功能不能滿足我們的要求,比如我們以前自定義的Edge類(https://blog.csdn.net/on_way_/article/details/8589187)。當我們想要知道每個機場乘客起飛的數量時。我們有如下數據
(北京, 上海) 張三
(北京, 青島) 李四。。。。。。。
如果我們用HashPartitioner來分配,那麼這兩行就會被送往不同的reducer上,機場起飛的數量就會被算兩次,而且每次都是錯誤的。
我們需要為我們的應用程序定製一個partitioner。
import org.apache.hadoop.io.Writable; import org.apache.hadoop.mapred.JobConf; import org.apache.hadoop.mapred.Partitioner; public class EdgePartitioner implements Partitioner<Edge, Writable>{ @Override public void configure(JobConf job) { // TODO Auto-generated method stub } @Override public int getPartition(Edge key, Writable value, int numPartitions) { // TODO Auto-generated method stub return key.getDepartureNode().hashCode() % numPartitions; } }
在map和reduce兩個階段之間,一個MapReduce程序必須把mapper的輸出分配到多個reducer上,這個過程叫做shuffling,因為一個mapper的輸出結果有可能被分配到集群中的多個節點中去。
Combiner----local reduce
在有些情況下,我們希望在分配mapper的結果之前進行一次“local reduce”。比如WordCount程序,我們在處理完一個文檔之後得到了“the”1000次,it much more efficient to store and shuffle the pair("the",574) once instread of the pair("the",1) multiple times.這個過程就叫做combiner。今天先簡單介紹一下combiner,以後會自己詳解。
最後更新:2017-04-04 07:03:49