-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathQ6HigherThanAveSalary.java
102 lines (80 loc) · 3.42 KB
/
Q6HigherThanAveSalary.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.elon33.mr1;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
public class Q6HigherThanAveSalary extends Configured implements Tool {
public static class MapClass extends Mapper<LongWritable, Text, IntWritable, Text> {
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String[] kv = value.toString().split(",");
// 输出<0,每个人的工资> 用于计算平均工资
context.write(new IntWritable(0), new Text(kv[5]));
// 输出<1,该员工姓名+员工工资> 用于和平均工资比较
context.write(new IntWritable(1), new Text(kv[1] + "," + kv[5]));
}
}
public static class Reduce extends Reducer<IntWritable, Text, Text, Text> {
private long allSalary = 0;
private int allEmpCount = 0;
private long aveSalary = 0;
private long empSalary = 0;
public void reduce(IntWritable key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
for (Text val : values) {
// 得到工资总数
if (0 == key.get()) {
allSalary += Long.parseLong(val.toString());
allEmpCount++;
System.out.println("allEmpCount = " + allEmpCount);
} else if (1 == key.get()) {
if (aveSalary == 0) {
aveSalary = allSalary / allEmpCount; // 计算平均工资
context.write(new Text("Average Salary = "), new Text("" + aveSalary));
context.write(new Text("Following employees have salarys higher than Average:"), new Text(""));
}
// System.out.println("Employee salary = " + val.toString());
// aveSalary = allSalary / allEmpCount;
empSalary = Long.parseLong(val.toString().split(",")[1]);
if (empSalary > aveSalary) {
context.write(new Text(val.toString().split(",")[0]), new Text("" + empSalary));
}
}
}
}
}
@Override
public int run(String[] args) throws Exception {
Job job = new Job(getConf(), "Q6HigherThanAveSalary");
job.setJobName("Q6HigherThanAveSalary");
job.setJarByClass(Q6HigherThanAveSalary.class);
job.setMapperClass(MapClass.class);
job.setReducerClass(Reduce.class);
job.setNumReduceTasks(1);
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(Text.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
String[] otherArgs = new GenericOptionsParser(job.getConfiguration(), args).getRemainingArgs();
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
job.waitForCompletion(true);
return job.isSuccessful() ? 0 : 1;
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new Configuration(), new Q6HigherThanAveSalary(), args);
System.exit(res);
}
}