hadoop编程之部门工资求和

数据集展示

7369SMITHCLERK79021980/12/1780020
7499ALLENSALESMAN76981981/2/20160030030
7521WARDSALESMAN76981981/2/22125050030
7566JONESMANAGER78391981/4/2297520
7654MARTINSALESMAN76981981/9/281250140030
7698BLAKEMANAGER78391981/5/1285030
7782CLARKMANAGER78391981/6/9245010
7788SCOTTANALYST75661987/4/19300020
7839KINGPRESIDENT1981/11/17500010
7844TURNERSALESMAN76981981/9/81500030
7876ADAMSCLERK77881987/5/23110020
7900JAMESCLERK76981981/12/395030
7902FORDANALYST75661981/12/3300020
7934MILLERCLERK77821982/1/23130010

 建立不同的java类

SalaryTotalMain、SalaryTotalMapper、SalaryTotalReducer这三个类

对应代码如下:

SalaryTotalMain

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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;
public class SalaryTotalMain {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJarByClass(SalaryTotalMain.class);
        job.setMapperClass(SalaryTotalMapper.class);
        job.setReducerClass(SalaryTotalReducer.class);
        job.setMapOutputKeyClass(LongWritable.class);
        job.setMapOutputValueClass(LongWritable.class);
        job.setOutputKeyClass(LongWritable.class);
        job.setOutputValueClass(LongWritable.class);
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        job.waitForCompletion(true);
    }
}

SalaryTotalMapper

import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Mapper.Context;
public class SalaryTotalMapper extends Mapper<LongWritable,Text, LongWritable, LongWritable> {
    @Override
    protected void map(LongWritable key1,Text value1, Context context)
            throws IOException, InterruptedException {
        String data = value1.toString();
        String[] words = data.split(",");
        context.write(new LongWritable(Integer.parseInt(words[7])),new LongWritable(Integer.parseInt(words[5])));
    }

}

SalaryTotalReducer

import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Reducer.Context;
public class SalaryTotalReducer extends Reducer<LongWritable, LongWritable,LongWritable, LongWritable> {
    @Override
    protected void reduce(LongWritable k3, Iterable<LongWritable> v3,Context context) throws IOException,InterruptedException {
        long  total = 0;
        long  max = 0;
        for(LongWritable v:v3)
        {
            total=v.get();
            if(max<total)
            {
                max=total;
            }
        }
        context.write(k3, new LongWritable(max));
    }

}

命令

hadoop jar 2.jar  ch02.SalaryTotalMain  /user/data/input/emp.csv  /user/data/output/ch2
hadoop jar 包名   主类  输入路径 输出路径

对应结果

 

 学习链接

在Ubuntu上用mapreduce进行词频统计(伪分布式)_mapreduce怎么统计txt文件词频终端-CSDN博客

利用mapreduce统计部门的最高工资_使用mapreduce查询某个部门中薪资最高的员工姓名,如果输出结果的格式为“薪资 员-CSDN博客

hadoop编程之工资序列化排序-CSDN博客

 hadoop编程之部门工资求和-CSDN博客

hadoop编程之词频统计-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张謹礧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值