Hadoop HelloWord Examples-平均数を求める


もう一つのhadoopの入門demoは、平均数を求めます.WordCountというdemoに対する小さな修正です.成績表(人名、成績)を入力し、各人の成績平均数を求めます.たとえば、次のようにします.
//  subject1.txt
  a 90   b 80   c 70
 //subject2.txt
  a 100   b 90   c 80
a,b,cの3人の平均点を求めます.解決の構想は簡単で、map段階でkeyは名前で、valueは成績で、直接outputです.reduceフェーズではmap出力のkey名前が得られ,valuesはその名前に対応する一連の成績であり,平均数を求めればよい.
ここでは,TextInputFormatとKeyValueTextInputFormatをそれぞれ入力フォーマットとして用いた2つのバージョンのコードを実現した.
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<Object, Text, Text, IntWritable>
	{
		@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<Text, IntWritable, Text, IntWritable>
	{
		@Override
		public void reduce(Text key, Iterable<IntWritable> 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<Text, Text, Text, IntWritable>
	{
		@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<Text, IntWritable, Text, IntWritable>
	{
		@Override
		public void reduce(Text key, Iterable<IntWritable> 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
 
作者:qiul 12345发表于2013-8-23 21:51:03原文链接
読書:113コメント:0コメントを表示