Hadoop HelloWord Examples- 求平均数

本文提供了一个Hadoop入门demo,演示如何利用Hadoop计算成绩单中每个人的平均成绩。通过TextInputFormat和KeyValueTextInputFormat实现不同版本的代码,最终输出每位学生的平均成绩。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  另外一个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<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

 

<think>好的,我现在需要回答用户关于hadoop-mapreduce-examples-3.1.4.jar功能的提问。首先,我要确认这个JAR文件是Hadoop的一部分,特别是MapReduce的例子。 我应该先回忆Hadoop的组件结构。Hadoop主要包括HDFS和MapReduce,而MapReduce例子包通常包含一些演示程序。用户提到的是3.1.4版本,这个版本可能包含的标准示例有哪些呢? 可能需要列举常见的例子,比如WordCount,计算词频。然后还有其他比如排序、Join操作、统计等。每个例子的用途需要简要说明,让用户了解它们的作用。 另外,这个JAR文件的位置可能在Hadoop安装目录的share/hadoop/mapreduce下,用户可以通过命令行运行这些示例。需要提到如何调用它们,比如使用hadoop jar命令,指定主类名和参数。 还要注意,不同版本的Hadoop可能例子略有不同,但3.1.4应该包含大部分经典示例。需要确保信息准确,可能需要查阅相关文档或版本发布说明来确认。 另外,用户可能想知道这些示例的用途,以便学习MapReduce编程或测试集群。因此,回答中需要说明这些示例既是学习工具,也是测试工具。 需要避免错误,比如不存在的例子,或者错误的命令语法。确保每个例子的描述简洁明了,让用户能快速理解每个示例的功能。 总结下来,回答的结构应该是先介绍JAR文件的作用,然后列出主要示例及其功能,最后说明如何使用它们。同时,提醒用户查阅官方文档获取完整列表。</think>Hadoop MapReduce Examples 3.1.4.jar 是 Apache Hadoop 官方提供的示例程序集合,主要用于演示 MapReduce 编程模型的实际应用。该 JAR 文件通常位于 Hadoop 安装目录的 `share/hadoop/mapreduce/` 路径下,包含以下典型功能示例: --- ### **核心功能示例** 1. **WordCount** - **用途**:统计文本文件中每个单词的出现次数 - **命令示例**: ```bash hadoop jar hadoop-mapreduce-examples-3.1.4.jar wordcount /input /output ``` 2. **Teragen/Terasort** - **用途**:生成大规模随机数据并测试分布式排序性能 - **命令示例**: ```bash hadoop jar hadoop-mapreduce-examples-3.1.4.jar teragen 1000000 /teragen-input hadoop jar hadoop-mapreduce-examples-3.1.4.jar terasort /teragen-input /terasort-output ``` 3. **Pi Estimator** - **用途**:通过蒙特卡洛方法估算圆周率 $π$ 的值 - **命令示例**: ```bash hadoop jar hadoop-mapreduce-examples-3.1.4.jar pi 100 1000 ``` 4. **Grep** - **用途**:在大型文本中搜索匹配正则表达式的行 - **命令示例**: ```bash hadoop jar hadoop-mapreduce-examples-3.1.4.jar grep /input /output "error.*" ``` 5. **Join 操作** - **用途**:演示两个数据集(如用户信息与订单记录)的关联查询 - **示例类**:`join.DataJoin` 或 `multifileinput.MultiFileWordCount` --- ### **其他实用工具** - **SecondarySort**:展示如何对中间键值对进行二次排序 - **AggregateWordCount**:优化版词频统计(使用组合器提升性能) - **WordMean/WordMedian**:计算单词长度的均值和中位数 - **RandomTextWriter**:生成随机文本用于测试 --- ### **使用场景** 1. **学习 MapReduce**:通过示例代码理解 Map/Reduce 阶段的数据处理流程 2. **集群验证**:运行 Terasort 等任务测试 Hadoop 集群性能和稳定性 3. **算法验证**:快速验证自定义 MapReduce 逻辑的正确性 --- ### **调用方式** 通过 `hadoop jar` 命令运行指定主类: ```bash hadoop jar hadoop-mapreduce-examples-3.1.4.jar <example_class> [参数...] ``` 完整示例列表可通过以下命令查看: ```bash hadoop jar hadoop-mapreduce-examples-3.1.4.jar ``` 建议参考官方文档获取每个示例的详细参数说明:[Apache Hadoop MapReduce Examples](https://hadoop.apache.org/docs/stable/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值