【发布时间】:2015-07-20 03:55:00
【问题描述】:
我有一个简单的 MapReduce 作业,它应该从文本文件中读取字典,然后他们逐行处理另一个巨大的文件并计算逆文档矩阵。输出应该是这样的:
word-id1 docX:tfX docY:tfY
word-id2 docX:tfX docY:tfY etc...
但是,reducer 的输出仅在一个 huuuge 行中发出。我不明白为什么,因为它应该为每个 word-id 发出一个新行(这是减速器的关键)。
映射器产生正确的输出(一对word-id 和doc-id:tf 的值在不同的行上)。我在没有减速器的情况下进行了测试。 reducer 应该只是将对应于同一键的值附加到每个键的一行上。
能否请您看一下我的代码(特别是关于作业的reducer 和配置)并告诉我为什么reducer 只发出一个大行而不是对应于指定键的多行?我花了很多时间调试它,但无法理解它。
public class Indexer extends Configured implements Tool {
/*
* Vocabulary: key = term, value = index
*/
private static Map<String, Integer> vocab = new HashMap<String, Integer>();
public static void main(String[] arguments) throws Exception {
System.exit(ToolRunner.run(new Indexer(), arguments));
}
public static class Comparator extends WritableComparator {
protected Comparator() {
super(Text.class, true);
}
@Override
public int compare(WritableComparable a, WritableComparable b) {
// Here we use exploit the implementation of compareTo(...) in
// Text.class.
return -a.compareTo(b);
}
}
public static class IndexerMapper extends
Mapper<Object, Text, IntWritable, Text> {
private Text result = new Text();
// load vocab from distributed cache
public void setup(Context context) throws IOException {
Configuration conf = context.getConfiguration();
FileSystem fs = FileSystem.get(conf);
URI[] cacheFiles = DistributedCache.getCacheFiles(conf);
Path getPath = new Path(cacheFiles[0].getPath());
BufferedReader bf = new BufferedReader(new InputStreamReader(
fs.open(getPath)));
String line = null;
while ((line = bf.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, " \t");
int index = Integer.parseInt(st.nextToken()); // first token is the line number - term id
String word = st.nextToken(); // second element is the term
// save vocab
vocab.put(word, index);
}
}
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
// init TF map
Map<String, Integer> mapTF = new HashMap<String, Integer>();
// parse input string
StringTokenizer st = new StringTokenizer(value.toString(), " \t");
// first element is doc index
int index = Integer.parseInt(st.nextToken());
// count term frequencies
String word;
while (st.hasMoreTokens()) {
word = st.nextToken();
// check if word is in the vocabulary
if (vocab.containsKey(word)) {
if (mapTF.containsKey(word)) {
int count = mapTF.get(word);
mapTF.put(word, count + 1);
} else {
mapTF.put(word, 1);
}
}
}
// compute TF-IDF
int wordIndex;
for (String term : mapTF.keySet()) {
int tf = mapTF.get(term);
if (vocab.containsKey(term)) {
wordIndex = vocab.get(term);
context.write(new IntWritable(wordIndex), new Text(index + ":" + tf));
}
}
}
}
public static class IndexerReducer extends Reducer<IntWritable, Text, IntWritable, Text>
{
@Override
public void reduce(IntWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException
{
StringBuilder sb = new StringBuilder(16000);
for (Text value : values)
{
sb.append(value.toString() + " ");
}
context.write(key, new Text(sb.toString()));
}
}
/**
* This is where the MapReduce job is configured and being launched.
*/
@Override
public int run(String[] arguments) throws Exception {
ArgumentParser parser = new ArgumentParser("TextPreprocessor");
parser.addArgument("input", true, true, "specify input directory");
parser.addArgument("output", true, true, "specify output directory");
parser.parseAndCheck(arguments);
Path inputPath = new Path(parser.getString("input"));
Path outputDir = new Path(parser.getString("output"));
// Create configuration.
Configuration conf = getConf();
// add distributed file with vocabulary
DistributedCache
.addCacheFile(new URI("/user/myslima3/vocab.txt"), conf);
// Create job.
Job job = new Job(conf, "WordCount");
job.setJarByClass(IndexerMapper.class);
// Setup MapReduce.
job.setMapperClass(IndexerMapper.class);
//job.setCombinerClass(IndexerReducer.class);
job.setReducerClass(IndexerReducer.class);
// Sort the output words in reversed order.
job.setSortComparatorClass(Comparator.class);
job.setNumReduceTasks(1);
// Specify (key, value).
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(Text.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class);
// Input.
FileInputFormat.addInputPath(job, inputPath);
job.setInputFormatClass(TextInputFormat.class);
// Output.
FileOutputFormat.setOutputPath(job, outputDir);
job.setOutputFormatClass(TextOutputFormat.class);
FileSystem hdfs = FileSystem.get(conf);
// Delete output directory (if exists).
if (hdfs.exists(outputDir))
hdfs.delete(outputDir, true);
// Execute the job.
return job.waitForCompletion(true) ? 0 : 1;
}
}
【问题讨论】:
-
只是为了确认一下,您在映射器输出中获得了不同的键??您还可以更新示例输出。如果您有分隔符,也只需在写字板中查看,如果您的线条非常大,您可能会忽略线条。
-
是的,我从映射器获得了不同的键,这已确认...输出表单映射器是键 [TAB] 值
-
你如何确认你的映射器输出是正确的?你把减速器的数量设为0了吗?另外我认为您需要将对象投射到比较器中?只是尝试删除自定义比较器,看看它是否有任何改变?
-
按照您的猜测,我使用 0 个 reducer 运行了 Mapper,并检查了输出是否有意义。感谢您提供有关比较器的提示 - 我会尽快将其删除并在此处发布它是如何进行的。
-
当然...如果有效,请告诉我。