编写Java代码实现MapReduce词汇统计

WordCount思想:通过Mapper对文档内部字段进行分词,再由Reduce进行相同词汇进行汇总、统计(理解可能有误,还望路过的大神指正)

MyMapper:

package com.alvis.workcount;

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 MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

	@Override
	protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context)
		throws IOException, InterruptedException {
		//map阶段:读取文本进行分词
		//例如value = i love Guiyang  一行文本
		//将Text数据类型装换成String类型
		String formatValue = value.toString();
		//目的,将Text文本按空格进行分割
		String[] spiltWord = formatValue.split(" ");
		//分词好之后将每个单词及其出现的次数输出给下一阶段
		for(String word : spiltWord){
			context.write(new Text(word), new IntWritable(1));
		}
	}
}

MyReduce:

package com.alvis.workcount;

import java.io.IOException;

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

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

	@Override
	protected void reduce(Text key3, Iterable<IntWritable> value3,
	Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
	//reduce 合并和统计
	    int sum = 0;
	    for(IntWritable intWritable : value3){
		    sum += intWritable.get();
	    }
	    
		context.write(new Text(key3), new IntWritable(sum));
	}
    
}

MyJob

package com.alvis.workcount;

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 MyJob {
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		//1、创建程序入口及获取配置文件对象
		Configuration configuration = new Configuration();
		Job job = Job.getInstance(configuration);
        job.setJarByClass(MyJob.class);
        
        //2、指定Job的map的输出及其输出的类型
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //3、指定job的reduce输出及输出类型
        job.setReducerClass(MyReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        
        //4、指定job的输入文件及输出文件的路径
        FileInputFormat.setInputPaths(job, new Path("/input/test.txt"));
        FileOutputFormat.setOutputPath(job, new Path("/output/2019326"));
        
        //5、执行程序
        job.waitForCompletion(true);
	}

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解关于使用MapReduce进行词汇统计的相关知识,我可以为您提供一些参考。 MapReduce是一种分布式计算框架,可以用于处理大规模数据。在词汇统计方面,MapReduce可以将大量文本数据分成若干个小块,每个小块都由一个Map任务处理,Map任务将文本数据转换为键值对形式,其中键表示单词,值表示单词出现的次数。然后,所有Map任务的输出将被合并,以便进行排序和分组,最后由Reduce任务计算每个单词的总出现次数。 具体而言,MapReduce词汇统计的流程如下: 1. 输入数据:将要处理的文本分成若干个小块,每个小块由一个Map任务处理。 2. Map任务:Map任务将文本数据转换为键值对形式,其中键表示单词,值表示单词出现的次数。Map任务的输出将传递给Reduce任务进行处理。 3. Shuffle过程:所有Map任务的输出将被合并,以便进行排序和分组。在这个过程中,Map任务的输出会根据键值进行排序,并按照键值进行分组。 4. Reduce任务:Reduce任务计算每个单词的总出现次数。在这个过程中,Reduce任务会对每个单词的值进行累加运算,以得到单词的总出现次数。 5. 输出结果:Reduce任务的输出将作为最终的结果进行输出。 需要注意的是,MapReduce词汇统计的性能取决于集群的规模和计算节点的数量。因此,在使用MapReduce进行词汇统计时,需要根据数据的规模和计算资源的可用性来选择适当的集群配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值