设计思路:该题处理方案和MapReduce案例学习(8) 列出工资最高的头三名员工姓名及其工资 类似的,只要将employee类中的compareTo方法改写比较规则即可
map阶段:将employee对象作为key,value直接设置为NullWritable
reduce阶段:在对reduce的输入参数value进行遍历时,里面的对象都是根据key自动排好序的,所以直接把相关信息拼接输出。
package week06;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.WritableComparable;
/*
* Employee Object
*/
public class Employee implements WritableComparable<Employee> {
private String empno;
private String ename;
private String job;
private String mgr;
private String hiredate;
private int sal;
private int comm;
private String deptno;
private String dname;
private String loc;
private boolean valid = true;
public void set(Employee emp) {
this.valid = emp.isValid();
this.empno = emp.getEmpno();
this.ename = emp.getEname();
this.job = emp.getJob();
this.mgr = emp.getMgr();
this.hiredate = emp.getHiredate();
this.sal = emp.getSal();
this.comm = emp.getComm();
this.deptno = emp.getDeptno();
this.dname = emp.getDname();
this.loc = emp.getLoc();
}
//Emp_Test8使用这个compareTo方法
// public int compareTo(Employee bean) {
// if (this.sal >= bean.getSal()) {
// return -1;
// } else {
// return 1;
// }
// }
//Emp_Test9使用这个compareTo方法
public int compareTo(Employee bean) {
int total = this.sal + this.comm;
int bean_total = bean.getSal() + bean.getComm();
if (total >= bean_total) {
return -1;
} else {
return 1;
}
}
public void write(DataOutput out) throws IOException {
out.writeUTF(empno);
out.writeUTF(ename);
out.writeUTF(job);
out.writeUTF(mgr);
out.writeUTF(hiredate);
out.writeInt(sal);
out.writeInt(comm);
out.writeUTF(deptno);
out.writeUTF(dname);
out.writeUTF(loc);
out.writeBoolean(val