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