MapReduce案例学习(2) 求各个部门的人数和平均工资

本文通过MapReduce实现对各部门员工记录的处理,Map阶段按部门输出工资,Reduce阶段计算部门总工资与人数,从而得出部门平均工资。
摘要由CSDN通过智能技术生成

设计思路:

map阶段:map读取每行记录,将部门作为key,工资作为value输出;

reduce阶段:将相同的key即同部门的工资作叠加运算得出总工资,同时在遍历value时,定义一个计数变量,统计该部门的人员数,最后总工资除以人员数得出该部门的平均工资。

package week06;

import java.io.IOException;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

// 2) 求各个部门的人数和平均工资
public class Emp_Test2 extends Configured implements Tool {

	/**
	 * 计数器 用于计数各种异常数据
	 */
	enum Counter {
		LINESKIP,
	}

	/**
	 * M
可以使用MapReduce实现该功能,具体实现步骤如下: 1. Map阶段:将输入的员工信息按照部门编号作为key,员工姓名和工资作为value进行映射。 ```java public static class Map extends Mapper<LongWritable, Text, Text, Text> { private Text outKey = new Text(); private Text outValue = new Text(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] fields = value.toString().split(","); String deptNo = fields[0]; String empName = fields[1]; String salary = fields[2]; outKey.set(deptNo); outValue.set(empName + ":" + salary); context.write(outKey, outValue); } } ``` 2. Reduce阶段:对于每个部门,遍历其所有员工的工资,找出最高工资,并将其对应的员工姓名输出。 ```java public static class Reduce extends Reducer<Text, Text, Text, Text> { private Text outValue = new Text(); @Override protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException { String maxSalaryEmpName = ""; int maxSalary = Integer.MIN_VALUE; for (Text value : values) { String[] fields = value.toString().split(":"); String empName = fields[0]; int salary = Integer.parseInt(fields[1]); if (salary > maxSalary) { maxSalary = salary; maxSalaryEmpName = empName; } } outValue.set(maxSalaryEmpName); context.write(key, outValue); } } ``` 3. Driver阶段:设置MapReduce的输入路径、输出路径、Mapper、Reducer等信息,并提交作业。 ```java public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "max salary by department"); job.setJarByClass(MaxSalaryByDepartment.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } ``` 这样就可以通过MapReduce各个部门最高工资的员工姓名了,而且不需要调用get方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值