使用Hadoop的MapReduce實現資料排序
阿新 • • 發佈:2019-02-19
最近想系統學習大資料知識,在觀看視訊編寫程式碼的時候,在資料排序的時候,出現了一些問題,一致於弄了好久才找到原因,現在記錄下來,方便檢視
資料輸入格式:
按照我的程式碼邏輯,應該輸出資料為
在程式碼處理時,計算結果卻是
沒有輸出輸入的資料,而是輸出
最後儲存在HDFS上的資料只是
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15
16 16
17 17
我猜測是後面的資料覆蓋了前面的寫入的資料
我在網上查了一下,發現自己程式碼中使用了setCombinerClass()
將這一行程式碼註釋後,執行,真能跑出結果,然後就查setCombinerClass的用法:
是同時使用了setCombineClass()和setReducerClass()造成的
完整程式碼示例:
import org.apache.hadoop.conf.Configuration; 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.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser; import java.io.IOException; /** * FileName: SortedData * Author: hadoop * Email: [email protected] * Date: 18-10-6 上午10:54 * Description: * 數字排序 */ public class SortedData { /** * 使用Mapper將資料檔案中的資料本身作為Mapper輸出的key直接輸出 */ public static class forSortedMapper extends Mapper<Object, Text, IntWritable, IntWritable> { private IntWritable mapperValue = new IntWritable(); //存放key的值 public void map(Object key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); //獲取讀取的值,轉化為String mapperValue.set(Integer.parseInt(line)); //將String轉化為Int型別 context.write(mapperValue,new IntWritable(1)); //將每一條記錄標記為(key,value) key--數字 value--出現的次數 //每出現一次就標記為(number,1) } } /** * 使用Reducer將輸入的key本身作為key直接輸出 */ public static class forSortedReducer extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable>{ private IntWritable postion = new IntWritable(1); //存放名次 @Override protected void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { for (IntWritable item :values){ //同一個數字可能出多次,就要多次並列排序 context.write(postion,key); //寫入名次和具體數字 System.out.println(postion + "\t"+ key); postion = new IntWritable(postion.get()+1); //名次加1 } } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); //設定MapReduce的配置 conf.set("mapred.job.tracker", "192.168.1.108:9000"); String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs(); if(otherArgs.length < 2){ System.out.println("Usage: SortedData <in> [<in>...] <out>"); System.exit(2); } //設定作業 //Job job = new Job(conf); Job job = Job.getInstance(conf); job.setJarByClass(SortedData.class); job.setJobName("SortedData"); //設定處理map,reduce的類 job.setMapperClass(forSortedMapper.class); job.setReducerClass(forSortedReducer.class); //設定輸入輸出格式的處理 job.setOutputKeyClass(IntWritable.class); job.setOutputValueClass(IntWritable.class); //設定輸入輸出路徑 for (int i = 0; i < otherArgs.length-1;++i){ FileInputFormat.addInputPath(job,new Path(otherArgs[i])); } FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length-1])); System.exit(job.waitForCompletion(true)?0:1); } }