微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

数据清洗

数据清洗

什么是ETL

ETL,是英文Extract-Transform-Load的缩写,用来描述将数据从来源端经过抽取(extract)、转换(transform)、加载(load)至目的端的过程

ETL是将业务系统的数据经过抽取、清洗转换之后加载到数据仓库的过程,目的是将企业中的分散、零乱、标准不统一的数据整合到一起,为企业的决策提供分析依据

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


实例

需求

去除日志中字段长度小于等于11的日志。


输入

194.237.142.21 - - [18/Sep/2013:06:49:18 +0000] "GET /wp-content/uploads/2013/07/rstudio-git3.png HTTP/1.1" 304 0 "-" "Mozilla/4.0 (compatible;)"
183.49.46.228 -
163.177.71.12 - - [18/Sep/2013:06:49:33 +0000] "HEAD / HTTP/1.1" 200 20 "-" "DNSPod-Monitor/1.0"

编写LogMapper类

package com.dev1.weblog;

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;
//ETL 抽取 清洗 转换  加载
//清洗    去除掉长度<=11的log
//int countTrue = 0;
//int countFalse = 0;
public class LogMapper extends Mapper<LongWritable, Text,Text, NullWritable> {//key、value不需要是用NullWritable来填补
    private Text text=new Text();
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line=value.toString();
        boolean result=parseLog(line);
        if(result){
            text.set(line);
            context.write(text,NullWritable.get());//key、value不需要是用NullWritable来填补

        }else{
            //什么也不处理
        }
    }

    private boolean parseLog(String line) {
        //判断当前log的长度是>=11的吗?  ["a","BCD"]
        //当前的line 通过 split切割,得到数组的元素>11
        String[] arr=line.split(" ");
        if(arr.length>11){
            return true;
        }else
            return false;
    }
}


编写LogDriver 类

package com.dev1.weblog;

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

public class LogDriver {
    public static void main(String[] args) throws Exception {
        // 输入输出路径需要根据自己电脑上实际的输入输出路径设置
//        args = new String[] { "e:/input/inputlog", "e:/output1" };


        // 1 获取job信息
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);


        // 2 加载jar包
        job.setJarByClass(LogDriver.class);


        // 3 关联map
        job.setMapperClass(LogMapper.class);


        // 4 设置最终输出类型
        job.setoutputKeyClass(Text.class);
        job.setoutputValueClass(NullWritable.class);


        // 设置reducetask个数为0
        job.setNumReduceTasks(0);//***********注意


        // 5 设置输入和输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setoutputPath(job, new Path(args[1]));


        // 6 提交
        job.waitForCompletion(true);
    }
}


输出

在这里插入图片描述


计数器

package com.dev1.weblog;
//ETL 抽取 清洗 转换  加载
//清洗    去除掉长度<=11的log
//int countTrue = 0;
//int countFalse = 0;
public class LogMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
    @Override
  protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //读取行
  String line = value.toString();
        //调用一个判断长度的方法
  boolean result = parseLog(line);

        if(result){
            //写到磁盘
  Text text = new Text();
            text.set(line);
            context.write(text,NullWritable.get());
            context.getCounter("etl","countTrue").increment(1);//计数器添加
        }else{
            //什么也不处理
  context.getCounter("etl","countFalse").increment(1);//计数器添加
        }
    }

    private boolean parseLog(String line) {
        //判断当前log的长度是>=11的吗?  ["a","BCD"]
 //当前的line 通过 split切割,得到数组的元素>11
  String[] arr = line.split(" ");
        if( arr . length >11){
            return true;
        }else{
            return false;
        }
    }
}



输出

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐