MapReduce 程序(1)
来源:互联网 发布:网络电视机排名 编辑:程序博客网 时间:2024/05/18 03:34
之前在网上看了很多MapReduce程序计数的帖子,在这里跟大家分享一下自己编写的代码。
此文章适合初入门级别。
file1:
sunying 89
yangkun 95
wangjie 75
zhaode 85
xiaoming 90
file2:
sunying 85
yangkun 75
wangjie 79
zhaode 89
file3:
sunying 78
yangkun 90
wangjie 85
zhaode 80
package org.sunying.practice;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class AvgScore {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(AvgScore.class);
// 设置map类,定义输出的数据类型
job.setMapperClass(ASMapper.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(LongWritable.class);
// 设置reduce类 ,定义输出的数据类型
job.setReducerClass(ASReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
// 设置输入输出的文件目录
FileInputFormat.setInputPaths(job, new Path("D://hadoop-2.6.0//input//score"));
FileOutputFormat.setOutputPath(job, new Path("D://hadoop-2.6.0//output"));
// 这几行代码是清理输出结果的文件
Path path = new Path("D://hadoop-2.6.0//output");
FileSystem fileSystem = path.getFileSystem(conf);
if(fileSystem.exists(path)){
fileSystem.delete(path, true);// true表示文件里有内容时也删除文件。
}
job.waitForCompletion(true);
}
public static class ASMapper extends Mapper<LongWritable, Text, Text, LongWritable>{
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// 将读入的内容转成string类型并分割,输出到reduce
String[] str = value.toString().split("\t");
context.write(new Text(str[0]), new LongWritable(Long.parseLong(str[1])));
}
}
public static class ASReducer extends Reducer<Text, LongWritable, Text, Text>{
@Override
protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
long count = 0;
for (LongWritable l : values) {
count = count + l.get();
}
// 此处将结果四舍五入,保留两位小数。这是简单的方法。
double rr =count / 3.0;
String ff = String.format("%.2f", rr);
context.write(key, new Text(ff));
}
}
}
- MapReduce 程序(1)
- MapReduce 1 程序开发
- 开发MapReduce程序 实验1
- MapReduce--1--入门程序WordCount
- hadoop学习之Mapreduce(2.4.1):写mapreduce程序时编写自己的writable类
- 用 Eclipse 开发MapReduce程序的问题(1)
- Hadoop教程(四):理解MapReduce、MapReduce计数器和连接、MapReduce Hadoop程序连接数据
- 简单的MapReduce程序(Hadoop2.2.0)
- Eclipse 调试Mapreduce程序 (2)
- 简单的MapReduce程序(Hadoop2.2.0)
- 第五个MapReduce程序----标准版(TopKey)
- 分布式倒置索引(MapReduce程序)
- eclipse 编写mapreduce程序(wordCount)
- (七)MapReduce字数统计程序
- Hadoop(十三)分析MapReduce程序
- MapReduce学习(1)
- C++版的mapreduce程序运行在hadoop上(1)
- mapreduce程序编写注意事项
- 数据库报错无法打开的紧急救援案例
- 文章标题 HDU 2874 : Connections between cities(LCA+并查集)
- yum安装lnmp-mysql
- ImageLoader加载图片
- linux编程---线程---条件变量
- MapReduce 程序(1)
- 仪表盘类型
- python6-运用PIL模块的简单图像处理
- 什么时候要用存储过程
- C# 结束程序代码
- HDU 6168 Numbers(规律)
- 多年iOS开发经验总结1
- Number Game(斯特林公式)
- 如何自定义注解及使用