nextKeyValue() - это основная функция, которая устанавливает пару ключей и значений для конкретного вызова карты. Таким образом, из вашей ссылки, приведенный ниже код (до цикла for) он просто устанавливает ключ с помощью pos, который является ничем иным, кроме смещения начала. key.set(pos)
И он буферизует ранее установленное значение. Соответствующий код:
public boolean nextKeyValue() throws IOException, InterruptedException {
if (key == null) {
key = new LongWritable();
}
key.set(pos);
if (value == null) {
value = new Text();
}
value.clear();
final Text endline = new Text("\n");
int newSize = 0;
После петли. Я добавил достаточные комментарии для каждой строки.
for(int i=0;i<NLINESTOPROCESS;i++){ //Since this is NLineInputFormat they want to read 3 lines at a time and set that as value,
so this loop will continue until that is satisfied.
Text v = new Text();
while (pos < end) { //This is to prevent the recordreader from reading the second split, if it is currently reading the first split. pos would be start
of the split and end would be end offset of the split.
newSize = in.readLine(v, maxLineLength,Math.max((int)Math.min(Integer.MAX_VALUE, end-pos),maxLineLength));
//This calls the linereader readline function which reads until it encounters a newline(default delim for TextInputformat and maxlinelength would be max integer size
just to ensure the whole line doesn''t go beyond the maxlinelength and the line read would be stored in Text variable v)
value.append(v.getBytes(),0, v.getLength());
//Reads from v(whole line) and appends it to the value,append is necessary because we are going to read 3 lines.
value.append(endline.getBytes(),0, endline.getLength());
//appends newline to each line read
if (newSize == 0) {
break;//If there is nothing to read then come out.
}
pos += newSize;
if (newSize < maxLineLength) {//There is a flaw here it should be >=, to imply if the read line is greater than max integer size then come out
break;
}
}
}
if (newSize == 0) {
key = null;//If there is nothing to read assign key and value as null else continue the process by returning true to map call.
value = null;
return false;
} else {
return true;
}
}
}