威凡网全力打造:网页编程、软件开发编程、平面设计、服务器端开发、操作系统等在线学习平台!学编程,上威凡网!
PHP教程>> PHP基础 PHP技巧 PHP实例 PHP文摘 PHP模板 PHP总结
当前位置:首页 > PHP教程 > PHP总结
上一节 下一节
 HadoopHelloWordExamples-求平均数

? 另外一个hadoop的入门demo,求平均数。是对wordcount这个demo的一个小小的修改。输入一堆成绩单(人名,成绩),然后求每个人成绩平均数,比如: //? subject1.txt ? a 90 ? b 80 ? c 70 ?// subject2.txt ? a 100 ? b 90 ? c 80 ? 求a,b,c这三个人的平均

? 另外一个hadoop的入门demo,求平均数。是对wordcount这个demo的一个小小的修改。输入一堆成绩单(人名,成绩),然后求每个人成绩平均数,比如:

//? subject1.txt

? a 90
? b 80
? c 70


?// subject2.txt

? a 100
? b 90
? c 80


? 求a,b,c这三个人的平均分。解决思路很简单,在map阶段key是名字,value是成绩,直接output。reduce阶段得到了map输出的key名字,values是该名字对应的一系列的成绩,那么对其求平均数即可。

? 这里我们实现了两个版本的代码,分别用textinputformat和 keyvaluetextinputformat来作为输入格式。

? textinputformat版本:

?

import java.util.*;
import java.io.*;
import org.apache.hadoop.conf.configuration;
import org.apache.hadoop.fs.path;
import org.apache.hadoop.io.text;
import org.apache.hadoop.io.intwritable;
import org.apache.hadoop.mapreduce.mapper;
import org.apache.hadoop.mapreduce.reducer;
import org.apache.hadoop.mapreduce.job;
import org.apache.hadoop.mapreduce.lib.input.fileinputformat;
import org.apache.hadoop.mapreduce.lib.output.fileoutputformat;
public class avescore {
	public static class avemapper extends mapper
	{
		@override
		public void map(object key, text value, context context) throws ioexception, interruptedexception
		{
			string line = value.tostring();
			string[] strs = line.split(" ");
			string name = strs[0];
			int score = integer.parseint(strs[1]);
			context.write(new text(name), new intwritable(score));
		}
	}
	public static class avereducer extends reducer
	{
		@override
		public void reduce(text key, iterable values, context context) throws ioexception, interruptedexception
		{
			int sum = 0;
			int count = 0;
			for(intwritable val : values)
			{
				sum += val.get();
				count++;
			}
			int avescore = sum / count;
			context.write(key, new intwritable(avescore));
		}
	}
	public static void main(string[] args) throws exception
	{
		configuration conf = new configuration();
		job job = new job(conf,"averagescore");
		job.setjarbyclass(avescore.class);
		job.setmapperclass(avemapper.class);
		job.setreducerclass(avereducer.class);
		job.setoutputkeyclass(text.class);
		job.setoutputvalueclass(intwritable.class);
		fileinputformat.addinputpath(job, new path(args[0]));
		fileoutputformat.setoutputpath(job, new path(args[1]));
		system.exit( job.waitforcompletion(true) ? 0 : 1);
	}
}

keyvaluetextinputformat版本;

import java.util.*;
import java.io.*;
import org.apache.hadoop.conf.configuration;
import org.apache.hadoop.fs.path;
import org.apache.hadoop.io.text;
import org.apache.hadoop.io.intwritable;
import org.apache.hadoop.mapreduce.mapper;
import org.apache.hadoop.mapreduce.reducer;
import org.apache.hadoop.mapreduce.job;
import org.apache.hadoop.mapreduce.lib.input.fileinputformat;
import org.apache.hadoop.mapreduce.lib.output.fileoutputformat;
import org.apache.hadoop.mapreduce.lib.input.keyvaluetextinputformat;
import org.apache.hadoop.mapreduce.lib.output.textoutputformat;
public class avescore_keyvalue {
	public static class avemapper extends mapper
	{
		@override
		public void map(text key, text value, context context) throws ioexception, interruptedexception
		{
		    int score = integer.parseint(value.tostring());
			context.write(key, new intwritable(score) );
		}
	}
	public static class avereducer extends reducer
	{
		@override
		public void reduce(text key, iterable values, context context) throws ioexception, interruptedexception
		{
			int sum = 0;
			int count = 0;
			for(intwritable val : values)
			{
				sum += val.get();
				count++;
			}
			int avescore = sum / count;
			context.write(key, new intwritable(avescore));
		}
	}
	public static void main(string[] args) throws exception
	{
		configuration conf = new configuration();
		conf.set("mapreduce.input.keyvaluelinerecordreader.key.value.separator", " ");
		job job = new job(conf,"averagescore");
		job.setjarbyclass(avescore_keyvalue.class);
		job.setmapperclass(avemapper.class);
		job.setreducerclass(avereducer.class);
		job.setoutputkeyclass(text.class);
		job.setoutputvalueclass(intwritable.class);
  		job.setinputformatclass(keyvaluetextinputformat.class);
		job.setoutputformatclass(textoutputformat.class)  ; 
		fileinputformat.addinputpath(job, new path(args[0]));
		fileoutputformat.setoutputpath(job, new path(args[1]));
		system.exit( job.waitforcompletion(true) ? 0 : 1);
	}
}


输出结果为:

? a 95
? b 85
? c 75

?

作者:qiul12345 发表于2013-8-23 21:51:03 原文链接

阅读:113 评论:0 查看评论


申明:本教程内容由威凡网编辑整理并提供IT程序员分享学习,如文中有侵权行为,请与站长联系(QQ:254677821)!

上一节 下一节
相关教程  
其他教程  
PHP基础
PHP技巧
PHP实例
PHP文摘
PHP模板
PHP总结

违法和不良信息举报中心】邮箱:254677821@qq.com
Copyright©威凡网 版权所有 苏ICP备2023020142号
站长QQ:254677821