ETL
数据从来源端经过抽取(Extract)、转换(Transform)、加载(Load)至目的端的过程 清理的过程往往只需要运行 Mapper 程序,不需要运行 Reduce 程序。
案例
将含有空字段的行清洗
编写ETLMapper类
package ETL;
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 ETLMapper 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, context);
if (!result){
return;
}
context.write(value,NullWritable.get());
}
private boolean parselog(String line, Context context) {
String[] split = line.split("\t");
if (split.length > 5){
return true;
}else {
return false;
}
}
}
编写ETLDriver类
package ETL;
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 ETLDriver {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration);
job.setJarByClass(ETLDriver.class);
job.setMapperClass(ETLMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(NullWritable.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
job.setNumReduceTasks(0);
FileInputFormat.setInputPaths(job, new Path("E:\\data.log"));
FileOutputFormat.setOutputPath(job, new Path("E:\\outdata_log"));
boolean b = job.waitForCompletion(true);
System.exit(b? 0:1);
}
}
清理了三行数据
|