Hadoop 学习笔记 (十一) MapReduce 求平均成绩
china:
张三 78
李四 89
王五 96
赵六 67
english
张三 80
李四 82
王五 84
赵六 86
math
张三 88
李四 99
王五 66
赵六 77 import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.FloatWritable;
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.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.reduce.IntSumReducer; public class MyAverage { public static class FormatMapper extends Mapper<Object, Text, Text, IntWritable>{ private IntWritable val = new IntWritable(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
String line[] = value.toString().split("\\s");
val.set(Integer.parseInt(line[]));
context.write(new Text(line[]), val);
}
} public static class AverageReducer extends Reducer<Text, IntWritable, Text, FloatWritable>{
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException{
int count = ;
int sum = ;
for (IntWritable val : values) {
sum += val.get();
count ++;
}
float ave = (float)sum / count;
context.write(key, new FloatWritable(ave));
}
} public static void main(String[] args) throws Exception {
String dir_in = "hdfs://localhost:9000/in_average";
String dir_out = "hdfs://localhost:9000/out_average"; Path in = new Path(dir_in);
Path out = new Path(dir_out); Configuration conf = new Configuration();
Job averageJob = new Job(conf, "my_average"); averageJob.setJarByClass(MyAverage.class); averageJob.setInputFormatClass(TextInputFormat.class);
averageJob.setMapperClass(FormatMapper.class);
averageJob.setCombinerClass(IntSumReducer.class);
//countJob.setPartitionerClass(HashPartitioner.class);
averageJob.setMapOutputKeyClass(Text.class);
averageJob.setMapOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(averageJob, in); averageJob.setReducerClass(AverageReducer.class);
//averageJob.setNumReduceTasks(1);
averageJob.setOutputKeyClass(Text.class);
averageJob.setOutputValueClass(FloatWritable.class);
//countJob.setOutputFormatClass(SequenceFileOutputFormat.class); FileOutputFormat.setOutputPath(averageJob, out); averageJob.waitForCompletion(true); } }
张三 82.0
李四 90.0
王五 82.0
赵六 76.666664
Hadoop 学习笔记 (十一) MapReduce 求平均成绩的更多相关文章
- hadoop 学习笔记:mapreduce框架详解
开始聊mapreduce,mapreduce是hadoop的计算框架,我学hadoop是从hive开始入手,再到hdfs,当我学习hdfs时候,就感觉到hdfs和mapreduce关系的紧密.这个可能 ...
- Hadoop学习笔记:MapReduce框架详解
开始聊mapreduce,mapreduce是hadoop的计算框架,我学hadoop是从hive开始入手,再到hdfs,当我学习hdfs时候,就感觉到hdfs和mapreduce关系的紧密.这个可能 ...
- 【Big Data - Hadoop - MapReduce】hadoop 学习笔记:MapReduce框架详解
开始聊MapReduce,MapReduce是Hadoop的计算框架,我学Hadoop是从Hive开始入手,再到hdfs,当我学习hdfs时候,就感觉到hdfs和mapreduce关系的紧密.这个可能 ...
- hadoop 学习笔记:mapreduce框架详解(转)
原文:http://www.cnblogs.com/sharpxiajun/p/3151395.html(有删减) Mapreduce运行机制 下面我贴出几张图,这些图都是我在百度图片里找到的比较好的 ...
- Hadoop学习笔记—11.MapReduce中的排序和分组
一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...
- Hadoop学习笔记—12.MapReduce中的常见算法
一.MapReduce中有哪些常见算法 (1)经典之王:单词计数 这个是MapReduce的经典案例,经典的不能再经典了! (2)数据去重 "数据去重"主要是为了掌握和利用并行化思 ...
- Hadoop学习笔记: MapReduce二次排序
本文给出一个实现MapReduce二次排序的例子 package SortTest; import java.io.DataInput; import java.io.DataOutput; impo ...
- Hadoop学习笔记: MapReduce Java编程简介
概述 本文主要基于Hadoop 1.0.0后推出的新Java API为例介绍MapReduce的Java编程模型.新旧API主要区别在于新API(org.apache.hadoop.mapreduce ...
- 三、Hadoop学习笔记————从MapReduce到Yarn
Yarn减轻了JobTracker的负担,对其进行了解耦
随机推荐
- Hadoop的辉煌还能延续多久?
摘要:Hadoop已经成为大数据的代名词.短短几年间,Hadoop从一种边缘技术成为事实上的标准.而另一方面,MapReduce在谷歌已不再显赫.当企业瞩目MapReduce的时候,谷歌好像早已进入到 ...
- [Angular 2] DI in Angular 2 - 1
Orgial aritial --> Link The problem with Angular 1 DI: Angular 2 DI: Solve the singletons problem ...
- [TypeScript] Stopping a TypeScript Build When Errors Are Found
TypeScript will always compile even if there are ridiculous errors in your project. This lesson show ...
- NSURLSessionDownloadTask 断点下载
#import "ViewController.h" #import "ASIHTTPRequest.h" #import <AFNetworking/A ...
- LabVIEW设计模式系列——事件结构中值改变事件
标准:1.将具有值改变事件的控件,放置在其事件结构的值改变页面里.
- lua function
This is the main reference for the World of Warcraft Lua Runtime. Note that these are mostly standar ...
- 获取IMEI码
核心代码: Imei = ((TelephonyManager) getSystemService(TELEPHONY_SERVICE)).getDeviceId(); 1.加入权限 在manifes ...
- c#不重复的排序方法
public int getRandom(int num) { Thread.Sleep(5); // Random ro = new Random(unchecked((int)DateTime.N ...
- 整理收藏一份PHP高级工程师的笔试…
注:本文转自 http://www.icultivator.com/p/5535.html 注:本文转自 http://www.yiichina.com/tutorial/57 整理了一份PHP高级工 ...
- html 包含一个公共文件
<SCRIPT> $(document).ready(function(){ $("#foo").load("top.html"); setTime ...