使用MapReduce做数据清洗(ETL)

本文介绍了如何使用MapReduce技术中的WebLogMapper类,通过编写Mapper程序来过滤日志数据,只保留字段长度大于11的部分。具体展示了如何编写Mapper和Driver类,并设置Job配置以完成数据清洗任务。
摘要由CSDN通过智能技术生成

        “ETL,是英文Extract-Transform-Load的缩写,用来描述将数据从来源端经过抽取(Extract)、转换(Transform)、加载(Load)至目的端的过程。ETL一词较常用在数据仓库,但其对象并不限于数据仓库

        在运行核心业务MapReduce程序之前,往往要先对数据进行清洗,清理掉不符合用户要求的数据。清理的过程往往只需要运行Mapper程序,不需要运行Reduce程序。

实例:去除日志中字段个数小于等于11的日志。

(1)编写WebLogMapper类

/**
 * @author LS
 * @date 2021/12/15 18:44
 * @description
 */
public class WebLogMapper extends Mapper<LongWritable,Text,Text,NullWritable> {
    //private Text outK = new Text();
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String string = value.toString();
        String[] split = string.split(" ");
        if (split.length>11){
            context.write(value,NullWritable.get());
        }
    }
}

(2)编写WebLogDriver类

/**
 * @author LS
 * @date 2021/12/15 18:45
 * @description
 */
public class WebLogDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        job.setJarByClass(WebLogDriver.class);
        job.setMapperClass(WebLogMapper.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputKeyClass(NullWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        job.setNumReduceTasks(0);

        FileInputFormat.setInputPaths(job,new Path("F:\\web.log"));
        FileOutputFormat.setOutputPath(job,new Path("F:\\etl"));

        job.waitForCompletion(true);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值