通过MapReduce计算拉勾网工作经验的要求分布百分比
时间: 2024-03-14 11:46:05 浏览: 16
假设我们有一个数据集,其中存储了拉勾网上的招聘信息,包括职位名称和工作经验要求。我们可以使用MapReduce计算出工作经验要求分布的百分比,步骤如下:
1. Map阶段:将数据集中每一条记录中的工作经验要求作为Key,将Value设为1,表示出现了一次该工作经验要求。
2. Reduce阶段:将Map阶段输出的结果按Key聚合,得到每种工作经验要求出现的次数。
3. 计算百分比:将Reduce阶段输出的结果进行处理,计算每种工作经验要求出现的百分比。
下面是Java伪代码:
Map阶段:
```
public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text experience = new Text();
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] fields = value.toString().split(",");
String exp = fields[1]; // 工作经验要求在第二个字段中
experience.set(exp);
context.write(experience, one);
}
}
```
Reduce阶段:
```
public static class Reduce extends Reducer<Text, IntWritable, Text, DoubleWritable> {
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int count = 0;
for (IntWritable value : values) {
count += value.get();
}
context.write(key, new DoubleWritable(count));
}
}
```
计算百分比:
```
public static class PercentageMapper extends Mapper<Text, DoubleWritable, Text, DoubleWritable> {
private double totalCount;
protected void setup(Context context) throws IOException, InterruptedException {
Configuration conf = context.getConfiguration();
totalCount = conf.getDouble("totalCount", 0.0);
}
public void map(Text key, DoubleWritable value, Context context) throws IOException, InterruptedException {
double percentage = (value.get() / totalCount) * 100;
context.write(key, new DoubleWritable(percentage));
}
}
```
最后,我们需要在驱动程序中设置总记录数并启动作业:
```
public static void main(String[] args) throws Exception {
Job job1 = Job.getInstance(new Configuration());
job1.setJobName("Experience Distribution");
job1.setOutputKeyClass(Text.class);
job1.setOutputValueClass(IntWritable.class);
job1.setMapperClass(Map.class);
job1.setReducerClass(Reduce.class);
FileInputFormat.setInputPaths(job1, new Path(args[0]));
FileOutputFormat.setOutputPath(job1, new Path(args[1]));
job1.waitForCompletion(true);
long totalCount = job1.getCounters().findCounter(TaskCounter.MAP_INPUT_RECORDS).getValue();
Job job2 = Job.getInstance(new Configuration());
job2.setJobName("Experience Percentage");
job2.setOutputKeyClass(Text.class);
job2.setOutputValueClass(DoubleWritable.class);
job2.setMapperClass(PercentageMapper.class);
job2.getConfiguration().setDouble("totalCount", totalCount);
FileInputFormat.setInputPaths(job2, new Path(args[1]));
FileOutputFormat.setOutputPath(job2, new Path(args[2]));
job2.waitForCompletion(true);
}
```
以上代码中,我们假设输入文件的路径为args[0],第一次输出文件的路径为args[1],第二次输出文件的路径为args[2]。在第一次作业中,我们使用MapReduce计算出每种工作经验要求出现的次数;在第二次作业中,我们使用MapReduce计算出每种工作经验要求出现的百分比,并将总记录数传递给Mapper通过setup()方法。最后,我们可以在输出文件中查看工作经验要求的分布情况及其百分比。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)