Доступ к индексу 0 всегда работает.
Код: Выделить всё
/*
* Rearranges the (key,value) pairs to have only the document as the key
*
* Input: ( (word@document) , wordCount )
* Output: ( document , (word=wordCount) )
*/
public static class DSMapper extends Mapper {
// map function that creates a (key,value) pair for every word in the document
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
List slist = Arrays.asList(value.toString().split("@"));
String word = slist.get(0);
List slist2 = Arrays.asList(slist.get(1).split("\t"));
String doc = slist2.get(0);
String wordcount = slist2.get(1);
Text processed_word = new Text(word + "=" + wordcount);
Text processed_key = new Text(doc);
context.write(processed_key, processed_word);
}
}
/*
* For each identical key (document), reduces the values (word=wordCount) into a sum (docSize)
*
* Input: ( document , (word=wordCount) )
* Output: ( (word@document) , (wordCount/docSize) )
*
* docSize = total number of words in the document
*/
public static class DSReducer extends Reducer {
public static final Log log = LogFactory.getLog(DSReducer.class);
public void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {
int docsize = 0;
for(Text val : values){
String ww = val.toString().split("=")[1];
String word = val.toString().substring(ww.length());
// word = word.replaceAll("[=]", "");
Text w = new Text(word);
int length = val.getLength();
String len = Integer.toString(length);
Text t = new Text("length: "+len);
context.write(key, w);
}
}
}
- длина печати val
- Обратите внимание, что val .splitString().split("=")[0] работает.
- индекс 1 вызывает ошибку.
- Я убедился, что каждая строка имеет знак "=" и данные после него.
Подробнее здесь: https://stackoverflow.com/questions/781 ... ops-reduce