mapreduce學習筆記四:排序
1.Map端:
(1)每個輸入分片會讓一個map任務來處理,默認情況下,以HDFS的一個塊的大小(默認為64M)為一個分片,當然我們也可以設置塊的大小。map輸出的結果會暫且放在一個環形內存緩沖區中(該緩沖區的大小默認為100M,由io.sort.mb屬性控制),當該緩沖區快要溢出時(默認為緩沖區大小的80%,由io.sort.spill.percent屬性控制),會在本地文件系統中創建一個溢出文件,將該緩沖區中的數據寫入這個文件。
(2)在寫入磁盤之前,線程首先根據reduce任務的數目將數據劃分為相同數目的分區,也就是一個reduce任務對應一個分區的數據。這樣做是為了避免有些reduce任務分配到大量數據,而有些reduce任務卻分到很少數據,甚至沒有分到數據的尷尬局面。其實分區就是對數據進行hash的過程。然後對每個分區中的數據進行排序,如果此時設置了Combiner,將排序後的結果進行Combia操作,這樣做的目的是讓盡可能少的數據寫入到磁盤。
(3)當map任務輸出最後一個記錄時,可能會有很多的溢出文件,這時需要將這些文件合並。合並的過程中會不斷地進行排序和combia操作,目的有兩個:①盡量減少每次寫入磁盤的數據量。②盡量減少下一復制階段網絡傳輸的數據量。最後合並成了一個已分區且已排序的文件。為了減少網絡傳輸的數據量,這裏可以將數據壓縮,只要將mapred.compress.map.out設置為true就可以了。
(4)將分區中的數據拷貝給相對應的reduce任務。有人可能會問:分區中的數據怎麽知道它對應的reduce是哪個呢?其實map任務一直和其父TaskTracker保持聯系,而TaskTracker又一直和JobTracker保持心跳。所以JobTracker中保存了整個集群中的宏觀信息。只要reduce任務向JobTracker獲取對應的map輸出位置就ok了哦。
到這裏,map端就分析完了。那到底什麽是Shuffle呢?Shuffle的中文意思是“洗牌”,如果我們這樣看:一個map產生的數據,結果通過hash過程分區卻分配給了不同的reduce任務,是不是一個對數據洗牌的過程呢?
2.Reduce端:
(1)Reduce會接收到不同map任務傳來的數據,並且每個map傳來的數據都是有序的。如果reduce端接受的數據量相當小,則直接存儲在內存中(緩沖區大小由mapred.job.shuffle.input.buffer.percent屬性控制,表示用作此用途的堆空間的百分比),如果數據量超過了該緩沖區大小的一定比例(由mapred.job.shuffle.merge.percent決定),則對數據合並後溢寫到磁盤中。
(2)隨著溢寫文件的增多,後臺線程會將它們合並成一個更大的有序的文件,這樣做是為了給後面的合並節省時間。其實不管在map端還是reduce端,MapReduce都是反復地執行排序,合並操作,現在終於明白了有些人為什麽會說:排序是hadoop的靈魂。
(3)合並的過程中會產生許多的中間文件(寫入磁盤了),但MapReduce會讓寫入磁盤的數據盡可能地少,並且最後一次合並的結果並沒有寫入磁盤,而是直接輸入到reduce函數。
熟悉MapReduce的人都知道:排序是MapReduce的天然特性!在數據達到reducer之前,MapReduce框架已經對這些數據按鍵排序了。但是在使用之前,首先需要了解它的默認排序規則。它是按照key值進行排序的,如果key為封裝的int為IntWritable類型,那麽MapReduce按照數字大小對key排序,如果Key為封裝String的Text類型,那麽MapReduce將按照數據字典順序對字符排序。
了解了這個細節,我們就知道應該使用封裝int的Intwritable型數據結構了,也就是在map這裏,將讀入的數據中要排序的字段轉化為Intwritable型,然後作為key值輸出(不排序的字段作為value)。reduce階段拿到<key,value-list>之後,將輸入的key作為的輸出key,並根據value-list中的元素的個數決定輸出的次數。
package mapreduce; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; public class OneSort { public static class Map extends Mapper<Object , Text , IntWritable,Text >{ private static Text goods=new Text(); private static IntWritable num=new IntWritable(); public void map(Object key,Text value,Context context) throws IOException, InterruptedException{ String line=value.toString(); String arr[]=line.split(" "); System.out.println(arr[1]); num.set(Integer.parseInt(arr[1])); goods.set(arr[0]); context.write(num,goods); } } public static class Reduce extends Reducer< IntWritable, Text, IntWritable, Text>{ @SuppressWarnings("unused") private static IntWritable result= new IntWritable(); public void reduce(IntWritable key,Iterable<Text> values,Context context) throws IOException, InterruptedException{ for(Text val:values){ context.write(key,val); } } } @SuppressWarnings("deprecation") public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException{ Configuration conf=new Configuration(); conf.set("dfs.client.use.datanode.hostname", "true"); Job job =new Job(conf,"OneSort"); job.setJarByClass(OneSort.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); job.setOutputKeyClass(IntWritable.class); job.setOutputValueClass(Text.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); Path in=new Path("hdfs://**:9000/user/hadoop/input/d.txt"); System.out.println("in執行完畢"); Path out=new Path("hdfs://**:9000/user/hadoop/output"); System.out.println("out執行完畢"); Path path = new Path("hdfs://**:9000/user/hadoop/output"); FileSystem fileSystem = path.getFileSystem(conf);// 根據path找到這個文件 if (fileSystem.exists(path)) { fileSystem.delete(path, true);// true的意思是,就算output有東西,也一帶刪除 } FileInputFormat.addInputPath(job,in); FileOutputFormat.setOutputPath(job,out); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
mapreduce學習筆記四:排序