MapReduce修改输出的文件名

时间:2022-01-26 07:06:15

MapReduce默认输出的文件名称格式如下:part-r-00000

自定义名称,比如editName,则输出的文件名称为:editName-r-0000,此方法没有彻底修改整个文件名,只修改了一部分

方法如下

重写TextOutPutFormat的setOutPutName方法,因为setOutPutName是protected方法,所以只能通过重写的方式来修改

代码如下

/**
*
*/
package com.zhen.outPutName; import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.JobContext;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; /**
* @author FengZhen
* setOutputName是protected方法,所以无法直接调用,只能自定义TextOutPutFormat重写该方法
*/
public class MyOutPutFormat extends TextOutputFormat<Text, IntWritable>{ protected static void setOutputName(JobContext job, String name) {
job.getConfiguration().set(BASE_OUTPUT_NAME, name);
}
}
/**
*
*/
package com.zhen.outPutName; import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; /**
* @author FengZhen
* hadoop jar /Users/FengZhen/Desktop/Hadoop/other/mapreduce_jar/OutPutNameTest.jar com.zhen.outPutName.OutPutNameTest /user/hadoop/mapreduce/combinerTest/input /user/hadoop/mapreduce/OutPutNameTest/output/
*/
public class OutPutNameTest { public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration configuration = new Configuration();
Job job = new Job(configuration, OutPutNameTest.class.getSimpleName());
job.setJarByClass(OutPutNameTest.class);
job.setMapperClass(MapTest.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class); job.setReducerClass(ReduceTest.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//设置job的输出类型
job.setOutputFormatClass(MyOutPutFormat.class); job.setCombinerClass(ReduceTest.class);
job.setPartitionerClass(PartitionTest.class);
job.setNumReduceTasks(); //结果名称如下:editName-r-00000,此方法只能修改part这一段
MyOutPutFormat.setOutputName(job, "editName"); FileInputFormat.addInputPath(job, new Path(args[]));
FileOutputFormat.setOutputPath(job, new Path(args[])); System.exit(job.waitForCompletion(true)?:);
} }

Map Reduce两个类省略。