[01].准备工作

19.员工平均工资统计案例 - 图1

  • 前台

/stuff/dept.txt
19.员工平均工资统计案例 - 图2
/stuff/input(用不着)
19.员工平均工资统计案例 - 图3

  • 本地

19.员工平均工资统计案例 - 图4
需要这样三个员工资源,我的其实都是一样的,为了简便起见。

  • stuf_1.txt(其他三个一样或者你自己随便写)

    1. 103294 11 9033
    2. 103295 12 9033
    3. 103296 14 9033
    4. 103297 13 9033
    5. 103298 12 9033
    6. 103299 11 9033
    7. 103290 13 9033
    8. 103291 14 9033
    9. 103292 14 9033
    10. 103293 10 9033
    11. 103284 10 9033
    12. 103274 11 9033
    13. 103264 12 9033
  • dept.txt部门编号

    1. 10 暴龙奥特曼分部
    2. 11 迪迦奥特曼分部
    3. 12 艾斯奥特曼分部
    4. 13 泰罗奥特曼分部
    5. 14 赛罗奥特曼分部

    01.老师给定的数据

  • staff_01.txt

    1. 10021456 11 9239
    2. 10021457 14 7168
    3. 10021458 12 8580
    4. 10021459 11 6537
    5. 10021460 12 8735
    6. 10021461 14 7086
    7. 10021462 14 9637
    8. 10021463 10 7411
    9. 10021464 12 6506
    10. 10021465 12 9141
    11. 10021466 11 6865
    12. 10021467 12 6130
    13. 10021468 11 8075
    14. 10021469 12 8654
    15. 10021470 11 6856
    16. 10021471 13 7452
    17. 10021472 10 9514
    18. 10021473 14 7524
    19. 10021474 12 9836
    20. 10021475 11 6633
    21. 10021476 12 9724
    22. 10021477 11 6425
    23. 10021478 10 8222
    24. 10021479 10 7353
    25. 10021480 13 6945
    26. 10021481 10 8507
    27. 10021482 14 7406
    28. 10021483 11 8574
    29. 10021484 13 7790
    30. 10021485 11 6050
    31. 10021486 12 8211
    32. 10021487 11 9236
    33. 10021488 12 6108
    34. 10021489 10 6319
    35. 10021490 11 9980
    36. 10021491 14 8554
    37. 10021492 12 9969
    38. 10021493 12 9582
    39. 10021494 10 7746
    40. 10021495 13 9664
    41. 10021496 14 9905
    42. 10021497 11 7072
    43. 10021498 11 7076
    44. 10021499 10 7276
    45. 10021500 10 6263
    46. 10021501 11 8525
    47. 10021502 10 6774
    48. 10021503 10 6561
    49. 10021504 14 9383
    50. 10021505 11 7630
  • staff_02.txt

    1. 10003566 13 7953
    2. 10003567 10 7356
    3. 10003568 14 9307
    4. 10003569 11 6624
    5. 10003570 11 7515
    6. 10003571 11 7966
    7. 10003572 13 7951
    8. 10003573 10 8763
    9. 10003574 11 8924
    10. 10003575 14 8633
    11. 10003576 12 6526
    12. 10003577 12 6537
    13. 10003578 13 7605
    14. 10003579 10 8560
    15. 10003580 11 7103
    16. 10003581 10 7049
    17. 10003582 10 7395
    18. 10003583 10 9529
    19. 10003584 12 6073
    20. 10003585 14 9974
    21. 10003586 13 9958
    22. 10003587 12 6525
    23. 10003588 14 7234
    24. 10003589 12 7591
    25. 10003590 11 7978
    26. 10003591 12 8683
    27. 10003592 11 9653
    28. 10003593 13 8763
    29. 10003594 12 6858
    30. 10003595 11 9773
    31. 10003596 13 8922
    32. 10003597 14 6267
    33. 10003598 13 8810
    34. 10003599 12 7875
    35. 10003600 10 8210
    36. 10003601 12 9061
    37. 10003602 10 7606
    38. 10003603 14 7936
    39. 10003604 13 9912
    40. 10003605 14 9456
    41. 10003606 13 7133
    42. 10003607 12 7139
    43. 10003608 12 8047
    44. 10003609 10 6840
    45. 10003610 12 8338
    46. 10003611 13 7212
    47. 10003612 13 6194
    48. 10003613 13 9296
    49. 10003614 11 6890
    50. 10003615 14 6866
  • staff_03.txt

    1. 10014823 12 9139
    2. 10014824 14 8002
    3. 10014825 14 9919
    4. 10014826 10 9976
    5. 10014827 12 6790
    6. 10014828 11 7669
    7. 10014829 12 9763
    8. 10014830 11 9105
    9. 10014831 12 8688
    10. 10014832 13 7595
    11. 10014833 13 9710
    12. 10014834 14 6655
    13. 10014835 13 8521
    14. 10014836 12 9620
    15. 10014837 14 9129
    16. 10014838 13 7387
    17. 10014839 12 9356
    18. 10014840 12 6740
    19. 10014841 13 8308
    20. 10014842 10 9272
    21. 10014843 14 7584
    22. 10014844 12 6767
    23. 10014845 14 7184
    24. 10014846 10 9998
    25. 10014847 12 9744
    26. 10014848 10 8675
    27. 10014849 12 6929
    28. 10014850 10 8096
    29. 10014851 12 8619
    30. 10014852 11 8189
    31. 10014853 14 6053
    32. 10014854 10 7049
    33. 10014855 14 6184
    34. 10014856 14 9899
  • dept.txt

    1. 10 ACCOUNTING
    2. 11 RESEARCH
    3. 12 SALES
    4. 13 OPERATIONS
    5. 14 DEVELOPMENT

    [02].项目代码

    01.StaffMapper

    1. import java.io.IOException;
    2. import java.net.URISyntaxException;
    3. import java.util.HashMap;
    4. import java.util.Map;
    5. import org.apache.hadoop.io.LongWritable;
    6. import org.apache.hadoop.io.Text;
    7. import org.apache.hadoop.mapreduce.Mapper;
    8. import cn.aigamejxb.hadoop.hdfs.tools.FSManager;
    9. /*
    10. * KEYIN:默认已经拿到的key(行首的偏移量:LongWritable(相当于java的long类型))
    11. *
    12. * VALUEIN:默认分片好的内容(Text类型(相当于string))
    13. *
    14. * KEYOUT:输出的key(Text类型)
    15. *
    16. * VALUEOUT:输出的value(都是1,记作{"word",1},数字类型IntWritable(int))
    17. */
    18. public class StaffMapper extends Mapper<LongWritable, Text, Text, LongWritable> {
    19. //用map集合存放depts的数据
    20. private Map<String,String>depts = new HashMap<String,String>();
    21. @Override
    22. protected void setup(Mapper<LongWritable, Text, Text, LongWritable>.Context context)
    23. throws IOException, InterruptedException {
    24. //用之前封装好的方法,读取文件
    25. try {
    26. String content = FSManager.readContent("/stuff/dept.txt");
    27. String content_lines[] = content.split("\n");
    28. for(String line : content_lines) {
    29. if(!line.trim().equals("")) {
    30. String items[] = line.split(" ");
    31. depts.put(items[0], items[1]);
    32. }
    33. }
    34. } catch (IOException e) {
    35. e.printStackTrace();
    36. } catch (URISyntaxException e) {
    37. e.printStackTrace();
    38. }
    39. }
    40. private Text outKey = new Text();
    41. private LongWritable longValue = new LongWritable();
    42. @Override
    43. protected void map(LongWritable key, Text value,Context context)throws IOException, InterruptedException {
    44. String items[] = value.toString().split(" ");//按照分隔符切分字符串,提取出部门和工资
    45. //item[1]:部门 item[2]:工资
    46. //从dept.txt中拿到部门编号:重写setup方法(map之前回调一次,适用于小的表)
    47. outKey.set(depts.get(items[1]));//拿到部门对应的编号
    48. longValue.set(Long.parseLong(items[2]));//薪水
    49. context.write(outKey, longValue);
    50. }
    51. }

    02.StaffReduce

    1. import java.io.IOException;
    2. import org.apache.hadoop.io.DoubleWritable;
    3. import org.apache.hadoop.io.LongWritable;
    4. import org.apache.hadoop.io.Text;
    5. import org.apache.hadoop.mapreduce.Reducer;
    6. /*
    7. * KEYIN:reduse输入key(部门名称Text)
    8. * VALUEIN:reduse输入value(工资LongWritable[1,2,3,4,5,6...])
    9. * KEYOUT:reduse输出的key(部门名称Text)
    10. * VALUEOUT:平均工资(DoubleWritable)
    11. */
    12. public class StaffReduce extends Reducer<Text, LongWritable, Text,DoubleWritable> {
    13. private DoubleWritable outValue = new DoubleWritable();
    14. @Override
    15. protected void reduce(Text key, Iterable<LongWritable> values,Context context) throws IOException, InterruptedException {
    16. long num = 0 ;//数量
    17. long sum = 0 ;//总数
    18. for (LongWritable value : values) {
    19. ++num;
    20. sum+=value.get();
    21. }
    22. outValue.set(sum/(double)num);//薪水平均值
    23. context.write(key, outValue);
    24. }
    25. }

    03.StaffDriver

    1. import java.io.IOException;
    2. import org.apache.hadoop.conf.Configuration;
    3. import org.apache.hadoop.fs.Path;
    4. import org.apache.hadoop.io.DoubleWritable;
    5. import org.apache.hadoop.io.LongWritable;
    6. import org.apache.hadoop.io.Text;
    7. import org.apache.hadoop.mapreduce.Job;
    8. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
    9. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    10. public class StaffDriver {
    11. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
    12. // 1.创建job对象
    13. Configuration conf = new Configuration();
    14. Job job = Job.getInstance(conf);
    15. // 2.设置jar路径
    16. job.setJarByClass(StaffDriver.class);
    17. // 3.关联map与red
    18. job.setMapperClass(StaffMapper.class);
    19. job.setReducerClass(StaffReduce.class);
    20. // 4.设置map输出的键值对类型
    21. job.setMapOutputKeyClass(Text.class);
    22. job.setMapOutputValueClass(LongWritable.class);
    23. // 5.设置最终数据输出键值对类型
    24. job.setOutputKeyClass(Text.class);
    25. job.setOutputValueClass(DoubleWritable.class);
    26. // 6.设置输入路径(TextInputFormat)和输出路径(FileOutputFormat)
    27. TextInputFormat.addInputPath(job, new Path("D:\\360MoveData\\Users\\AIGameJXB\\Desktop\\Hadoop\\JavaProjectBigData\\src\\cn\\aigamejxb\\hadoop\\mapreduce\\staff\\input\\stuff_1.txt"));
    28. TextInputFormat.addInputPath(job, new Path("D:\\360MoveData\\Users\\AIGameJXB\\Desktop\\Hadoop\\JavaProjectBigData\\src\\cn\\aigamejxb\\hadoop\\mapreduce\\staff\\input\\stuff_2.txt"));
    29. TextInputFormat.addInputPath(job, new Path("D:\\360MoveData\\Users\\AIGameJXB\\Desktop\\Hadoop\\JavaProjectBigData\\src\\cn\\aigamejxb\\hadoop\\mapreduce\\staff\\input\\stuff_3.txt"));
    30. FileOutputFormat.setOutputPath(job, new Path("D:\\360MoveData\\Users\\AIGameJXB\\Desktop\\Hadoop\\JavaProjectBigData\\src\\cn\\aigamejxb\\hadoop\\mapreduce\\staff\\output\\stuff"));
    31. // 7.提交job
    32. boolean result = job.waitForCompletion(true);// true:打印运行信息
    33. System.exit(result ? 0 : 1);// 1:非正常退出
    34. }
    35. }

    [03].测试运行

  • 截屏(显而易见,运行成功了)

19.员工平均工资统计案例 - 图5

  • 接下来去看看输出了什么

19.员工平均工资统计案例 - 图6