MapReduce默认输出的文件命名为part-0000。日常业务开发情况需要根据不同的结果文件名,来辨认不同输出需求数据,因此,默认输出格式不足以满足需求。我们需要对MapReduce的文件输出做特殊的设置。
一、job端
MultipleOutputs.addNamedOutput(job,"corpInfo",TextOutputFormat.class,Text.class,Text.class);
MultipleOutputs.setCountersEnabled(job,true);
// 注:设置的别名只是标识输出格式,key格式,value格式的一个类变量。
// 即表示,如果你要输出的文件有多种不同的格式标识,则设置多个别名以标示输出格式。
二、Reduce端
private MultipleOutputs outs;
@Override
protected void setup(Context context){
outs=new MultipleOutputs(context);
}
if (flag&&flagTotal){
Text text = new Text();
for (String val : list){
text.set(val);
// context.write(text, NullWritable.get());
outs.write("corpInfo",text, NullWritable.get(),"NotMissingEvidence");
} }
if (flag&&!flagTotal){
Text text = new Text();
for (String val : list){
text.set(val);
outs.write("corpInfo",text, NullWritable.get(),"MissingEvidence");
} }
public void cleanup(Context context) throws IOException,InterruptedException{
super.cleanup(context);
outs.close();
}
源码如下:
Job端
MultipleOutputs.addNamedOutput(job,"corpInfo",TextOutputFormat.class,Text.class,Text.class);
MultipleOutputs.setCountersEnabled(job,true);
// 注:设置的别名只是标识输出格式,key格式,value格式的一个类变量。
// 即表示,如果你要输出的文件有多种不同的格式标识,则设置多个别名以标示输出格式。
Map端
public class EvidenceMapper extends Mapper<LongWritable, Text, Text, Text> {
int collectTargetIdNum;
String flag = "1";
String vl;
@Override
protected void setup(Context context) throws IOException, InterruptedException {
Configuration configuration = context.getConfiguration();
collectTargetIdNum = Integer.valueOf(configuration.get("collectTargetIdNum"));
}
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] split = value.toString().split("\t", -1);
vl=value.toString();
context.write(new Text(split[collectTargetIdNum-1]),new Text(flag+"\t"+vl));
}
}
public class TotalEvidenceMapper extends Mapper<LongWritable, Text, Text, Text> {
int collectTargetToralIdNum;
String flag = "2";
String vl ;
@Override
protected void setup(Context context) throws IOException, InterruptedException {
Configuration configuration = context.getConfiguration();
collectTargetToralIdNum = Integer.valueOf(configuration.get("collectTargetToralIdNum"));
}
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
StringBuilder sb = new StringBuilder();
String[] split = value.toString().split("\t", -1);
sb.append("2").append("\t").append(value.toString());
// vl="2"+"\t"+value.toString();
context.write(new Text(split[collectTargetToralIdNum-1]),new Text(sb.toString()));
}
}
Reduce端
public class MissingEvidenceReduce extends Reducer<Text, Text, Text, NullWritable> {
private MultipleOutputs outs;
@Override
protected void setup(Context context){
outs=new MultipleOutputs(context);
}
public void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
boolean flag = false;
boolean flagTotal = false ;
List<String> list = new ArrayList<String>();
for(Text val: values){
String[] split = val.toString().split("\t", -1);
if (split[0].equals("1"))
{ flag = true;
}
if (split[0].equals("2"))
{ flagTotal = true;
}
list.add(val.toString());
}
if (flag&&flagTotal){
Text text = new Text();
for (String val : list){
text.set(val);
// context.write(text, NullWritable.get());
outs.write("corpInfo",text, NullWritable.get(),"NotMissingEvidence");
} }
if (flag&&!flagTotal){
Text text = new Text();
for (String val : list){
text.set(val);
outs.write("corpInfo",text, NullWritable.get(),"MissingEvidence");
} }
}
public void cleanup(Context context) throws IOException,InterruptedException{
super.cleanup(context);
outs.close();
}
}