您好,登錄后才能下訂單哦!
本篇文章為大家展示了Hadoop中怎么自定義輸出排序,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
package com.hgs; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.DoubleWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.io.WritableComparable; 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; public class AvgValue { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { if(args.length!=2) { System.err.println("Usage: MaxTemperature <input path> <output path>"); System.exit(1); } Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "avg of grades"); job.setJarByClass(AvgValue.class); job.setMapperClass(InputClass.class); job.setReducerClass(OutputClass.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(DoubleWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true)?0:1); } } class InputClass extends Mapper<LongWritable, Text, Text, DoubleWritable>{ @Override protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, DoubleWritable>.Context context) throws IOException, InterruptedException { String line = value.toString(); if(line.length()>0){ String[] array = line.split("\t"); if(array.length==2){ String name=array[0]; int grade = Integer.parseInt(array[1]); context.write(new Text(name), new DoubleWritable(grade)); } } } } class OutputClass extends Reducer<Text, DoubleWritable, NameKey, DoubleWritable>{ @Override protected void reduce(Text text, Iterable<DoubleWritable> iterable, Reducer<Text, DoubleWritable, NameKey, DoubleWritable>.Context context) throws IOException, InterruptedException { int sum = 0; int cnt= 0 ; for(DoubleWritable iw : iterable) { sum+=iw.get(); cnt++; } NameKey nk = new NameKey(text,new DoubleWritable(sum/cnt)); context.write(nk, new DoubleWritable(sum/cnt)); } } //該處通過將輸出記過封裝為一個bean并且實現(xiàn)WritableComparable類,重寫compareTo,來實現(xiàn)對自定義排序 class NameKey implements WritableComparable<NameKey>{ private Text name ; private DoubleWritable grade ; public NameKey(Text name,DoubleWritable grade) { this.name = name; this.grade = grade; } public Text getName() { return name; } public void setName(Text name) { this.name = name; } public DoubleWritable getGrade() { return grade; } public void setGrade(DoubleWritable grade) { this.grade = grade; } @Override public void write(DataOutput out) throws IOException { name.write(out); grade.write(out); } @Override public void readFields(DataInput in) throws IOException { name.readFields(in); grade.readFields(in); } @Override public String toString() { return name.toString(); } @Override public int compareTo(NameKey o) { double me = grade.get(); double other = o.getGrade().get(); int slid = (int)(me-other); return slid; } } //class Maxreducer extends Reducer
上述內(nèi)容就是Hadoop中怎么自定義輸出排序,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。