【发布时间】:2015-03-30 23:10:11
【问题描述】:
Map Reduce 程序,它将两个文件作为输入,并给出两个文件中的一组单词(两个文件的交集。)
我试过这样..
Map 函数:将文件作为输入并给出 (word, 1) 作为输出。我在一个名为 part-r-00000 的文件中得到了这个输出。我对这两个文件都做了这一步,现在我有两个文件(两个部分-r-00000 文件。)
如何将这些文件作为输入提供给 Reduce 函数。
并给我一些建议来编写两个文件交集的reduce函数..
这是字数统计示例程序:
package org.apache.hadoop.examples;
import java.io.IOException;
import java.util.StringTokenizer;
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 org.apache.hadoop.util.GenericOptionsParser;
public class WordCountMap {
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 GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Job job = new Job(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);
}
}
reducer 类在注释中,所有与 reducer 类相关的行都在注释中,但我仍然得到一个文件 part-r-00000.。输出是
海1 这 1 一个 1 是 1 是 1 检查 1 示例 1 示例 1 示例 1 公平 1 文件 1 加尼什 1 Hadoop 1 怎么样 1 马力 1 是 1 是 1 是 1 地图 1 不是 1 只有1个 程序。 1 减少 1 所以 1 这个 1 这个 1 到 1 你 1 你 1
【问题讨论】:
-
part-r-0000文件来自减速器本身。也许在您的地图功能中,您应该使用一个标志来区分来自第一个文件和第二个文件的数据。并且在 reducer 中使用标志来比较值(这个逻辑似乎有很多冗余)和write仅在两者中都存在的那些 -
如果你没有指定 Reducer,它会选择默认的 Identity Reducer。这只是从映射器中获取数据并按原样打印。由于您使用的是新 API,请参阅 this