赞
踩
这次终于开始了这是的MapReduce的编码过程,记录以下
编写MapReduce对一个文本中单词的使用频率进行统计
hello world
hello hadoop
hello mapreduce
hello spark
hello school
hadoop 1
hello 5
mapreduce 1
school 1
spark 1
world 1
在这个问题中,将文本中的内容切割为每一个单词,然后将相同的单词聚集在一起,最后计算此书并输出
在这个问题中,map阶段主要负责单词切割任务。
Mapper处理的数据是由InputFormat分解过的数据集,其中InputFormat的作用是将数据集切割成小数据集InputSplits,每一个InputSplit将由一个Mapper负责处理,此外,InputFormat中还提供了一个RecordReader的实现,并将一个InputSolit解析成< key,value>对提供给map函数。
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
reduce阶段主要是接收map结算的单词分组,然后对单词进行统计。
Mapper中传递的数据为的结果对<key,value>,会送到Reducer中进行合并,合并的过程中,有相同的key的键/值对则送到同一个Reducer上,并由reduce具体计算出单词所对应的频数。
public static class IntSumReducer
extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
在运行代码之前,我们需要在该项目之下建立一个input文件夹,在input文件夹下建立一个文本文件,存放用来处理的单词。
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.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import java.io.IOException; import java.util.StringTokenizer; public class WordCount { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Object key, Text value, Context context ) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } } public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context ) throws IOException, InterruptedException { int sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); String[] otherArgs = new String[]{"input/file.txt", "output"}; //input/file.txt为本地文件 //程序在本地直接运行,毕竟每次都要打包并上传集群好麻烦,或许以后考虑写一个自动化的脚本 if (otherArgs.length != 2) { System.out.println("参数错误"); System.exit(2); } Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(otherArgs[0])); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
毕竟如上的输入数据比较少,大家可以动手查找自然语言处理的语料包添加到input文件中,感受以下大数据的魅力
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。