MapReduce案例学习(1)求各个部门的总工资

这篇博客介绍了如何使用MapReduce进行案例实践,通过定义employee类,然后在map阶段以部门为key,工资为value处理数据。在reduce阶段,对相同部门的工资进行累加,从而得出每个部门的总工资。
摘要由CSDN通过智能技术生成

为了方便employee对象的引用,定义了一个employee类:

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(valid);
	}


	// 注意必须和write方法中的写入顺序和类型保持一致,否则会出错。
	public void readFields(DataInput in) throws IOException {
		this.empno = in.readUTF();
		this.ename = in.readUTF();
		this.job = in.readUTF();
		this.mgr = in.readUTF();
		this.hiredate = in.readUTF();
		this.sal = in.readInt();
		this.comm = in.readInt();
		
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值