MapReduce统计部门员工组别,并排序

学习目标:MapReduce统计部门员工

员工数据

孙悟空	23	西游部	筋斗云
武松	33	水浒部	醉拳
关胜	32	水浒部	大刀
猪八戒	21	西游部	吃吃吃
贾宝玉	21	红楼部	呵呵
林黛玉	16	红楼部	嘻嘻
沙僧	25	西游部	潜水
王熙凤	46	红楼部

提示:有一个员工缺少一个技能属性字段

学习内容:现根据员工的组别进行自然排序,之后按照年龄排序

实体类


import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class Employees implements WritableComparable<Employees> {
    private String name;
    private int age;
    private String department;
    private String skill;

    public Employees() {
        super();
    }

    public Employees(String name, int age, String department, String skill) {
        this.name = name;
        this.age = age;
        this.department = department;
        this.skill = skill;
    }
    public void setEmployees(String name, int age, String department) {
        this.skill="";
        this.name = name;
        this.age = age;
        this.department = department;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getSkill() {
        return skill;
    }

    public void setSkill(String skill) {
        this.skill = skill;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(name);
        out.writeInt(age);
        out.writeUTF(department);
        out.writeUTF(skill);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        System.out.println("序列化————————————————————————————————————————————————");
        this.name=in.readUTF();
        this.age=in.readInt();
        this.department=in.readUTF();
        this.skill=in.readUTF();
    }

    @Override
    public String toString() {
        return  name + '\t'+ +age +'\t'+ department + '\t' + skill ;
    }

    @Override
    public int compareTo(Employees o) {
			//先根据第一个组别进行判断,现根据组别自然排序,在进行年龄排序
        int i = this.department.compareTo(o.department);
        if (0 ==i){
            return Integer.valueOf(this.age).compareTo(o.age);
        }

        return i;


    }
}

Mapper

import mapreduce.entity.Employees;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class Mapper_sort_group extends Mapper<LongWritable, Text, Employees,Text> {



    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        Employees employees = new Employees();

        String line = value.toString();
        String [] split = line.split("\t");

        if (split.length==4){
            employees.setName(split[0]);
            employees.setAge((int) Long.parseLong(split[1]));
            employees.setDepartment(split[2]);
            employees.setSkill(split[3]);
        }else {
            employees.setEmployees(split[0], (int) Long.parseLong(split[1]),split[2]);
        }

        context.write(employees,new Text());
    }
}

Reduce

import mapreduce.entity.Employees;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class Reduce_sort_group extends Reducer<Employees, Text, Employees,Text> {


    @Override
    protected void reduce(Employees key, Iterable<Text> values, Context context) throws IOException, InterruptedException {

        context.write(key,new Text());
    }
}

启动类


import mapreduce.entity.Employees;
import mapreduce.tools.UtilsMapReduce;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Driver_sort_group {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        Job job = UtilsMapReduce.getJob("Driver_group");

        job.setJarByClass(Driver_sort_group.class);

        //关联mapper和reduce
        job.setMapperClass(Mapper_sort_group.class);
        job.setReducerClass(Reduce_sort_group.class);

        //设置map输出额Kv类型
        job.setMapOutputKeyClass(Employees.class);
        job.setMapOutputValueClass(Text.class);

        //设置最终输出的KV类型
        job.setOutputKeyClass(Employees.class);
        job.setOutputValueClass(Text.class);


        //设置输入路径和输出路径
        FileInputFormat.setInputPaths(job,new Path("input2\\employees"));
        FileOutputFormat.setOutputPath(job,new Path("output\\employees_sort_group"));

        //7.提交job
        boolean result =job.waitForCompletion(true);
        System.exit(result?0:1);


    }
}

结果

关胜	32	水浒部	大刀	
武松	33	水浒部	醉拳	
林黛玉	16	红楼部	嘻嘻	
贾宝玉	21	红楼部	呵呵	
王熙凤	46	红楼部		
猪八戒	21	西游部	吃吃吃	
孙悟空	23	西游部	筋斗云	
沙僧	25	西游部	潜水	

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值