微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MapReduce编程笔记3-计算部门工资

一、分析数据处理的过程

二、程序代码

2.1 main程序

@H_404_11@import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; 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 { //1、创建任务Job,并且指定任务的入口 Job job = Job.getInstance(new Configuration()); job.setJarByClass(SalaryTotalMain.class); //2、指定任务的Map,Map的输出类型 job.setMapperClass(SalaryTotalMapper.class); job.setMapOutputKeyClass(IntWritable.class); //k2 job.setMapOutputValueClass(IntWritable.class); //v2 //3、指定任务的Reduce,Reduce的输出类型 job.setReducerClass(SalaryTotalReducer.class); job.setoutputKeyClass(IntWritable.class); //k4 job.setoutputValueClass(IntWritable.class); //v4 //4、指定任务的输入路径和输出路径 FileInputFormat.setInputPaths(job, new Path(args[0])); FileOutputFormat.setoutputPath(job, new Path(args[1])); //5、执行任务 job.waitForCompletion(true); } }

2.2 Map程序

@H_404_11@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 java.io.IOException; public class SalaryTotalMapper extends Mapper<LongWritable, Text,IntWritable, IntWritable> { @Override protected void map(LongWritable key1, Text value1, Context context) throws IOException, InterruptedException { /* context代表Map的上下文 上文:DHFS的输入 下文:Reduce */ String data =value1.toString(); String [] words = data.split(","); for ( String w:words){ context.write(new IntWritable(Integer.parseInt(words[7])),new IntWritable(Integer.parseInt(words[5]))); } } }

2.1 Reduce程序

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐