hadoop入门 使用MapReduce编写自定义outputformat(十)

1、继承RecordWriter重写write和close方法

package com.example.hadoop.outputformat;

import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

import java.io.IOException;

public class LogRecordWriter extends RecordWriter<Text, NullWritable> {
   private FSDataOutputStream outStream;
   private FSDataOutputStream otherStream;

    public LogRecordWriter(TaskAttemptContext job)  {
        try {
            FileSystem fs=FileSystem.get(job.getConfiguration());
            outStream = fs.create(new Path("D:\\hadoop\\output12\\out.log"));
            otherStream= fs.create(new Path("D:\\hadoop\\output12\\other.log"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void write(Text key, NullWritable nullWritable) throws IOException, InterruptedException {
        String log=key.toString();
        //具体写
        if(log.contains("139")||log.contains("138")||log.contains("135")){
            outStream.writeBytes(log+"\n");
        }else{
            otherStream.writeBytes(log+"\n");
        }
    }

    @Override
    public void close(TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
        //关闭流
        IOUtils.closeStream(outStream);
        IOUtils.closeStream(otherStream);
    }
}

2、继承FileOutputFormat重写getRecordWriter

package com.example.hadoop.outputformat;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class LogOutputFormat extends FileOutputFormat<Text, NullWritable> {
    @Override
    public RecordWriter<Text, NullWritable> getRecordWriter(TaskAttemptContext job) throws IOException, InterruptedException {
        LogRecordWriter lrw=new LogRecordWriter(job);
        return lrw;
    }
}

3、mapper

package com.example.hadoop.outputformat;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class LogMapper extends Mapper<LongWritable, Text,Text, NullWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        context.write(value,NullWritable.get());
    }
}

4、reducer

package com.example.hadoop.outputformat;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class LogReducer extends Reducer<Text, NullWritable, Text,NullWritable> {
    @Override
    protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        //防止有相同数据丢数据
        for (NullWritable value:values){
            context.write(key,NullWritable.get());
        }
    }
}

5、main方法

package com.example.hadoop.outputformat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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 java.io.IOException;

public class LogDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //1、获取job
        Configuration conf = new Configuration();
        Job job= Job.getInstance(conf);
        //2、设置jar包途径
        job.setJarByClass(LogDriver.class);
        //3、关联mapper和reducer
        job.setMapperClass(LogMapper.class);
        job.setReducerClass(LogReducer.class);
        //4、设置map输出的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);
        //5、设置最终输出的kv类型
        job.setOutputKeyClass(Text.class);
        job.setMapOutputValueClass(NullWritable.class);
        //设置自定义outputformat
        job.setOutputFormatClass(LogOutputFormat.class);
        //6、设置输入路径和输出路径
        FileInputFormat.setInputPaths(job,new Path("D:\\flow.txt"));
        FileOutputFormat.setOutputPath(job,new Path("D:\\hadoop\\output12"));
        //7、提交job
        boolean result=job.waitForCompletion(true);

        System.exit(result?0:1);
    }
}

在这里插入图片描述

6、flow文档

1	13736230513	192.196.100.1	www.hadoop.com	2481	24681	200
2	13846544121	192.196.100.2	264	0	200
3	13956435636	192.196.100.3	132	1512	200
4	13966251146	192.168.100.1	240	0	404
5	18271575951	192.168.100.2	www.hadoop.com	1527	2106	200
6	18418841312	192.168.100.3	www.hadoop.com	4116	1432	200
7	13590439668	192.168.100.4	1116	954	200
8	15910133277	192.168.100.5	www.hao123.com	3156	2936	200
9	13729199489	192.168.100.6	240	0	200
10	13630577991	192.168.100.7	www.shouhu.com	6960	690	200
11	15043685818	192.168.100.8	www.baidu.com	3659	3538	200
12	15959002129	192.168.100.9	www.hadoop.com	1938	180	500
13	13560439638	192.168.100.10	918	4938	200
14	13470253144	192.168.100.11	180	180	200
15	13682846555	192.168.100.12	www.qq.com	1938	2910	200
16	13992314666	192.168.100.13	www.gaga.com	3008	3720	200
17	13509468723	192.168.100.14	www.qinghua.com	7335	110349	404
18	18390173782	192.168.100.15	www.sogou.com	9531	2412	200
19	13975057813	192.168.100.16	www.baidu.com	11058	48243	200
20	13768778790	192.168.100.17	120	120	200
21	13568436656	192.168.100.18	www.alibaba.com	2481	24681	200
22	13568436656	192.168.100.19	1116	954	200
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值