博客
关于我
【MapReduce】招聘数据清洗
阅读量:337 次
发布时间:2019-03-04

本文共 4173 字,大约阅读时间需要 13 分钟。

招聘数据清洗

1. 数据集

提供了一份招聘信息数据集,以下是清洗条件:

  • 包含有两条含有空值的数据
  • 两条重复的数据集

2. 清洗目标

  • 去除数据的首行字段记录
  • 对含有空值的记录进行去除
  • 对记录进行去重
  • 对薪资进行处理,将其结果展示为最高薪资与最低薪资的均值

3. 思路

  • 去除数据的首行字段记录:通过字符串比较开头,选择性忽略
  • 对含有空值的记录进行去除:使用isEmpty()方法
  • 对记录进行去重:利用Reduce阶段Key的特性
  • 对薪资进行处理:字符串截取,类型转换计算

4. 代码执行

Mapper阶段

package 招聘数据清洗;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;import org.apache.hadoop.mapreduce.Mapper;public class Map extends Mapper
{ @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { // 忽略包含字段名的记录 if (value.toString().startsWith("\uFEFFpositionName")) { return; } // 拆分字段 String[] fields = value.toString().split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1); // 去空 if (valid(fields)) { // 处理薪资 if (fields[1].contains("*")) { String[] salary = fields[1].split("\\*"); String[] salarys = salary[0].split("-"); int max = Integer.parseInt(salarys[1].trim().substring(0, salarys[1].length() - 1)) * Integer.parseInt(salary[1]); int min = Integer.parseInt(salarys[0].trim().substring(0, salarys[0].length() - 1)); fields[1] = ((max + min) / 2) + "k"; } else { String[] salary = fields[1].split("-"); int max = Integer.parseInt(salary[1].trim().substring(0, salary[1].length() - 1)); int min = Integer.parseInt(salary[0].trim().substring(0, salary[0].length() - 1)); fields[1] = ((max + min) / 2) + "k"; } // 输出结果 StringBuffer sb = new StringBuffer(); for (int i = 0; i < fields.length; i++) { sb.append(fields[i]).append("\t"); } context.write(new Text(sb.toString()), NullWritable.get()); } } private boolean valid(String[] fields) { for (String item : fields) { if (item.trim().isEmpty()) { return false; } } return true; }}

Reducer阶段

package 招聘数据清洗;import org.apache.hadoop.io.NullWritable;import org.apache.hadoop.io.Text;import org.apache.hadoop.mapreduce.Reducer;import java.io.IOException;public class Reduce extends Reducer
{ @Override protected void reduce(Text key, Iterable
values, Context context) throws IOException, InterruptedException { context.write(key, NullWritable.get()); }}

Driver阶段

package 招聘数据清洗;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FileSystem;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 Driver {    public static void main(String[] args) throws Exception {        Configuration conf = new Configuration();        Job job = Job.getInstance(conf);        job.setMapperClass(Map.class);        job.setReducerClass(Reduce.class);        job.setJarByClass(Driver.class);        job.setMapOutputKeyClass(Text.class);        job.setMapOutputValueClass(NullWritable.class);        job.setOutputKeyClass(Text.class);        job.setOutputValueClass(NullWritable.class);        Path in = new Path("G:\\Projects\\IdeaProject-C\\MapReduce\\src\\main\\java\\招聘数据清洗\\data\\zhaopin.txt");        Path out = new Path("G:\\Projects\\IdeaProject-C\\MapReduce\\src\\main\\java\\招聘数据清洗\\output");        FileInputFormat.setInputPaths(job, in);        FileOutputFormat.setOutputPath(job, out);        FileSystem fs = FileSystem.get(conf);        if (fs.exists(out)) {            fs.delete(out, true);        }        System.exit(job.waitForCompletion(true) ? 0 : 1);    }}

5. 困难点

one

BOM编码格式问题

  • BOM(byte-order mark)是Unicode文件的编码类型标记
  • UTF-8文件不需要BOM,但带有BOM的文件仍需注意
  • BOM主要用于多字节编码文件(如UTF-16、UTF-32)

two

字段分割问题

  • 数据中字段值可能包含多个分隔符
  • 需要使用正则表达式进行精确截取
  • 例如:" , "","的不同处理方式

6. 结果

  • 清洗后输出96条记录
  • 原始数据100条(去除1条字段,2条包含空值记录,1条重复记录)

转载地址:http://lceq.baihongyu.com/

你可能感兴趣的文章
Objective-C实现IIR数字滤波器(附完整源码)
查看>>
Objective-C实现insertion sort插入排序算法(附完整源码)
查看>>
Objective-C实现integer partition整数分区算法(附完整源码)
查看>>
Objective-C实现integerPartition整数划分算法(附完整源码)
查看>>
Objective-C实现interpolation search插值搜索算法(附完整源码)
查看>>
Objective-C实现Interpolation search插值查找算法(附完整源码)
查看>>
Objective-C实现intersection交集算法(附完整源码)
查看>>
Objective-C实现intro sort内省排序算法(附完整源码)
查看>>
Objective-C实现inversions倒置算法(附完整源码)
查看>>
Objective-C实现isalpha函数功能(附完整源码)
查看>>
Objective-C实现islower函数功能(附完整源码)
查看>>
Objective-C实现isPowerOfTwo算法(附完整源码)
查看>>
Objective-C实现isupper函数功能(附完整源码)
查看>>
Objective-C实现ItemCF算法(附完整源码)
查看>>
Objective-C实现ItemCF算法(附完整源码)
查看>>
Objective-C实现iterating through submasks遍历子掩码算法(附完整源码)
查看>>
Objective-C实现iterative merge sort迭代归并排序算法(附完整源码)
查看>>
Objective-C实现jaccard similarity相似度无平方因子数算法(附完整源码)
查看>>
Objective-C实现Julia集算法(附完整源码)
查看>>
Objective-C实现k nearest neighbours k最近邻分类算法(附完整源码)
查看>>