Blog
hadoop-18 数据清洗 ETL
ETL:Extract-Transform-Load 用来描述将数据从来源端警告抽取Extract 转换Transform 加载Load至目的端的过程
清理的过程只需要运行Mapper程序,不需要运行Reduce程序
- WebLogMapper.java
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 WebLogMapper extends Mapper<LongWritable, Text, Text, NullWritable> {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// 获取一行数据
String line = value.toString();
// ETL
boolean result = parseLog(line);
if (!result) {
return;
}
// 写出
context.write(value, NullWritable.get());
}
private boolean parseLog(String line) {
String[] fields = line.split(" ");
// 判断日志长度是否大于11
if (fields.length > 11) {
return true;
} else {
return false;
}
}
}
WebLogDriver.java
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;
import java.io.IOException;
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.setMapOutputValueClass(NullWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
job.setNumReduceTasks(0);
FileInputFormat.setInputPaths(job, new Path("E:\\hadoop\\webloginput"));
FileOutputFormat.setOutputPath(job, new Path("E:\\hadoop\\weblogoutput"));
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}