我正在尝试将String转换为Text对象,并将值作为MapOutputKey输出到Reducer。但是,在reducer中,键看起来像一个带有值(而不是MapOutputValue)的黑色空间作为内存位置。 但是,如果我将String硬编码到文本对象中并将其发送出去,我可以在Reducer中看到正确的值。 编辑: - 这是非常奇怪和有趣的。相同的程序适用于较小的数据集,即从映射器发送的文本密钥在reducer中接收。但是如果我使用原始数据集,则从地图发送的文本键不会反映在reducer中。 我不知道,Hadoop有一些我不知道的东西。 以下是完整的源代码: -
public class InvertedIndex {
public InvertedIndex(){}
public static class InvertedMap extends Mapper<IntWritable,Text,Text,DocAttributes> {
Integer docNum;
String word;
Integer docFrequency;
Integer termFrequency;
private Text key = new Text();
@Override
public void map(IntWritable mapIn,Text mapValIn,Context context) throws IOException,InterruptedException{
try{
String line = mapValIn.toString();
String[] words = line.trim().split("\\s");
List<String> wordList = new ArrayList<String>(Arrays.asList(words));
String k;
for(int i=0; i<wordList.size();i++){
k = wordList.get(i);
//Text key = new Text(k);
// DocAttributes da = new DocAttributes();
key.set(k);
int sum=1;
for(int j=i;j<wordList.size();j++){
if(wordList.get(i).matches(wordList.get(j)) && j>i){
sum++;
docNum = mapIn.get();
docFrequency = sum;
word = wordList.get(i);
termFrequency = sum;
wordList.remove(j);
}
else{
docNum = mapIn.get();
docFrequency = sum;
word = wordList.get(i);
termFrequency = sum;
}
}
if(i == words.length-1){
docNum = mapIn.get();
docFrequency = sum;
word = wordList.get(i);
termFrequency = sum;
}
context.write(key, new DocAttributes(docNum,word,docFrequency,termFrequency));
}
}
catch(NullPointerException ne){
ne.printStackTrace();
}
}
}
public static class InvertedReduce extends Reducer<Text,DocAttributes,LongWritable,DocAttributes>{
@Override
public void reduce(Text key, Iterable<DocAttributes> value,Context context) throws IOException,InterruptedException{
Iterator<DocAttributes> iterator = value.iterator();
DocAttributes doc = new DocAttributes();
List<DocAttributes> list = new ArrayList<DocAttributes>();
while(iterator.hasNext()){
list.add(new DocAttributes(iterator.next()));
}
Integer docFrequency = 0;
for(DocAttributes d : list){
docFrequency += d.getDocFrequency();
doc.setDocNum(d.getDocNum());
doc.setWord(d.getWord());
doc.setDocFrequency(docFrequency);
doc.setTermFrequency(d.getTermFrequency());
}
context.write(new LongWritable(), doc);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setMapperClass(InvertedMap.class);
// job.setCombinerClass(InvertedCombine.class);
job.setReducerClass(InvertedReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(DocAttributes.class);
job.setJarByClass(InvertedIndex.class);
TextInputFormat.addInputPath(job, new Path(args[0]));
TextOutputFormat.setOutputPath(job,new Path(args[1]));
job.setInputFormatClass(DocInput.class);
job.setOutputFormatClass(TextOutputFormat.class);
job.submit();
job.waitForCompletion(true);
}
}
答案 0 :(得分:0)
您正在迭代时使用for循环修改ArrayList'wordList'。您可以尝试使用Iterator循环在迭代时删除元素(Iterator.remove())。