MapReduce经典案例——求平均分数
2014-01-09 18:30
387 查看
资源文件math
张三 99
李四 90
王五 90
赵六 60
资源文件china
张三 79李四 75王五 80赵六 90资源文件english
张三 89李四 75王五 70赵六 90分析:
map 阶段将将学生姓名作为key 成绩作为value.这样Reduce阶段得到的数据就是
key:张三 value:{99,79,89}……
在Reduce中将学生的成绩球平均值。
实现:
运行:
1:将程序打包 选中打包的类-->右击-->Export-->java-->JAR file--填入保存路径-->完成2:将jar包拷贝到hadoop的目录下。(因为程序中用到来hadoop的jar包)3:将资源文件上传到定义的hdfs目录下 创建hdfs目录命令(在hadoop已经成功启动的前提下):hadoop fs -mkdir /自定义/自定义/input 上传本地资源文件到hdfs上:hadop fs -put -copyFromLocal /home/user/Document/math /自定义/自定义/input……4:运行MapReduce程序:hadoop jar /home/user/hadoop-1.0.4/AvgSorce.jar com.bwzy.hadoop.AvgSorce /自定义/自定义/input /自定义/自定义/output
说明:hadoop运行后会自动创建/自定义/自定义/output目录,在该目录下会有两个文件,其中一个文件中存放来MapReduce运行的结果。如果重新运行该程序,需要将/自定义/自定义/output目录删除,否则系统认为该结果已经存在了。5:运行的结果为张三 89
李四 80
王五 80
赵六 80
张三 99
李四 90
王五 90
赵六 60
资源文件china
张三 79李四 75王五 80赵六 90资源文件english
张三 89李四 75王五 70赵六 90分析:
map 阶段将将学生姓名作为key 成绩作为value.这样Reduce阶段得到的数据就是
key:张三 value:{99,79,89}……
在Reduce中将学生的成绩球平均值。
实现:
package com.bwzy.hadoop; import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.Reducer.Context; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; import com.bwzy.hadoop.HeBing.Map; import com.bwzy.hadoop.HeBing.Reduce; public class AvgSorce extends Configured implements Tool { public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> { public void map(LongWritable key, Text value, Context context) throws IOException,InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while(tokenizer.hasMoreElements()){ String strName = tokenizer.nextToken(); String strSorce = tokenizer.nextToken(); context.write(new Text(strName), new IntWritable(Integer.parseInt(strSorce))); } } } public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> { public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; int num = 0; for (IntWritable sorce : values) { sum+=sorce.get(); num++; } context.write(key, new IntWritable((int)(sum/num))); } } @Override public int run(String[] arg0) throws Exception { Job job = new Job(getConf()); job.setJobName("AvgSorce"); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); job.setMapperClass(Map.class); // job.setCombinerClass(Reduce.class); job.setReducerClass(Reduce.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.setInputPaths(job, new Path(arg0[0])); FileOutputFormat.setOutputPath(job, new Path(arg0[1])); boolean success = job.waitForCompletion(true); return success ? 0 : 1; } public static void main(String[] args) throws Exception { int ret = ToolRunner.run(new AvgSorce(), args); System.exit(ret); } }
运行:
1:将程序打包 选中打包的类-->右击-->Export-->java-->JAR file--填入保存路径-->完成2:将jar包拷贝到hadoop的目录下。(因为程序中用到来hadoop的jar包)3:将资源文件上传到定义的hdfs目录下 创建hdfs目录命令(在hadoop已经成功启动的前提下):hadoop fs -mkdir /自定义/自定义/input 上传本地资源文件到hdfs上:hadop fs -put -copyFromLocal /home/user/Document/math /自定义/自定义/input……4:运行MapReduce程序:hadoop jar /home/user/hadoop-1.0.4/AvgSorce.jar com.bwzy.hadoop.AvgSorce /自定义/自定义/input /自定义/自定义/output
说明:hadoop运行后会自动创建/自定义/自定义/output目录,在该目录下会有两个文件,其中一个文件中存放来MapReduce运行的结果。如果重新运行该程序,需要将/自定义/自定义/output目录删除,否则系统认为该结果已经存在了。5:运行的结果为张三 89
李四 80
王五 80
赵六 80
相关文章推荐
- (二)MapReduce经典案例分享(求平均分数)
- MapReduce经典案例分享
- MapReduce案例11——影评分析3(特定电影不同年龄段平均评分)
- MapReduce经典案例——统计单词数
- MapReduce经典案例——合并文档
- MapReduce经典入门小案例
- Hadoop集群MapReduce经典案例
- MapReduce案例2——求学生平均成绩
- MapReduce案例学习(2) 求各个部门的人数和平均工资
- MapReduce初级案例(3):使用MapReduce实现平均成绩
- MapReduce初级经典案例实现
- MapReduce —案例(一)求最高分,最低分,平均分
- 使用MapReduce实现一些经典的案例
- Hadoop集群MapReduce初级案例(经典案例)
- Hadoop经典案例Spark实现(四)——平均成绩
- MapReduce案例学习(6) 列出工资比公司平均工资要高的员工姓名及其工资
- MapReduce—案例(二)求出现了相同分数的分数,次数,该分数的人数
- 备考蓝桥杯(36)平均距离最小的4个点(超级经典exeforward案例)
- MapReduce初级经典案例实现
- Hadoop(4-2)-MapReduce程序案例-WordCount(Intellij Idea环境)