您的位置:首页 > 其它

MapReduce多文件输出

2015-07-11 11:05 337 查看
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()中关闭输出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: