MapReduce根据不同要求将结果输出不同命名文件

  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();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值