public static class MyReduce extends Reducer<Text,Text,Text,Text>{
public static Text keyout = new Text();
public static Text valout = new Text();
private MultipleOutputs<Text,Text> mos;
<pre name="code" class="java"> <span style="color:#FF0000;">//使用输入的上下文创建MultipleOutputs 实例</span>
public void setup(Context context) throws IOException, InterruptedException{
mos = new MultipleOutputs(context);
}
public void reduce(Text key,Iterable<Text> values,Context context) throws IOException, InterruptedException{
int tmplen = 0;
String tmpval = "";
for(Text val:values){
int tmpvallen = val.toString().length();
if(tmpvallen >tmplen){
tmplen = tmpvallen;
tmpval = val.toString();
}
}
keyout.set(key);
valout.set(tmpval);
<span style="color:#FF0000;"> //输出格式===key.toString().split(",").length+"" -m - nnnnn</span>
mos.write(keyout, valout, key.toString().split(",").length+"");
//context.write(keyout, valout);
}
//一定要close(),否则完全没有输出 public void cleanup(Context context) throws IOException, InterruptedException{ mos.close();}}
MultipleOutputs可以在Mapper或Reducer中使用,使用时需要在map()或reduce()中的setup()方法里面创建MultipleOutputs实例,还需要在cleanup()中关闭输出。