Mapreduce《案例之数据排序》
数据源:
a.txt 内容:
aaa 120
bbb 100
ccc 130
ddd 150
b.txt内容:
aaa 121
bbb 101
ccc 131
ddd 150
c.txt内容
aaa 119
bbb 99
ccc 129
ddd 150
输出结果:
aaa120
bbb100
ccc130
ddd150
===========================java code==========================
package gq;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
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.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;
/**
*
* Class Description:求平均分测试类
*
* Author:gaoqi
*
* Date:2015年6月5日 下午2:03:08
*
*/
public class AvgScore {
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().trim();
StringTokenizer stk = new StringTokenizer(line,"/n");
while(stk.hasMoreElements()){
StringTokenizer sk = new StringTokenizer(stk.nextToken());
context.write(new Text(sk.nextToken()), new IntWritable(Integer.parseInt(sk.nextToken())));
}
}
}
public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException, InterruptedException{
Iterator<IntWritable> its = values.iterator();
int sum = 0;
int cnt = 0;
while(its.hasNext()){
sum += its.next().get();
cnt++;
}
context.write(key, new IntWritable(sum/cnt));
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = new Job(conf, "AvgScore");
job.setJarByClass(AvgScore.class);
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path("hdfs://h0:9000/user/tallqi/in/inputAvgScore"));
FileOutputFormat.setOutputPath(job, new Path("hdfs://h0:9000/user/tallqi/in/outputAvgScore"));
System.exit(job.waitForCompletion(true)?0:1);
}
}
相关推荐
MapReduce求取行平均值 MapReduce小实例 数据有经过处理已经添加行号的 也有未添加的 行平均值的四种求法
MapReduce求行平均值--标准差--迭代器处理--MapReduce案例
利用MapReduce实现了求学生成绩的最大值,最小值,及成绩分布。结合我的博客“MapReduce之学生平均成绩”看,效果更好。
了解map和reduce工作原理,以及排序,分组,分区设置,有详细的注释,方便查看学习,适合入门初学者练手
1.社交网络综合评分案例 2.微博精准营销案例 3.物品推荐案例 4.QQ好友推荐案例
2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx2-MapReduce案例.docx
大数据mapreduce案例介绍,包括代码解释,详解MRS工作流程
mapreduce案例测试数据
mapreduce案例测试数据
mapreduce的Wordcount案例将main生成jar可直接在hdfs上运行
mapreduce案例数据:dept.csv,emp.csv,sales.csv
mapreduce案例测试数据
MapReduce实现单词分类,可直接运行。MapReduce实现单词分类,可直接运行。
mapreduce基本数据读取,通俗易懂。 此项目情景为,遗传关系族谱。 (爷爷 、父母 、孩子) 经典案例
前四节提供了几个小案例 下面详细介绍MapReduce中Map任务Reduce任务以及MapReduce的执行流程。 Map任务: 读取输入文件内容,解析成key,value对。对输入文件的每一行,解析成key,value对。每一个键值对调用一次map...
整理了MapRduce的几个入门案例,提供源数据 案例分为: 1、统计单词的个数 2、设置分区 3、统计json数据 4、统计并排序 5、mapReducer找朋友的使用
细细品味Hadoop_Hadoop集群(第9期)_MapReduce初级案例
MapReduce求平均值示例程序,并且程序里利用了logger类,可以将调试信息输出到mapreduce的日志,可以根据输出信息了解运行机制
MapReduce字数统计案例,希望大家交流,最好自己写完后在对比交流,欢迎前来交流
https://blog.csdn.net/qq_39063526/article/details/105968494 本文案例中用到的文件