ETL数据清洗

1.需求:数据来源各种各样,大量的数据中难免会有脏数据,我们需要将脏数据清洗掉,提高数据的准确度。

本次要将字段缺失的数据过滤掉,只留下保存度完整的数据。

2.项目开发:

  (1)清洗数据类:

package com.xnmzdx.mapreduce.etl;

import java.io.IOException;

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

public class LogMapper extends Mapper<LongWritable, Text, Text, NullWritable> {

    
    @Override
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
        //1 获取一行数据
        String line = value.toString();
        
        //2 解析日志
        boolean result = parseLog(line,context);
        
        //3 判断日志符合不符合要求
        if(!result){
            return;
        }
        
        //4 写出数据
        context.write(value, NullWritable.get());
        
    }

    private boolean parseLog(String line, Context context) {
        //1 截取数据
        String[] split = line.split(" ");
        //2 日志长度大于11为符合要求
        if(split.length > 11){
            //计算器
            context.getCounter("LogMapper", "parseLog true").increment(1);
            return true;
        }else{
            //计算器
            context.getCounter("LogMapper", "parseLog false").increment(1);
            return false;
        }
    }

}


         (2)驱动类:

package com.xnmzdx.mapreduce.etl;

import java.io.IOException;
import java.net.URI;

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 LogDrive {

    static{ //static代码块说明:windows中hadoop的问题,无法找到dll文件,所以提前加载一下,hadoop正常的,代码块删除即可
        System.load("E:\\tools\\hadoop2.7.2_win10\\bin\\hadoop.dll");
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        
        args = new String[]{"D:/DSJ_ceshi/web.log","D:/DSJ_ceshi/inputlogout2"};
        
        // 1 获取配置信息,创建job
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);

        // 2 指定本地程序jar包所在的路径
        job.setJarByClass(LogDrive.class);

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

        // 5 指定最终输出的K,V类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        // 设置reduce个数为0,就没有reduce阶段了
        job.setNumReduceTasks(0);

        // 6 指定job的输入和输出目录
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        // 7 提交job
        // job.submit();
        // 等待job完成
        job.waitForCompletion(true);
    }

}

发布了26 篇原创文章 · 获赞 35 · 访问量 740

猜你喜欢

转载自blog.csdn.net/csdnliu123/article/details/105497556
今日推荐