MapReduce之Wordcount

本文详细介绍了如何在本地Eclipse环境中搭建MapReduce环境,包括配置Hadoop、Maven项目依赖以及实现wordcount程序的具体步骤。从创建Maven项目到编写Mapper、Reducer和Driver类,再到运行程序,为初学者提供了全面的指导。

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

学习MapReduce第一个程序,wordcount

本次的程序是在本机上eclipse中进行,再此之前应该先将在win10编译后的Hadoop解压,并配置好环境变量。

并不需要其他插件什么的,因为在导入依赖后生成的jar包里面有支持在本机跑mr的源码,它会认为你一是个jobrunner,并在执行程序时生成一些配置文件比如core-set.xml 等等,就是我们在集群上配置的一些文件,所以,在保证自己代码没问题的情况下直接运行就好。

一:首先在eclipse中建立一个Maven项目,在pom.xml中加入以下依赖
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>2.7.2</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.7.2</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-hdfs</artifactId> <version>2.7.2</version> </dependency>

当然,写依赖要根据你的版本来写。

二,准备工作完成后,分别写三个类,WordcountMapper、WordcountReducer、WordcountDriver,在写程序的时候一定要注意自己是否import正确的包!!

1,WordcountMapper类:


import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class WordcountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
	Text k = new Text();
	IntWritable v = new IntWritable(1);
	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {
		//获取一行
		String line = value.toString();
		//分割
		String[] words = line.split(" ");
		//输出
		for (String word : words) {
			k.set(word);
			context.write(k, v);
		}
	}
}

2,WordcountReducer类

import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class WordcountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

//创建IntWritable 对象是为了将sum转换为IntWritable类型
	IntWritable v = new IntWritable();
	int sum;
	@Override
	protected void reduce(Text key, Iterable<IntWritable> values,
			Context context) throws IOException, InterruptedException {
		sum = 0;
		for (IntWritable count : values) {
		//累加求和
			sum += count.get();
		}
		//输出
		v.set(sum);
		context.write(key, v);
	}
}

3,WordcountDriver

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordcountDriver {

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
	
	//输出文件在执行前不能存在,执行后自动创建,否则报错
	args = new String[]{"输入文件的路径","输出文件的路径"};
		// 1 获取配置信息以及封装任务
		Configuration configuration = new Configuration();
		Job job = Job.getInstance(configuration);

		// 2 设置jar加载路径
		job.setJarByClass(WordcountDriver.class);

		// 3 设置map和reduce类
		job.setMapperClass(WordcountMapper.class);
		job.setReducerClass(WordcountReducer.class);

		// 4 设置map输出
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);

		// 5 设置最终输出kv类型
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(IntWritable.class);
		
		// 6 设置输入和输出路径
		FileInputFormat.setInputPaths(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));

		// 7 提交
		boolean result = job.waitForCompletion(true);
		System.exit(result ? 0 : 1);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值